-
-
Notifications
You must be signed in to change notification settings - Fork 33.3k
http: add reusedSocket property on client request #29715
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -676,6 +676,62 @@ Removes a header that's already defined into headers object. | |
request.removeHeader('Content-Type'); | ||
``` | ||
|
||
### request.reusedSocket | ||
|
||
<!-- YAML | ||
added: REPLACEME | ||
--> | ||
|
||
* {boolean} Whether the request is send 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. | ||
|
||
```js | ||
const http = require('http'); | ||
|
||
// Server has a 5 seconds keep-alive timeout by default | ||
http | ||
.createServer((req, res) => { | ||
res.write('hello\n'); | ||
res.end(); | ||
}) | ||
.listen(3000); | ||
|
||
setInterval(() => { | ||
// Adapting a keep-alive agent | ||
http.get('http://localhost:3000', { agent }, (res) => { | ||
res.on('data', (data) => { | ||
// Do nothing | ||
}); | ||
}); | ||
}, 5000); // Sending request on 5s interval so it's easy to hit idle timeout | ||
``` | ||
|
||
By marking a request whether it reused socket or not, we can do | ||
automatic error retry base on it. | ||
|
||
```js | ||
const http = require('http'); | ||
const agent = new http.Agent({ keepAlive: true }); | ||
|
||
function retriableRequest() { | ||
const req = http | ||
.get('http://localhost:3000', { agent }, (res) => { | ||
// ... | ||
}) | ||
.on('error', (err) => { | ||
// Check if retry is needed | ||
if (req.reusedSocket && err.code === 'ECONNRESET') { | ||
|
||
retriableRequest(); | ||
} | ||
}); | ||
} | ||
|
||
retriableRequest(); | ||
``` | ||
|
||
### request.setHeader(name, value) | ||
<!-- YAML | ||
added: v1.6.0 | ||
|
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should probably include a note about idempotent methods.