温馨提示:本站仅提供公开网络链接索引服务,不存储、不篡改任何第三方内容,所有内容版权归原作者所有
AI智能索引来源:http://www.bun.com/reference/node/http/ClientRequest/reusedSocket
点击访问原文链接

ClientRequest.reusedSocket property | Node.js http module | Bun

ClientRequest.reusedSocket property | Node.js http module | Bun { 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 import http from '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(); ```"/> { 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 import http from '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(); ```"/>BuildDocsReferenceGuidesBlogDiscord/node:http/ClientRequest/reusedSocketPreusedSocket

Search the reference...

/

BuildDocsReferenceGuidesBlogDiscord/node:http/ClientRequest/reusedSocketPreusedSocket

property

http.ClientRequest.reusedSocket { 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 import http from '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(); ```" data-algolia-static="false" data-algolia-merged="false" data-type="Property">reusedSocket: booleanWhen 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.

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) => { 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.

import http from '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();

Resources

ReferenceDocsGuidesDiscordMerch StoreGitHubBlog 

Toolkit

RuntimePackage managerTest runnerBundlerPackage runner

Project

Bun 1.0Bun 1.1Bun 1.2Bun 1.3RoadmapContributingLicenseBaked with ❤️ in San Francisco

We're hiring →

智能索引记录