A fast, simple and pure Ruby reactor library
0
Please welcome Reactor, a reactor library with the very original name of "Reactor".
What is a reactor any way?
A reactor library is one that provides an asynchronus event handling mechanism. Ruby already has a couple of those, the most prominent being EventMachine and Rev. Many high performing Ruby applications like Thin and Evented Mongrel are utilizing EventMachine for event handling. Both Rev and EventMachine build atop native reactor implementations written in C or C++; while this ensures high performance, it makes some integration aspects with Ruby a bit quirky, sometimes even at a noticable performance cost.
This is why I thought of building Reactor, a much simpler reactor library in pure Ruby that attempts to use as much as possible of the Ruby built-in classes and standard libraries. It only provides a minimal API that does not attempt to be so smart. It differs from EventMachine and Rev in the following aspects:
- Pure Ruby, no C or C++ code involved
- Very small (~100 lines of code)
- Uses the vanilla Ruby socket and server implementations
- Decent (high) performance on Ruby 1.9.1
- Ruby threading friendly (naturally)
- You can have multiple reactors running (like Rev and unlike EventMachine)
Usage is simple; here's a simple Echo server that uses Reactor
require 'reactor'
require 'socket'
reactor = Reactor::Base.new
server = TCPServer.new("0.0.0.0",8080)
reactor.attach(:read, server) do |server|
conn = server.accept
conn.write(conn.gets)
conn.close
end
reactor.run # blocking call, will run for ever
The server is a normal Ruby TCPServer. It attaches itself to the reactor and asks to be notified if there is data to be read on the wire. A block is provided that will handle those notifications. Alternatively, the server can implement a notify_readable method that will be fired instead.
Any IO object can be attached to the reactor, but it doesn't make much sense to attach actual files since they will be blocked upon reading or writing anyway. Sockets and pipes will work in a non-blocking manner though.
Reactor is using Ruby's IO.select behind the scenes. This limits its ability to scale in comparison to something like EventMachine or Rev, which are able to utilize Epoll and Kqueue, both of which scale much better. This is not a major concern though. Most servers listen to a few fds most of the time, which is a bit faster when using select. Besides, one can hope that Ruby will be able to use Epoll and Kqueue some day, which will translate as a direct benefit to Reactor.
Written By:
Muhammad A. Ali (oldmoe.blogspot.com)
Post a Comment
eSpace podcast Prodcast
Archive
- September 2011
- April 2011
- March 2011
- December 2010
- November 2010
- September 2010
- August 2010
- July 2010
- June 2010
- April 2010
- March 2010
- November 2009
- October 2009
- September 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- November 2008
- October 2008
- September 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
- January 2008
- April 2007
- March 2007
Latest Comments
- SpectraMind Commented on Egypt Wins UK's National Outsourcing Association Award
- Rofaida Awad Commented on Go Egypt Go!
- Different Mike Commented on Only idiots change their iPhone root password!
- Mike Commented on Only idiots change their iPhone root password!
- smile Commented on Only idiots change their iPhone root password!

