Class: Async::HTTP::Client
- Inherits:
-
Object
- Object
- Async::HTTP::Client
- Includes:
- Verbs
- 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.
-
#protocol ⇒ Object
readonly
Returns the value of attribute protocol.
Class Method Summary collapse
Instance Method Summary collapse
- #call(request) ⇒ Object
- #close ⇒ Object
-
#initialize(endpoint, protocol = nil, authority = nil, retries: 3, **options) ⇒ Client
constructor
A new instance of Client.
Constructor Details
#initialize(endpoint, protocol = nil, authority = nil, retries: 3, **options) ⇒ Client
Returns a new instance of Client.
30 31 32 33 34 35 36 37 38 |
# File 'lib/async/http/client.rb', line 30 def initialize(endpoint, protocol = nil, = nil, retries: 3, **) @endpoint = endpoint @protocol = protocol || endpoint.protocol @authority = || endpoint.hostname @retries = retries @connections = connect(**) end |
Instance Attribute Details
#authority ⇒ Object (readonly)
Returns the value of attribute authority.
42 43 44 |
# File 'lib/async/http/client.rb', line 42 def @authority end |
#endpoint ⇒ Object (readonly)
Returns the value of attribute endpoint.
40 41 42 |
# File 'lib/async/http/client.rb', line 40 def endpoint @endpoint end |
#protocol ⇒ Object (readonly)
Returns the value of attribute protocol.
41 42 43 |
# File 'lib/async/http/client.rb', line 41 def protocol @protocol end |
Class Method Details
.open(*args, &block) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/async/http/client.rb', line 44 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
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/async/http/client.rb', line 62 def call(request) request. ||= @authority 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 connections, it's possible these connections 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 = @connections.acquire response = connection.call(request) # The connection won't be released until the body is completely read/released. Body::Streamable.wrap(response) do @connections.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. @connections.release(connection) attempt += 1 if attempt < @retries retry else raise end rescue @connections.release(connection) if request.idempotent? and attempt < @retries retry else raise end end end |
#close ⇒ Object
56 57 58 |
# File 'lib/async/http/client.rb', line 56 def close @connections.close end |