class HTTPX::Pool

  1. lib/httpx/pool.rb
Superclass: Object

Constants

POOL_TIMEOUT = 5  

Public Class methods

new(options)

Sets up the connection pool with the given options, which can be the following:

:max_connections_per_origin

the maximum number of connections held in the pool pointing to a given origin.

:pool_timeout

the number of seconds to wait for a connection to a given origin (before raising HTTPX::PoolTimeoutError)

[show source]
   # File lib/httpx/pool.rb
19 def initialize(options)
20   @max_connections_per_origin = options.fetch(:max_connections_per_origin, Float::INFINITY)
21   @pool_timeout = options.fetch(:pool_timeout, POOL_TIMEOUT)
22   @resolvers = Hash.new { |hs, resolver_type| hs[resolver_type] = [] }
23   @resolver_mtx = Thread::Mutex.new
24   @connections = []
25   @connection_mtx = Thread::Mutex.new
26   @origin_counters = Hash.new(0)
27   @origin_conds = Hash.new { |hs, orig| hs[orig] = ConditionVariable.new }
28 end

Public Instance methods

checkin_connection(connection)
[show source]
   # File lib/httpx/pool.rb
61 def checkin_connection(connection)
62   return if connection.options.io
63 
64   @connection_mtx.synchronize do
65     @connections << connection
66 
67     @origin_conds[connection.origin.to_s].signal
68   end
69 end
checkin_resolver(resolver)
[show source]
    # File lib/httpx/pool.rb
100 def checkin_resolver(resolver)
101   @resolver_mtx.synchronize do
102     resolvers = @resolvers[resolver.class]
103 
104     resolver = resolver.multi
105 
106     resolvers << resolver unless resolvers.include?(resolver)
107   end
108 end
checkout_connection(uri, options)

opens a connection to the IP reachable through uri. Many hostnames are reachable through the same IP, so we try to maximize pipelining by opening as few connections as possible.

[show source]
   # File lib/httpx/pool.rb
42 def checkout_connection(uri, options)
43   return checkout_new_connection(uri, options) if options.io
44 
45   @connection_mtx.synchronize do
46     acquire_connection(uri, options) || begin
47       if @origin_counters[uri.origin] == @max_connections_per_origin
48 
49         @origin_conds[uri.origin].wait(@connection_mtx, @pool_timeout)
50 
51         return acquire_connection(uri, options) || raise(PoolTimeoutError.new(uri.origin, @pool_timeout))
52       end
53 
54       @origin_counters[uri.origin] += 1
55 
56       checkout_new_connection(uri, options)
57     end
58   end
59 end
checkout_mergeable_connection(connection)
[show source]
   # File lib/httpx/pool.rb
71 def checkout_mergeable_connection(connection)
72   return if connection.options.io
73 
74   @connection_mtx.synchronize do
75     idx = @connections.find_index do |ch|
76       ch != connection && ch.mergeable?(connection)
77     end
78     @connections.delete_at(idx) if idx
79   end
80 end
checkout_resolver(options)
[show source]
   # File lib/httpx/pool.rb
86 def checkout_resolver(options)
87   resolver_type = options.resolver_class
88   resolver_type = Resolver.resolver_for(resolver_type)
89 
90   @resolver_mtx.synchronize do
91     resolvers = @resolvers[resolver_type]
92 
93     idx = resolvers.find_index do |res|
94       res.options == options
95     end
96     resolvers.delete_at(idx) if idx
97   end || checkout_new_resolver(resolver_type, options)
98 end
pop_connection()
[show source]
   # File lib/httpx/pool.rb
30 def pop_connection
31   @connection_mtx.synchronize do
32     conn = @connections.shift
33     @origin_conds.delete(conn.origin) if conn && (@origin_counters[conn.origin.to_s] -= 1).zero?
34     conn
35   end
36 end
reset_resolvers()
[show source]
   # File lib/httpx/pool.rb
82 def reset_resolvers
83   @resolver_mtx.synchronize { @resolvers.clear }
84 end