doc: fix request.reusedSocket timeout example

Signed-off-by: Efe Karasakal <hi@efe.dev>
PR-URL: https://github.com/nodejs/node/pull/66286
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Tim Perry <pimterry@gmail.com>
This commit is contained in:
Efe Karasakal authored and Richard Lau committed 2026-10-12 14:40:43 +00:00
1 parent 062ece77fc
commit bb816582af
1 file changed
+26 -27
+26 -27
View File
@@ -1191,56 +1191,55 @@ added:
* Type: {boolean} Whether the request is sent through a reused socket.
When sending request through a keep-alive enabled agent, the underlying socket
might be reused. But if server closes connection at unfortunate time, client
may run into a 'ECONNRESET' error.
When sending a request through a keep-alive-enabled agent, the underlying socket
might be reused. If the server closes the connection at the same time that the
agent reuses it, the request may encounter an `ECONNRESET` error.
The following example sends requests near the server's keep-alive timeout to
make this race easier to reproduce. It sets
[`server.keepAliveTimeoutBuffer`][] to `0` so the internal socket timeout
matches [`server.keepAliveTimeout`][].
```mjs
import http from 'node:http';
const agent = new http.Agent({ keepAlive: true });
// Server has a 5 seconds keep-alive timeout by default
http
.createServer((req, res) => {
const server = http.createServer(
{ keepAliveTimeout: 3000, keepAliveTimeoutBuffer: 0 },
(req, res) => {
res.write('hello\n');
res.end();
})
.listen(3000);
},
);
server.listen(3000);
setInterval(() => {
// Adapting a keep-alive agent
http.get('http://localhost:3000', { agent }, (res) => {
res.on('data', (data) => {
// Do nothing
});
res.resume();
});
}, 5000); // Sending request on 5s interval so it's easy to hit idle timeout
}, 3000);
```
```cjs
const http = require('node:http');
const agent = new http.Agent({ keepAlive: true });
// Server has a 5 seconds keep-alive timeout by default
http
.createServer((req, res) => {
const server = http.createServer(
{ keepAliveTimeout: 3000, keepAliveTimeoutBuffer: 0 },
(req, res) => {
res.write('hello\n');
res.end();
})
.listen(3000);
},
);
server.listen(3000);
setInterval(() => {
// Adapting a keep-alive agent
http.get('http://localhost:3000', { agent }, (res) => {
res.on('data', (data) => {
// Do nothing
});
res.resume();
});
}, 5000); // Sending request on 5s interval so it's easy to hit idle timeout
}, 3000);
```
By marking a request whether it reused socket or not, we can do
automatic error retry base on it.
By checking `request.reusedSocket`, an application can retry idempotent requests
that fail because a reused connection was reset.
```mjs
import http from 'node:http';