Class: EMRPC::MultithreadedClient
- Defined in:
- lib/emrpc/blocking_api/multithreaded_client.rb
Instance Attribute Summary collapse
-
#backends ⇒ Object
readonly
Returns the value of attribute backends.
-
#pool ⇒ Object
readonly
Returns the value of attribute pool.
-
#timeout ⇒ Object
readonly
Returns the value of attribute timeout.
Instance Method Summary collapse
-
#initialize(options) ⇒ MultithreadedClient
constructor
Options: :backends - an array of backends implementing send(meth, *args) method.
- #send(meth, *args, &blk) ⇒ Object
-
#timer_action! ⇒ Object
Pushes :timeout message to a queue for all the threads in a backlog every @timeout seconds.
Constructor Details
#initialize(options) ⇒ MultithreadedClient
Options:
:backends - an array of backends implementing send(meth, *args) method.
:backend - specify a single backend instead of array of :backends (just a friendly shortcut)
:queue - an optional queue object (default is Queue.new from the standard library)
:timeout - an optional timeout in seconds (default is 5 seconds)
:timer - optional proc, which accepts two arguments: +timeout+ and
another proc to be called every +timeout+ seconds.
Default is Proc.new{|t,p| Thread.new{sleep(t); p.call}}.
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/emrpc/blocking_api/multithreaded_client.rb', line 15 def initialize() @backends = [:backend] && [[:backend]] || [:backends] or raise "No backends supplied!" @pool = [:queue] || ::Queue.new @timeout = [:timeout] || 5 @timer = [:timer] || Timers::EVENTED @timeout_thread = @timer.call(@timeout, method(:timer_action!)) @backends.each do |backend| @pool.push(backend) end end |
Instance Attribute Details
#backends ⇒ Object (readonly)
Returns the value of attribute backends.
5 6 7 |
# File 'lib/emrpc/blocking_api/multithreaded_client.rb', line 5 def backends @backends end |
#pool ⇒ Object (readonly)
Returns the value of attribute pool.
5 6 7 |
# File 'lib/emrpc/blocking_api/multithreaded_client.rb', line 5 def pool @pool end |
#timeout ⇒ Object (readonly)
Returns the value of attribute timeout.
5 6 7 |
# File 'lib/emrpc/blocking_api/multithreaded_client.rb', line 5 def timeout @timeout end |
Instance Method Details
#send(meth, *args, &blk) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/emrpc/blocking_api/multithreaded_client.rb', line 26 def send(meth, *args, &blk) start = Time.now # wait for the available connections here # if @timeout_thread sent a :timeout message thru the queue, # be ready to raise a PoolTimeout exception. while :timeout == (backend = @pool.shift) seconds = Time.now - start if seconds > @timeout raise PoolTimeout, "Thread #{Thread.current} waited #{seconds} seconds for backend connection in a pool. Pool size is #{@backends.size}. Maybe too many threads are running concurrently. Increase the pool size or decrease the number of threads." end end begin # Backend can throw its own exceptions which must be caught somewhere outside. backend.send(meth, *args, &blk) ensure # Always push backend to a pool after using it! @pool.push(backend) end end |
#timer_action! ⇒ Object
Pushes :timeout message to a queue for all the threads in a backlog every @timeout seconds.
47 48 49 |
# File 'lib/emrpc/blocking_api/multithreaded_client.rb', line 47 def timer_action! @pool.num_waiting.times { @pool.push(:timeout) } end |