Skip to content

Commit 9fd1f21

Browse files
committed
Bump minor version.
1 parent 0085d9d commit 9fd1f21

8 files changed

Lines changed: 604 additions & 125 deletions

File tree

‎context/choosing-a-client.md‎

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
# Choosing a Client
2+
3+
This guide explains how to choose between ruby:`Async::HTTP::Internet`, ruby:`Async::HTTP::Client`, and higher-level interfaces for libraries.
4+
5+
ruby:`Async::HTTP::Internet` and ruby:`Async::HTTP::Client` use the same request and response model. The important differences are how destinations are selected, where connection settings are applied, and who owns the client life cycle.
6+
7+
## Quick Decision
8+
9+
| Situation | Interface | Why |
10+
| --- | --- | --- |
11+
| Requests may target different origins and the defaults are suitable. | Shared ruby:`Async::HTTP::Internet` | Selects and reuses a client for each origin automatically. |
12+
| Requests may target different origins, but need common client options or explicit ownership. | Explicit ruby:`Async::HTTP::Internet` | Applies the same options to each managed client and can be injected or closed early. |
13+
| Requests repeatedly target one configured origin. | ruby:`Async::HTTP::Client` | Exposes the endpoint, protocol, retry, and connection-pool configuration directly. |
14+
| A library wraps one HTTP service directly. | Injected ruby:`Async::HTTP::Client` or `Protocol::HTTP` middleware | Leaves transport configuration, ownership, and testing under application control. |
15+
| A library models an HTTP API as resources and representations. | [`async-rest`](https://socketry.github.io/async-rest/guides/getting-started/) | Provides higher-level API modeling over an injectable `Protocol::HTTP` delegate. |
16+
| A library uses Faraday as its HTTP abstraction. | [`async-http-faraday`](https://socketry.github.io/async-http-faraday/guides/getting-started/) | Lets the application retain the Faraday interface while using `Async::HTTP` as the transport. |
17+
18+
Application code can start with the shared `Internet` interface unless it has a specific ownership or configuration requirement. Library code should accept an explicit HTTP dependency.
19+
20+
## Shared Internet for General Requests
21+
22+
The shared `Internet` interface is the simplest choice for requests to arbitrary URLs. It maintains one client for each origin and reuses persistent connections:
23+
24+
~~~ ruby
25+
require "async/http/internet/instance"
26+
27+
urls = [
28+
"https://www.ruby-lang.org/en/",
29+
"https://example.com/",
30+
]
31+
32+
Sync do
33+
urls.each do |url|
34+
Async::HTTP::Internet.get(url) do |response|
35+
puts "#{url}: #{response.status}"
36+
end
37+
end
38+
end
39+
~~~
40+
41+
The class-level interface uses a thread-local `Internet` instance. Its connection pools are bound to the event loop and close when that event loop exits. The response block closes each response after it is processed.
42+
43+
Use the shared interface when:
44+
45+
- The application requests URLs from multiple or dynamically selected origins.
46+
- Default retry and connection-pool settings are suitable.
47+
- The client does not need to be injected as an application dependency.
48+
49+
## Explicit Internet for Shared Configuration
50+
51+
An explicit `Internet` provides the same per-origin client selection while making ownership and client options visible. Options are passed to every client it creates; for example, `limit` applies independently to the pool for each origin:
52+
53+
~~~ ruby
54+
require "async/http/internet"
55+
56+
Sync do
57+
internet = Async::HTTP::Internet.new(retries: 1, limit: 4)
58+
59+
begin
60+
internet.get("https://www.ruby-lang.org/en/") do |response|
61+
puts response.status
62+
end
63+
ensure
64+
internet.close
65+
end
66+
end
67+
~~~
68+
69+
Use an explicit `Internet` when:
70+
71+
- Several origins should share the same retry or pool settings.
72+
- The client should be injected into another object or replaced during testing.
73+
- Connections should be released before the event loop exits.
74+
75+
## Client for One Endpoint
76+
77+
A `Client` targets one ruby:`Async::HTTP::Endpoint`. Use it when a remote service is a stable part of the application architecture and needs its own protocol, TLS, retry, or pool configuration:
78+
79+
~~~ ruby
80+
require "async/http"
81+
82+
endpoint = Async::HTTP::Endpoint.parse("https://httpbin.org")
83+
84+
Sync do
85+
Async::HTTP::Client.open(endpoint, retries: 1, limit: 4) do |client|
86+
response = client.get("/status/200")
87+
88+
begin
89+
puts response.status
90+
ensure
91+
response.close
92+
end
93+
end
94+
end
95+
~~~
96+
97+
Client convenience methods accept a path rather than a complete URL. They return a response that the caller must close. `Client.open` closes the client and its connection pool when the block exits.
98+
99+
Reuse a client for repeated requests rather than creating one per request; otherwise the application cannot benefit from persistent connections.
100+
101+
## Building a Library That Makes HTTP Requests
102+
103+
A library should generally accept its HTTP client as an explicit dependency. This lets the application configure connection limits, retries, proxies, instrumentation, and test doubles without the library creating hidden global state:
104+
105+
~~~ ruby
106+
require "async/http"
107+
108+
class StatusService
109+
def initialize(client)
110+
@client = client
111+
end
112+
113+
def healthy?
114+
response = @client.get("/status/200")
115+
response.status == 200
116+
ensure
117+
response&.close
118+
end
119+
end
120+
121+
endpoint = Async::HTTP::Endpoint.parse("https://httpbin.org")
122+
123+
Sync do
124+
Async::HTTP::Client.open(endpoint) do |client|
125+
puts StatusService.new(client).healthy?
126+
end
127+
end
128+
~~~
129+
130+
The library does not close an injected client because the caller owns it and may share it with other components. If the library also provides an `open` convenience method that constructs a client, that method should close the client it creates when its block exits.
131+
132+
Define the accepted interface precisely. A ruby:`Async::HTTP::Client` is bound to one endpoint and its convenience methods accept relative paths, while ruby:`Async::HTTP::Internet` selects an endpoint from a complete URL. They should not be treated as interchangeable merely because both provide methods such as `get`. If the library constructs ruby:`Protocol::HTTP::Request` objects and only calls `call`, it can accept a `Protocol::HTTP` middleware delegate instead of requiring a concrete client.
133+
134+
## Modeling Resources with async-rest
135+
136+
Use [`async-rest`](https://socketry.github.io/async-rest/guides/getting-started/) when a library benefits from modeling a remote HTTP API as resources and representations rather than exposing request operations directly.
137+
138+
ruby:`Async::REST::Resource` accepts a `Protocol::HTTP` middleware delegate, so the application can supply and configure the transport. Its `open` method provides the complementary convenience interface: it creates a ruby:`Async::HTTP::Client`, yields the resource, and closes the client when the block exits.
139+
140+
## Supporting Faraday with async-http-faraday
141+
142+
Use [`async-http-faraday`](https://socketry.github.io/async-http-faraday/guides/getting-started/) when a library uses Faraday as its public HTTP abstraction or needs compatibility with the Faraday ecosystem. A new Async-native library can usually accept a ruby:`Async::HTTP::Client` or `Protocol::HTTP` middleware delegate directly.
143+
144+
If a library uses Faraday, accept a configured `Faraday::Connection` rather than changing `Faraday.default_adapter` globally. The application can then select the `Async::HTTP` adapter for that connection:
145+
146+
~~~ ruby
147+
require "async/http/faraday"
148+
149+
class StatusService
150+
def initialize(connection)
151+
@connection = connection
152+
end
153+
154+
def healthy?
155+
@connection.get("/status/200").success?
156+
end
157+
end
158+
159+
connection = Faraday.new("https://httpbin.org") do |builder|
160+
builder.adapter :async_http
161+
end
162+
163+
puts StatusService.new(connection).healthy?
164+
~~~

‎context/concurrent-requests.md‎

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
# Concurrent Requests and Connection Pooling
2+
3+
This guide explains how to run HTTP requests concurrently while keeping request fan-out, connection usage, and resource life cycles bounded.
4+
5+
Concurrent requests allow independent network operations to overlap. `Async::HTTP` combines this task concurrency with persistent connection pools, but the number of request tasks and the number of connections are separate controls.
6+
7+
## Running Independent Requests Concurrently
8+
9+
When several requests do not depend on each other, start one child task for each request and then wait for their results:
10+
11+
~~~ ruby
12+
require "async/http/internet/instance"
13+
require "json"
14+
15+
names = ["async", "async-http", "falcon"]
16+
17+
versions = Sync do |task|
18+
tasks = names.map do |name|
19+
task.async do
20+
url = "https://rubygems.org/api/v1/gems/#{name}.json"
21+
22+
Async::HTTP::Internet.get(url) do |response|
23+
raise "Could not fetch #{name}: #{response.status}" unless response.success?
24+
25+
JSON.parse(response.read).fetch("version")
26+
end
27+
end
28+
end
29+
30+
tasks.map(&:wait)
31+
end
32+
33+
names.zip(versions) do |name, version|
34+
puts "#{name}: #{version}"
35+
end
36+
~~~
37+
38+
Each request can make progress while the others are waiting for network I/O. ruby:`Async::Task#wait` returns the task result and re-raises any failure, so request errors are not silently discarded. Results are collected in the original order even if requests finish in a different order.
39+
40+
The response block closes each response after its body is processed. General task creation, cancellation, and failure propagation are covered by the [`async` Tasks guide](https://socketry.github.io/async/guides/tasks/).
41+
42+
## Limiting Request Concurrency
43+
44+
Creating one task per item is appropriate for a small, fixed collection. A large or externally supplied collection should have an explicit concurrency limit so it cannot overwhelm the remote service or retain an unbounded number of pending operations.
45+
46+
Use ruby:`Async::Semaphore` to set a fixed request limit and ruby:`Async::Barrier` to wait for all the work:
47+
48+
~~~ ruby
49+
require "async"
50+
require "async/semaphore"
51+
require "async/http/internet"
52+
53+
names = ["async", "async-http", "falcon", "protocol-http", "io-event", "console"]
54+
55+
Sync do
56+
internet = Async::HTTP::Internet.new(limit: 2)
57+
58+
begin
59+
Barrier(parent: nil) do |barrier|
60+
requests = Async::Semaphore.new(4, parent: barrier)
61+
62+
names.each do |name|
63+
requests.async do
64+
url = "https://rubygems.org/api/v1/gems/#{name}.json"
65+
66+
internet.get(url) do |response|
67+
raise "Could not fetch #{name}: #{response.status}" unless response.success?
68+
69+
puts "#{name}: #{response.status}"
70+
end
71+
end
72+
end
73+
end
74+
ensure
75+
internet.close
76+
end
77+
end
78+
~~~
79+
80+
The semaphore allows at most four request operations to run at once. The barrier tracks those tasks, waits for them to finish, propagates failures, and cancels unfinished work if the block exits early. `parent: nil` disables the barrier's load-based scheduling because the semaphore already provides an explicit limit.
81+
82+
See the [`async` Best Practices guide](https://socketry.github.io/async/guides/best-practices/) for general guidance on barriers, semaphores, and large workloads.
83+
84+
## Request Limits and Connection Limits
85+
86+
Request concurrency and pool capacity solve different problems:
87+
88+
| Control | What It Limits | Scope |
89+
| --- | --- | --- |
90+
| ruby:`Async::Semaphore` | Concurrent request operations and their application work. | The tasks started through that semaphore. |
91+
| `limit` passed to ruby:`Async::HTTP::Client` | Connections held by one client pool. | One configured endpoint. |
92+
| `limit` passed to ruby:`Async::HTTP::Internet` | Connections held by each client it creates. | Applied independently to every origin. |
93+
94+
The client creates connections lazily and reuses viable persistent connections. When its pool reaches `limit`, a request waits until an existing connection has capacity or is released.
95+
96+
For HTTP/1, a connection normally processes one request at a time, so connection capacity also constrains active requests. HTTP/2 can multiplex several request streams over one connection, so a small connection pool may support much higher request concurrency. Use a semaphore when the application needs a protocol-independent request limit; use `limit` to control connection resources.
97+
98+
## Releasing Connections for Reuse
99+
100+
A response retains pool capacity until its body is finished or closed. Always close responses promptly, especially before starting more requests that use the same limited pool:
101+
102+
~~~ ruby
103+
require "async/http"
104+
105+
endpoint = Async::HTTP::Endpoint.parse("https://rubygems.org")
106+
107+
Sync do
108+
Async::HTTP::Client.open(endpoint, limit: 1) do |client|
109+
response = client.get("/api/v1/gems/async-http.json")
110+
111+
begin
112+
raise "Download failed: #{response.status}" unless response.success?
113+
114+
bytes = 0
115+
116+
response.each do |chunk|
117+
bytes += chunk.bytesize
118+
end
119+
120+
puts "Downloaded #{bytes} bytes."
121+
ensure
122+
response.close
123+
end
124+
end
125+
end
126+
~~~
127+
128+
The block form of ruby:`Async::HTTP::Internet` performs this cleanup automatically. When using ruby:`Async::HTTP::Client`, close the response explicitly with an `ensure` block. Holding an unread response while waiting for another request can exhaust an HTTP/1 pool and make the second request wait indefinitely.
129+
130+
## Pool Life Cycle
131+
132+
Connection pools are bound to the event loop in which they are used. Their internal maintenance task is transient, so it does not keep the event loop alive and closes the pool when the event loop exits.
133+
134+
Use the shared ruby:`Async::HTTP::Internet` interface when connections can remain open until the event loop exits. Use ruby:`Async::HTTP::Client.open`, or explicitly close an `Internet` or `Client`, when connections should be released earlier. See [Choosing a Client](../choosing-a-client/) for the ownership trade-offs.

0 commit comments

Comments
 (0)