Class: Rack::FiberPool

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/fiber_pool.rb

Constant Summary collapse

VERSION =
'0.9.2'
SIZE =
100

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) {|@fiber_pool| ... } ⇒ FiberPool

The size of the pool is configurable:

use Rack::FiberPool, :size => 25

Yields:

  • (@fiber_pool)


11
12
13
14
15
16
# File 'lib/rack/fiber_pool.rb', line 11

def initialize(app, options={})
  @app = app
  @fiber_pool = ::FiberPool.new(options[:size] || SIZE)
  @rescue_exception = options[:rescue_exception] || Proc.new { |env, e| [500, {}, "#{e.class.name}: #{e.message.to_s}"] }
  yield @fiber_pool if block_given?
end

Instance Method Details

#call(env) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rack/fiber_pool.rb', line 18

def call(env)
  call_app = lambda do
    begin
      result = @app.call(env)
      env['async.callback'].call result
    rescue ::Exception => e
      env['async.callback'].call @rescue_exception.call(env, e)
    end
  end

  @fiber_pool.spawn(&call_app)
  throw :async
end