Skip to content

Commit

Permalink
Merge 54e55b5 into e04e28c
Browse files Browse the repository at this point in the history
  • Loading branch information
Shogun committed Dec 4, 2013
2 parents e04e28c + 54e55b5 commit 5eff63f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
14 changes: 11 additions & 3 deletions lib/rack/fiber_pool.rb
Expand Up @@ -12,8 +12,10 @@ class FiberPool
# @param [Hash] opts the options to configure the fiber pool
# @option opts [Fixnum] :size Size of the fiber pool (concurrent requests)
# @option opts [String] :rescue_exception your custom exception handler
# @option opts [TrueClass|FalseClass] :remove_async_callback if remove Rack async.callback inside the fiber
def initialize(app, options = {})
@app = app
@options = options
@fibers = []
@count = options[:size] || SIZE
@count.times do
Expand All @@ -36,11 +38,17 @@ def call(parent_env)
private

def request(env)
env['async.orig_callback'] = env.delete('async.callback')
env['async.fiberpool_callback'] ||= env['async.callback']
env.delete('async.callback') if @options[:remove_async_callback]

result = @app.call(env)
env['async.orig_callback'].call result
async_callback(env).call result
rescue Exception => exc
env['async.orig_callback'].call @rescue_exception.call(env, exc)
async_callback(env).call @rescue_exception.call(env, exc)
end

def async_callback(env)
env['async.fiberpool_callback'] || env['async.callback']
end

def fiber_loop(block)
Expand Down
19 changes: 17 additions & 2 deletions spec/lib/fiber_pool/fiber_pool_spec.rb
Expand Up @@ -8,15 +8,24 @@
class TestBuggyApp
attr_accessor :result
def call(env)
env['async.orig_callback'] = proc { |result| @result = result }
env['async.fiberpool_callback'] = proc { |result| @result = result }
fail Exception, 'I\'m buggy! Please fix me.'
end
end

class TestApp
attr_accessor :result
def call(env)
env['async.orig_callback'] = proc { |result| @result = result }
env['async.fiberpool_callback'] = proc { |result| @result = result }
[200, { 'Content-Type' => 'text/plain' }, ['Hello world!']]
end
end

class OriginalSyncApp
attr_accessor :result

def call(env)
env['async.callback'] = proc { |result| @result = result }
[200, { 'Content-Type' => 'text/plain' }, ['Hello world!']]
end
end
Expand All @@ -40,6 +49,12 @@ def call(env)
it { should eql [200, { 'Content-Type' => 'text/plain' }, ['Hello world!']] }
end

describe 'remove_async_callback' do
let(:app) { OriginalSyncApp.new }
let(:options) { { remove_async_callback: false } }
it { should eql [200, { 'Content-Type' => 'text/plain' }, ['Hello world!']] }
end

describe 'exception' do
let(:app) { TestBuggyApp.new }
it { should eql [500, {}, ['Exception: I\'m buggy! Please fix me.']] }
Expand Down

0 comments on commit 5eff63f

Please sign in to comment.