Class: VCAP::Services::Base::Barrier
Instance Method Summary collapse
- #call(*args) ⇒ Object
- #callback ⇒ Object
-
#initialize(options = {}, &callback) ⇒ Barrier
constructor
A new instance of Barrier.
- #on_timeout ⇒ Object
Constructor Details
#initialize(options = {}, &callback) ⇒ Barrier
Returns a new instance of Barrier.
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/base/barrier.rb', line 14 def initialize( = {}, &callback) raise ArgumentError unless [:timeout] || [:callbacks] @lock = Mutex.new @callback = callback @expected_callbacks = [:callbacks] @timer = EM.add_timer([:timeout]) {on_timeout} if [:timeout] @callback_fired = false @responses = [] = Proc.new {|*args| call(*args)} end |
Instance Method Details
#call(*args) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/base/barrier.rb', line 34 def call(*args) @lock.synchronize do unless @callback_fired @responses << args if @expected_callbacks @expected_callbacks -= 1 if @expected_callbacks <= 0 EM.cancel_timer(@timer) if @timer @callback_fired = true @callback.call(@responses) end end end end end |
#callback ⇒ Object
50 51 52 |
# File 'lib/base/barrier.rb', line 50 def callback end |
#on_timeout ⇒ Object
25 26 27 28 29 30 31 32 |
# File 'lib/base/barrier.rb', line 25 def on_timeout @lock.synchronize do unless @callback_fired @callback_fired = true @callback.call(@responses) end end end |