Commit 24a10f67b65 for nodejs
commit 24a10f67b657d44f426d0ee914c1dbc786e843c5
Author: Efe Karasakal <hi@efe.dev>
Date: Fri Sep 25 16:30:09 2026 +0200
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>
diff --git a/doc/api/http.md b/doc/api/http.md
index fccc308356f..3ad3c2d49d4 100644
--- a/doc/api/http.md
+++ b/doc/api/http.md
@@ -1193,56 +1193,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';