From dd2b2274ef0cd8a121cf7655ade939a1f63bc971 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Wed, 25 Nov 2009 19:01:34 -0800 Subject: AppPool middleware now compatible with Fibers This enables the safe use of Rainbows::AppPool with all concurrency models, not just threaded ones. AppPool is now effective with *all* Fiber-based concurrency models including Revactor (and of course the new Fiber{Pool,Spawn} ones). --- lib/rainbows/app_pool.rb | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) (limited to 'lib/rainbows/app_pool.rb') diff --git a/lib/rainbows/app_pool.rb b/lib/rainbows/app_pool.rb index f4c22a2..5c85e0a 100644 --- a/lib/rainbows/app_pool.rb +++ b/lib/rainbows/app_pool.rb @@ -42,11 +42,8 @@ module Rainbows # AppPool has no effect on the Rev or EventMachine concurrency models # as those are single-threaded/single-instance as far as application # concurrency goes. In other words, +P+ is always +one+ when using - # Rev or EventMachine. AppPool currently only works with the - # ThreadSpawn and ThreadPool models. It does not yet work reliably - # with the Revactor model, but actors are far more lightweight and - # probably better suited for lightweight applications that would - # not benefit from AppPool. + # Rev or EventMachine. As of \Rainbows! 0.7.0, it is safe to use with + # Revactor and the new FiberSpawn and FiberPool concurrency models. # # Since this is Rack middleware, you may load this in your Rack # config.ru file and even use it in threaded servers other than @@ -60,7 +57,7 @@ module Rainbows # You may to load this earlier or later in your middleware chain # depending on the concurrency/copy-friendliness of your middleware(s). - class AppPool < Struct.new(:pool) + class AppPool < Struct.new(:pool, :re) # +opt+ is a hash, +:size+ is the size of the pool (default: 6) # meaning you can have up to 6 concurrent instances of +app+ @@ -86,6 +83,20 @@ module Rainbows # Rack application endpoint, +env+ is the Rack environment def call(env) + + # we have to do this check at call time (and not initialize) + # because of preload_app=true and models being changeable with SIGHUP + # fortunately this is safe for all the reentrant (but not multithreaded) + # classes that depend on it and a safe no-op for multithreaded + # concurrency models + self.re ||= begin + case env["rainbows.model"] + when :FiberSpawn, :FiberPool, :Revactor + self.pool = Rainbows::Fiber::Queue.new(pool) + end + true + end + app = pool.shift app.call(env) ensure -- cgit v1.2.3-24-ge0c7