Class: Async::HTTP::Internet
- Inherits:
-
Object
- Object
- Async::HTTP::Internet
- Defined in:
- lib/async/http/internet.rb,
lib/async/http/internet/instance.rb
Overview
A high-level HTTP client for making requests to any URL, managing a pool of persistent connections keyed by host.
Instance Attribute Summary collapse
-
#clients ⇒ Object
A cache of clients.
Class Method Summary collapse
-
.instance ⇒ Object
The global instance of the internet.
Instance Method Summary collapse
-
#call(verb, url, *arguments, **options, &block) ⇒ Object
Make a request to the internet with the given ‘method` and `url`.
-
#client_for(endpoint) ⇒ Object
Get or create a client for the given endpoint.
-
#close ⇒ Object
Close all cached clients and release their resources.
-
#initialize(**options) ⇒ Internet
constructor
Initialize the internet client.
Constructor Details
#initialize(**options) ⇒ Internet
Initialize the internet client.
20 21 22 23 |
# File 'lib/async/http/internet.rb', line 20 def initialize(**) @clients = Hash.new @options = end |
Instance Attribute Details
#clients ⇒ Object
A cache of clients.
27 28 29 |
# File 'lib/async/http/internet.rb', line 27 def clients @clients end |
Class Method Details
.instance ⇒ Object
The global instance of the internet.
14 15 16 |
# File 'lib/async/http/internet/instance.rb', line 14 def self.instance ::Thread.current.async_http_internet_instance ||= self.new end |
Instance Method Details
#call(verb, url, *arguments, **options, &block) ⇒ Object
Make a request to the internet with the given ‘method` and `url`.
If you provide non-frozen headers, they may be mutated.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/async/http/internet.rb', line 48 def call(verb, url, *arguments, **, &block) endpoint = Endpoint[url] client = self.client_for(endpoint) [:authority] ||= endpoint. [:scheme] ||= endpoint.scheme request = ::Protocol::HTTP::Request[verb, endpoint.path, *arguments, **] response = client.call(request) return response unless block_given? begin yield response ensure response.close end end |
#client_for(endpoint) ⇒ Object
Get or create a client for the given endpoint.
32 33 34 35 36 37 38 |
# File 'lib/async/http/internet.rb', line 32 def client_for(endpoint) key = host_key(endpoint) @clients.fetch(key) do @clients[key] = self.make_client(endpoint) end end |
#close ⇒ Object
Close all cached clients and release their resources.
69 70 71 72 73 74 75 |
# File 'lib/async/http/internet.rb', line 69 def close # The order of operations here is to avoid a race condition between iterating over clients (#close may yield) and creating new clients. clients = @clients.values @clients.clear clients.each(&:close) end |