request.reusedSocket
添加于:v13.0.0、v12.16.0
- <boolean> 请求是否通过重用的套接字发送。
通过启用keep-alive 的代理发送请求时,可能会重用底层套接字。但是如果服务器在不幸的时候关闭了连接,客户端可能会遇到 'ECONNRESET' 错误。
const http = require('node: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
通过标记请求是否重用套接字,我们可以根据它进行自动错误重试。
const http = require('node: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();
相关用法
- Node.js http.ClientRequest request.removeHeader(name)用法及代码示例
- Node.js http2.Http2ServerRequest request.rawHeaders用法及代码示例
- Node.js http2.Http2ServerRequest request.url用法及代码示例
- Node.js request.socket用法及代码示例
- Node.js http.ClientRequest request.getHeaders()用法及代码示例
- Node.js http2.Http2ServerRequest request.headers用法及代码示例
- Node.js http.ClientRequest request.setHeader(name, value)用法及代码示例
- Node.js request.writableEnded用法及代码示例
- Node.js http.ClientRequest request.getHeaderNames()用法及代码示例
- Node.js http.ClientRequest request.getHeader(name)用法及代码示例
- Node.js http.ClientRequest request.hasHeader(name)用法及代码示例
- Node.js http.ClientRequest request.getRawHeaderNames()用法及代码示例
- Node.js http.ClientRequest request.socket用法及代码示例
- Node.js require.cache用法及代码示例
- Node.js require.main用法及代码示例
- Node.js require.extensions用法及代码示例
- Node.js require(id)用法及代码示例
- Node.js http.ServerResponse response.statusCode用法及代码示例
- Node.js readStream.isRaw用法及代码示例
- Node.js response.writeContinue()用法及代码示例
- Node.js http2.Http2ServerResponse response.removeHeader(name)用法及代码示例
- Node.js response.removeHeader()用法及代码示例
- Node.js http.ServerResponse response.getHeaderNames()用法及代码示例
- Node.js http2.Http2ServerResponse response.hasHeader(name)用法及代码示例
- Node.js http.ServerResponse response.removeHeader(name)用法及代码示例
注:本文由纯净天空筛选整理自nodejs.org大神的英文原创作品 request.reusedSocket。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。