Skip to content

Commit fe7ad02

Browse files
authored
clarify Simultaneous open connections for workers (#23661)
1 parent 76e0ddb commit fe7ad02

File tree

1 file changed

+34
-7
lines changed

1 file changed

+34
-7
lines changed

src/content/docs/workers/platform/limits.mdx

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -233,28 +233,55 @@ Once an invocation has six connections open, it can still attempt to open additi
233233

234234
- These attempts are put in a pending queue — the connections will not be initiated until one of the currently open connections has closed.
235235
- Earlier connections can delay later ones, if a Worker tries to make many simultaneous subrequests, its later subrequests may appear to take longer to start.
236+
- Earlier connections that are stalled<sup>1</sup> might get closed with a `Response closed due to connection limit` exception.
236237

237238
If you have cases in your application that use `fetch()` but that do not require consuming the response body, you can avoid the unread response body from consuming a concurrent connection by using `response.body.cancel()`.
238239

239240
For example, if you want to check whether the HTTP response code is successful (2xx) before consuming the body, you should explicitly cancel the pending response body:
240241

241242
```ts
242-
let resp = await fetch(url);
243+
const response = await fetch(url);
243244

244245
// Only read the response body for successful responses
245-
if (resp.statusCode <= 299) {
246-
// Call resp.json(), resp.text() or otherwise process the body
246+
if (response.statusCode <= 299) {
247+
// Call response.json(), response.text() or otherwise process the body
247248
} else {
248249
// Explicitly cancel it
249-
resp.body.cancel();
250+
response.body.cancel();
250251
}
251252
```
252253

253254
This will free up an open connection.
254255

255-
If the system detects that a Worker is deadlocked on open connections — for example, if the Worker has pending connection attempts but has no in-progress reads or writes on the connections that it already has open — then the least-recently-used open connection will be canceled to unblock the Worker.
256+
If the system detects that a Worker is deadlocked on stalled connections<sup>1</sup> — for example, if the Worker has pending connection attempts but has no in-progress reads or writes on the connections that it already has open — then the least-recently-used open connection will be canceled to unblock the Worker.
256257

257-
If the Worker later attempts to use a canceled connection, an exception will be thrown. These exceptions should rarely occur in practice, though, since it is uncommon for a Worker to open a connection that it does not have an immediate use for.
258+
If the Worker later attempts to use a canceled connection, a `Response closed due to connection limit` exception will be thrown. These exceptions should rarely occur in practice, though, since it is uncommon for a Worker to open a connection that it does not have an immediate use for.
259+
260+
<sup>1</sup>A connections is considered stalled when it is not not being
261+
actively read from or written to, for example:
262+
263+
```ts
264+
// Within a for-of loop
265+
const response = await fetch("https://example.org");
266+
for await (const chunk of response.body) {
267+
// While this code block is executing, there are no pending
268+
// reads on the response.body. Accordingly, the system may view
269+
// the stream as not being active within this block.
270+
}
271+
272+
// Using body.getReader()
273+
const response = await fetch("https://example.org");
274+
const reader = response.body.getReader();
275+
let chunk = await reader.read();
276+
await processChunk(chunk);
277+
chunk = await reader.read();
278+
await processChunk(chunk);
279+
280+
async function processChunk(chunk) {
281+
// The stream is considered inactive as there is no pending reads
282+
// on response.body. It may then get cancelled.
283+
}
284+
```
258285

259286
:::note
260287

@@ -294,7 +321,7 @@ To reduce the upload size of a Worker, consider some of the following strategies
294321

295322
- Removing unnecessary dependencies and packages
296323
- Storing configuration files, static assets, and binary data using [Workers KV](/kv/), [R2](/r2/), [D1](/d1/), or [Workers Static Assets](/workers/static-assets/) instead of bundling them within your Worker code.
297-
- Splitng functionality across multiple Workers and connecting them using [Service bindings](/workers/runtime-apis/bindings/service-bindings/).
324+
- Splitting functionality across multiple Workers and connecting them using [Service bindings](/workers/runtime-apis/bindings/service-bindings/).
298325

299326
---
300327

0 commit comments

Comments
 (0)