Class: EMRPC::MultithreadedClient

Inherits:
Object
  • Object
show all
Defined in:
lib/emrpc/blocking_api/multithreaded_client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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(options)
  @backends = options[:backend] && [options[:backend]] || options[:backends] or raise "No backends supplied!"
  @pool     = options[:queue] || ::Queue.new
  @timeout  = options[:timeout] || 5
  @timer    = options[:timer] || Timers::EVENTED
  @timeout_thread = @timer.call(@timeout, method(:timer_action!))
  @backends.each do |backend|
    @pool.push(backend)
  end
end

Instance Attribute Details

#backendsObject (readonly)

Returns the value of attribute backends.



5
6
7
# File 'lib/emrpc/blocking_api/multithreaded_client.rb', line 5

def backends
  @backends
end

#poolObject (readonly)

Returns the value of attribute pool.



5
6
7
# File 'lib/emrpc/blocking_api/multithreaded_client.rb', line 5

def pool
  @pool
end

#timeoutObject (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