Class: Net::HTTP::Pool::Connections

Inherits:
Object
  • Object
show all
Includes:
Celluloid
Defined in:
lib/net/http/pool.rb

Overview

Public: The Connection Pool.

host - The string of the host. options - The hash of options (default: {}).

:size - The integer size of the pool (default: 5)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, options = {}) ⇒ Connections

Returns a new instance of Connections.



20
21
22
23
24
25
26
27
# File 'lib/net/http/pool.rb', line 20

def initialize(host, options = {})
  @current_index = 0
  @pool = Array.new(options.fetch(:size, 5)) do
    connection = Net::HTTP::Persistent.new(host)
    connection.idle_timeout = nil
    connection
  end
end

Instance Attribute Details

#poolObject (readonly)

Returns the value of attribute pool.



18
19
20
# File 'lib/net/http/pool.rb', line 18

def pool
  @pool
end

Instance Method Details

#round_robin {|@pool[@current_index]| ... } ⇒ Object

Private: Round robin one of the connections

&block - The block to be called passing the current connection

Yields:

  • (@pool[@current_index])

Raises:

  • (LocalJumpError)


32
33
34
35
36
37
# File 'lib/net/http/pool.rb', line 32

def round_robin(&block)
  raise LocalJumpError unless block

  @current_index = @current_index > @pool.size ? 0 : @current_index + 1
  yield @pool[@current_index]
end

#with(&block) ⇒ Object

Public: Helper to access the connection asynchronous

&block - The block to be called passing the current connection



42
43
44
# File 'lib/net/http/pool.rb', line 42

def with(&block)
  round_robin! &block
end