Skip to content

Commit bb81658

Browse files
efekrsklrichardlau
authored andcommitted
doc: fix request.reusedSocket timeout example
Signed-off-by: Efe Karasakal <[email protected]> PR-URL: #66286 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Tim Perry <[email protected]>
1 parent 062ece7 commit bb81658

1 file changed

Lines changed: 26 additions & 27 deletions

File tree

‎doc/api/http.md‎

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1191,56 +1191,55 @@ added:
11911191

11921192
* Type: {boolean} Whether the request is sent through a reused socket.
11931193

1194-
When sending request through a keep-alive enabled agent, the underlying socket
1195-
might be reused. But if server closes connection at unfortunate time, client
1196-
may run into a 'ECONNRESET' error.
1194+
When sending a request through a keep-alive-enabled agent, the underlying socket
1195+
might be reused. If the server closes the connection at the same time that the
1196+
agent reuses it, the request may encounter an `ECONNRESET` error.
1197+
1198+
The following example sends requests near the server's keep-alive timeout to
1199+
make this race easier to reproduce. It sets
1200+
[`server.keepAliveTimeoutBuffer`][] to `0` so the internal socket timeout
1201+
matches [`server.keepAliveTimeout`][].
11971202

11981203
```mjs
11991204
import http from 'node:http';
12001205
const agent = new http.Agent({ keepAlive: true });
1201-
1202-
// Server has a 5 seconds keep-alive timeout by default
1203-
http
1204-
.createServer((req, res) => {
1206+
const server = http.createServer(
1207+
{ keepAliveTimeout: 3000, keepAliveTimeoutBuffer: 0 },
1208+
(req, res) => {
12051209
res.write('hello\n');
12061210
res.end();
1207-
})
1208-
.listen(3000);
1211+
},
1212+
);
1213+
server.listen(3000);
12091214

12101215
setInterval(() => {
1211-
// Adapting a keep-alive agent
12121216
http.get('http://localhost:3000', { agent }, (res) => {
1213-
res.on('data', (data) => {
1214-
// Do nothing
1215-
});
1217+
res.resume();
12161218
});
1217-
}, 5000); // Sending request on 5s interval so it's easy to hit idle timeout
1219+
}, 3000);
12181220
```
12191221

12201222
```cjs
12211223
const http = require('node:http');
12221224
const agent = new http.Agent({ keepAlive: true });
1223-
1224-
// Server has a 5 seconds keep-alive timeout by default
1225-
http
1226-
.createServer((req, res) => {
1225+
const server = http.createServer(
1226+
{ keepAliveTimeout: 3000, keepAliveTimeoutBuffer: 0 },
1227+
(req, res) => {
12271228
res.write('hello\n');
12281229
res.end();
1229-
})
1230-
.listen(3000);
1230+
},
1231+
);
1232+
server.listen(3000);
12311233

12321234
setInterval(() => {
1233-
// Adapting a keep-alive agent
12341235
http.get('http://localhost:3000', { agent }, (res) => {
1235-
res.on('data', (data) => {
1236-
// Do nothing
1237-
});
1236+
res.resume();
12381237
});
1239-
}, 5000); // Sending request on 5s interval so it's easy to hit idle timeout
1238+
}, 3000);
12401239
```
12411240

1242-
By marking a request whether it reused socket or not, we can do
1243-
automatic error retry base on it.
1241+
By checking `request.reusedSocket`, an application can retry idempotent requests
1242+
that fail because a reused connection was reset.
12441243

12451244
```mjs
12461245
import http from 'node:http';

0 commit comments

Comments
 (0)