Class: Async::HTTP::Client
- Inherits:
-
Protocol::HTTP::Methods
- Object
- Protocol::HTTP::Methods
- Async::HTTP::Client
- Includes:
- Proxy::Client
- Defined in:
- lib/async/http/client.rb
Instance Attribute Summary collapse
-
#authority ⇒ Object
readonly
Returns the value of attribute authority.
-
#endpoint ⇒ Object
readonly
Returns the value of attribute endpoint.
-
#pool ⇒ Object
readonly
Returns the value of attribute pool.
-
#protocol ⇒ Object
readonly
Returns the value of attribute protocol.
-
#retries ⇒ Object
readonly
Returns the value of attribute retries.
-
#scheme ⇒ Object
readonly
Returns the value of attribute scheme.
Class Method Summary collapse
Instance Method Summary collapse
- #call(request) ⇒ Object
- #close ⇒ Object
-
#initialize(endpoint, protocol = endpoint.protocol, scheme = endpoint.scheme, authority = endpoint.authority, retries: 3, connection_limit: nil) ⇒ Client
constructor
Provides a robust interface to a server.
- #secure? ⇒ Boolean
Methods included from Proxy::Client
#proxied_client, #proxied_endpoint, #proxy
Constructor Details
#initialize(endpoint, protocol = endpoint.protocol, scheme = endpoint.scheme, authority = endpoint.authority, retries: 3, connection_limit: nil) ⇒ Client
Provides a robust interface to a server.
-
If there are no connections, it will create one.
-
If there are already connections, it will reuse it.
-
If a request fails, it will retry it up to N times if it was idempotent.
The client object will never become unusable. It internally manages persistent connections (or non-persistent connections if that’s required).
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/async/http/client.rb', line 43 def initialize(endpoint, protocol = endpoint.protocol, scheme = endpoint.scheme, = endpoint., retries: 3, connection_limit: nil) @endpoint = endpoint @protocol = protocol @retries = retries @pool = make_pool(connection_limit) @scheme = scheme @authority = end |
Instance Attribute Details
#authority ⇒ Object (readonly)
Returns the value of attribute authority.
61 62 63 |
# File 'lib/async/http/client.rb', line 61 def @authority end |
#endpoint ⇒ Object (readonly)
Returns the value of attribute endpoint.
54 55 56 |
# File 'lib/async/http/client.rb', line 54 def endpoint @endpoint end |
#pool ⇒ Object (readonly)
Returns the value of attribute pool.
58 59 60 |
# File 'lib/async/http/client.rb', line 58 def pool @pool end |
#protocol ⇒ Object (readonly)
Returns the value of attribute protocol.
55 56 57 |
# File 'lib/async/http/client.rb', line 55 def protocol @protocol end |
#retries ⇒ Object (readonly)
Returns the value of attribute retries.
57 58 59 |
# File 'lib/async/http/client.rb', line 57 def retries @retries end |
#scheme ⇒ Object (readonly)
Returns the value of attribute scheme.
60 61 62 |
# File 'lib/async/http/client.rb', line 60 def scheme @scheme end |
Class Method Details
.open(*args, &block) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/async/http/client.rb', line 67 def self.open(*args, &block) client = self.new(*args) return client unless block_given? begin yield client ensure client.close end end |
Instance Method Details
#call(request) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/async/http/client.rb', line 88 def call(request) request.scheme ||= self.scheme request. ||= self. attempt = 0 # We may retry the request if it is possible to do so. https://tools.ietf.org/html/draft-nottingham-httpbis-retry-01 is a good guide for how retrying requests should work. begin attempt += 1 # As we cache pool, it's possible these pool go bad (e.g. closed by remote host). In this case, we need to try again. It's up to the caller to impose a timeout on this. If this is the last attempt, we force a new connection. connection = @pool.acquire response = request.call(connection) # The connection won't be released until the body is completely read/released. ::Protocol::HTTP::Body::Streamable.wrap(response) do @pool.release(connection) end return response rescue Protocol::RequestFailed # This is a specific case where the entire request wasn't sent before a failure occurred. So, we can even resend non-idempotent requests. @pool.release(connection) if connection if attempt < @retries retry else raise end rescue @pool.release(connection) if connection if request.idempotent? and attempt < @retries retry else raise end end end |
#close ⇒ Object
79 80 81 82 83 84 85 86 |
# File 'lib/async/http/client.rb', line 79 def close while @pool.busy? Async.logger.warn(self) {"Waiting for pool to drain: #{@pool}"} @pool.wait end @pool.close end |
#secure? ⇒ Boolean
63 64 65 |
# File 'lib/async/http/client.rb', line 63 def secure? @endpoint.secure? end |