• {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.

    1. const http = require('http');
    2. // Server has a 5 seconds keep-alive timeout by default
    3. http
    4. .createServer((req, res) => {
    5. res.write('hello\n');
    6. res.end();
    7. })
    8. .listen(3000);
    9. setInterval(() => {
    10. // Adapting a keep-alive agent
    11. http.get('http://localhost:3000', { agent }, (res) => {
    12. res.on('data', (data) => {
    13. // Do nothing
    14. });
    15. });
    16. }, 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.

    1. const http = require('http');
    2. const agent = new http.Agent({ keepAlive: true });
    3. function retriableRequest() {
    4. const req = http
    5. .get('http://localhost:3000', { agent }, (res) => {
    6. // ...
    7. })
    8. .on('error', (err) => {
    9. // Check if retry is needed
    10. if (req.reusedSocket && err.code === 'ECONNRESET') {
    11. retriableRequest();
    12. }
    13. });
    14. }
    15. retriableRequest();