Out of band stuff?

Eric Wong normalperson at yhbt.net
Mon Apr 9 03:05:52 UTC 2012


Damian Janowski <jano at dimaion.com> wrote:
> Hello everyone,
> 
> I'm needing to perform a couple of tasks outside of the request cycle.
> For instance, I want to ping a web service every X minutes so that I
> can maintain a persistent connection to it and have better response
> times for some requests that I have to do *inside* the request cycle.
> 
> What would be the reasonable way of doing this?

For Raindrops::Watcher, I start a background thread on the first request:

	git clone git://bogomips.org/raindrops.git
	$EDITOR lib/raindrops/watcher.rb

If you look at the `call' method in raindrops/watcher.rb, the important
line is this:

	# @start is a Mutex and @thr is nil in the initialize method.

	@start.synchronize { @thr ||= aggregator_thread(env["rack.logger"]) }

I avoid starting the background thread at initialization because it'll
die on fork (meaning I'd have to recheck it all the time, anyways).

The overhead of Mutex#synchronize and @thr||= assignment is negligible
in most apps.

> I'm thinking the OOB GC is just another example. But the current way
> the OOB GC is written makes one think it's a very specific case and
> not something one should use for other purposes.

Yes, Unicorn::OobGC is very specific to how Unicorn works and the way it
works wouldn't translate well to Rainbows! (as Rainbows! does persistent
connections and unicorn does not).


More information about the rainbows-talk mailing list