Class: Async::HTTP::Pool
- Inherits:
-
Object
- Object
- Async::HTTP::Pool
- Defined in:
- lib/async/http/pool.rb
Overview
Pool behaviours
-
Single request per connection (HTTP/1 without keep-alive)
-
Multiple sequential requests per connection (HTTP1 with keep-alive)
-
Multiplex requests per connection (HTTP2)
In general we don’t know the policy until connection is established.
This pool doesn’t impose a maximum number of open resources, but it WILL block if there are no available resources and trying to allocate another one fails.
Resources must respond to #multiplex -> 1 or more. #reusable? -> can be used again.
Instance Attribute Summary collapse
-
#resources ⇒ Object
readonly
Returns the value of attribute resources.
Instance Method Summary collapse
- #acquire ⇒ Object
- #close ⇒ Object
- #empty? ⇒ Boolean
-
#initialize(limit = nil, &block) ⇒ Pool
constructor
A new instance of Pool.
-
#release(resource) ⇒ Object
Make the resource resources and let waiting tasks know that there is something resources.
- #to_s ⇒ Object
Constructor Details
#initialize(limit = nil, &block) ⇒ Pool
Returns a new instance of Pool.
40 41 42 43 44 45 46 47 48 |
# File 'lib/async/http/pool.rb', line 40 def initialize(limit = nil, &block) @resources = {} # resource => count @available = Async::Notification.new @limit = limit @active = 0 @constructor = block end |
Instance Attribute Details
#resources ⇒ Object (readonly)
Returns the value of attribute resources.
50 51 52 |
# File 'lib/async/http/pool.rb', line 50 def resources @resources end |
Instance Method Details
#acquire ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/async/http/pool.rb', line 56 def acquire resource = wait_for_resource return resource unless block_given? begin yield resource ensure release(resource) end end |
#close ⇒ Object
78 79 80 81 82 83 |
# File 'lib/async/http/pool.rb', line 78 def close @resources.each_key(&:close) @resources.clear @active = 0 end |
#empty? ⇒ Boolean
52 53 54 |
# File 'lib/async/http/pool.rb', line 52 def empty? @resources.empty? end |
#release(resource) ⇒ Object
Make the resource resources and let waiting tasks know that there is something resources.
69 70 71 72 73 74 75 76 |
# File 'lib/async/http/pool.rb', line 69 def release(resource) # A resource that is not good should also not be reusable. if resource.reusable? reuse(resource) else retire(resource) end end |
#to_s ⇒ Object
85 86 87 |
# File 'lib/async/http/pool.rb', line 85 def to_s "\#<#{self.class} resources=#{availability_string} limit=#{@limit}>" end |