Class: Async::HTTP::Client
- Inherits:
-
Object
- Object
- Async::HTTP::Client
- Includes:
- Methods
- 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
A new instance of Client.
Constructor Details
#initialize(endpoint, protocol = endpoint.protocol, scheme = endpoint.scheme, authority = endpoint.authority, retries: 3, connection_limit: nil) ⇒ Client
Returns a new instance of Client.
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/async/http/client.rb', line 31 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.
49 50 51 |
# File 'lib/async/http/client.rb', line 49 def @authority end |
#endpoint ⇒ Object (readonly)
Returns the value of attribute endpoint.
42 43 44 |
# File 'lib/async/http/client.rb', line 42 def endpoint @endpoint end |
#pool ⇒ Object (readonly)
Returns the value of attribute pool.
46 47 48 |
# File 'lib/async/http/client.rb', line 46 def pool @pool end |
#protocol ⇒ Object (readonly)
Returns the value of attribute protocol.
43 44 45 |
# File 'lib/async/http/client.rb', line 43 def protocol @protocol end |
#retries ⇒ Object (readonly)
Returns the value of attribute retries.
45 46 47 |
# File 'lib/async/http/client.rb', line 45 def retries @retries end |
#scheme ⇒ Object (readonly)
Returns the value of attribute scheme.
48 49 50 |
# File 'lib/async/http/client.rb', line 48 def scheme @scheme end |
Class Method Details
.open(*args, &block) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/async/http/client.rb', line 51 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
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 101 102 103 104 105 106 107 108 109 |
# File 'lib/async/http/client.rb', line 69 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 = connection.call(request) # The connection won't be released until the body is completely read/released. 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 attempt += 1 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
63 64 65 |
# File 'lib/async/http/client.rb', line 63 def close @pool.close end |