Class: EMRPC::MultithreadedClient
- Inherits:
-
Object
- Object
- EMRPC::MultithreadedClient
- Defined in:
- lib/emrpc/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
A new instance of MultithreadedClient.
- #send_message(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
Returns a new instance of MultithreadedClient.
6 7 8 9 10 11 12 13 14 |
# File 'lib/emrpc/multithreaded_client.rb', line 6 def initialize() @backends = [:backends] or raise "No backends supplied!" @pool = [:queue] || ::Queue.new @timeout = [:timeout] || 5 @timeout_thread = Thread.new { 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/multithreaded_client.rb', line 5 def backends @backends end |
#pool ⇒ Object (readonly)
Returns the value of attribute pool.
5 6 7 |
# File 'lib/emrpc/multithreaded_client.rb', line 5 def pool @pool end |
#timeout ⇒ Object (readonly)
Returns the value of attribute timeout.
5 6 7 |
# File 'lib/emrpc/multithreaded_client.rb', line 5 def timeout @timeout end |
Instance Method Details
#send_message(meth, args, blk) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/emrpc/multithreaded_client.rb', line 16 def (meth, args, blk) start = Time.now # wait for the available connections here while :timeout == (backend = @pool.pop) 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.(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.
34 35 36 37 |
# File 'lib/emrpc/multithreaded_client.rb', line 34 def timer_action! sleep @timeout @pool.num_waiting.times { @pool.push(:timeout) } end |