RubyDNS 0.7.0 - Using Fiber for improved DSL

Hi Everyone,

I wouldn’t normally share an incremental release, but I thought that the
way I used Fibers in the latest version of RubyDNS might be interesting
to
others.

Previously, RubyDNS used EventMachine deferrables for managing
“sequential”
event based processing. The code to wire up sequential events was fairly
unintuitive and for clients using the RubyDNS DSL, it was even more
buggy/confusing.

Enter Fibers. Now deferred and sequential processing follow the same
pathway. For each transaction (incoming DNS request) a Fiber is created.
Once the Fiber terminates, the result is returned to the client. By
default, the execution is linear, but if the user wants to pass the DNS
request upstream, the fiber is suspended until the request is done (or
fails).

When I first started looking at fibers, I didn’t realise how powerful
they
are because they avoid tangled event driven code for the user and for
the
most part hide the implementation details regarding what might take time
to
complete. Now, I feel confident that RubyDNS has improved both the
intuitive nature of the DSL, and also the actual implementation is
easier
to follow/test.

For more details, please see the RubyDNS project page:
http://www.codeotaku.com/projects/rubydns/index.en or browse the latest
source code:
https://github.com/ioquatix/rubydns/blob/master/lib/rubydns/server.rb#L75-L94

Here is the code, pre-fiberization:
https://github.com/ioquatix/rubydns/blob/0505d52f6078919eceb5e0599f8b225f9a54dffb/lib/rubydns/server.rb#L189-L238

Fiber is a really nice feature, looking forward to using it in more
projects.

Kind regards,
Samuel

On 02/02/2014 06:27 AM, Samuel W. wrote:

For more details, please see the RubyDNS project page:
http://www.codeotaku.com/projects/rubydns/index.en or browse the latest
source code:
https://github.com/ioquatix/rubydns/blob/master/lib/rubydns/server.rb#L75-L94

Here is the code, pre-fiberization:

https://github.com/ioquatix/rubydns/blob/0505d52f6078919eceb5e0599f8b225f9a54dffb/lib/rubydns/server.rb#L189-L238

Very nice. So, next time I see a chain of lambdas, I’ll try to replace
it with a fiber.

Very OT, but it reminded me of this example from SimPy:

http://simpy.readthedocs.org/en/latest/examples/gas_station_refuel.html

This uses (python) generators to preserve the imperative flow of an
actor, while also allowing interaction with the simulation.
(Python’s generators seem to be more or less fibers, but I know little
python.)

Be sure to check out Celluloid if you’re interested in these sorts of
patterns:

http://celluloid.io

On Tue, Feb 4, 2014 at 12:18 PM, Joel VanderWerf