lib/agent.js
function onTimeout() {
    const listenerCount = socket.listeners('timeout').length;
    debug('%s(requests: %s, finished: %s) timeout after %sms, listeners %s',
      socket[SOCKET_NAME], socket[SOCKET_REQUEST_COUNT], socket[SOCKET_REQUEST_FINISHED_COUNT],
      getSocketTimeout(socket), listenerCount);
    agent.timeoutSocketCount++;
    const name = agent.getName(options);
    if (agent.freeSockets[name] && agent.freeSockets[name].indexOf(socket) !== -1) {
      // free socket timeout, destroy quietly
      socket.destroy();
      // Remove it from freeSockets list immediately to prevent new requests
      // from being sent through this socket.
      agent.removeSocket(socket, options);
      debug('%s is free, destroy quietly', socket[SOCKET_NAME]);
    } else {
      // if there is no any request socket timeout handler,
      // agent need to handle socket timeout itself.
      //
      // custom request socket timeout handle logic must follow these rules:
      //  1. Destroy socket first
      //  2. Must emit socket 'agentRemove' event tell agent remove socket
      //     from freeSockets list immediately.
      //     Otherise you may be get 'socket hang up' error when reuse
      //     free socket and timeout happen in the same time.
      if (listenerCount === 1) {
        const error = new Error('Socket timeout');
        error.code = 'ERR_SOCKET_TIMEOUT';
        error.timeout = getSocketTimeout(socket);
        // must manually call socket.end() or socket.destroy() to end the connection.
        // https://nodejs.org/dist/latest-v10.x/docs/api/net.html#net_socket_settimeout_timeout_callback
        socket.destroy(error);
        agent.removeSocket(socket, options);
        debug('%s destroy with timeout error', socket[SOCKET_NAME]);
      }
    }
  }
function onTimeout() {
    const listenerCount = socket.listeners('timeout').length;
    debug('%s(requests: %s, finished: %s) timeout after %sms, listeners %s',
      socket[SOCKET_NAME], socket[SOCKET_REQUEST_COUNT], socket[SOCKET_REQUEST_FINISHED_COUNT],
      getSocketTimeout(socket), listenerCount);
    agent.timeoutSocketCount++;
    const name = agent.getName(options);
    if (agent.freeSockets[name] && agent.freeSockets[name].indexOf(socket) !== -1) {
      // free socket timeout, destroy quietly
      socket.destroy();
      // Remove it from freeSockets list immediately to prevent new requests
      // from being sent through this socket.
      agent.removeSocket(socket, options);
      debug('%s is free, destroy quietly', socket[SOCKET_NAME]);
    } else {
      // if there is no any request socket timeout handler,
      // agent need to handle socket timeout itself.
      //
      // custom request socket timeout handle logic must follow these rules:
      //  1. Destroy socket first
      //  2. Must emit socket 'agentRemove' event tell agent remove socket
      //     from freeSockets list immediately.
      //     Otherise you may be get 'socket hang up' error when reuse
      //     free socket and timeout happen in the same time.
      if (listenerCount === 1) {
        const error = new Error('Socket timeout');
        error.code = 'ERR_SOCKET_TIMEOUT';
        error.timeout = getSocketTimeout(socket);
        // must manually call socket.end() or socket.destroy() to end the connection.
        // https://nodejs.org/dist/latest-v10.x/docs/api/net.html#net_socket_settimeout_timeout_callback
        socket.destroy(error);
        agent.removeSocket(socket, options);
        debug('%s destroy with timeout error', socket[SOCKET_NAME]);
      }
    }
  }
function onTimeout ( ) { const listenerCount = socket . listeners ( 'timeout' ) . length ; debug ( '%s(requests: %s, finished: %s) timeout after %sms, listeners %s' , socket [ SOCKET_NAME ] , socket [ SOCKET_REQUEST_COUNT ] , socket [ SOCKET_REQUEST_FINISHED_COUNT ] , getSocketTimeout ( socket ) , listenerCount ) ; agent . timeoutSocketCount ++ ; const name = agent . getName ( options ) ; if ( agent . freeSockets [ name ] && agent . freeSockets [ name ] . indexOf ( socket ) !== - 1 ) { // free socket timeout, destroy quietly socket . destroy ( ) ; // Remove it from freeSockets list immediately to prevent new requests // from being sent through this socket. agent . removeSocket ( socket , options ) ; debug ( '%s is free, destroy quietly' , socket [ SOCKET_NAME ] ) ; } else { // if there is no any request socket timeout handler, // agent need to handle socket timeout itself. // // custom request socket timeout handle logic must follow these rules: //  1. Destroy socket first //  2. Must emit socket 'agentRemove' event tell agent remove socket //     from freeSockets list immediately. //     Otherise you may be get 'socket hang up' error when reuse //     free socket and timeout happen in the same time. if ( listenerCount === 1 ) { const error = new Error ( 'Socket timeout' ) ; error . code = 'ERR_SOCKET_TIMEOUT' ; error . timeout = getSocketTimeout ( socket ) ; // must manually call socket.end() or socket.destroy() to end the connection. // https://nodejs.org/dist/latest-v10.x/docs/api/net.html#net_socket_settimeout_timeout_callback socket . destroy ( error ) ; agent . removeSocket ( socket , options ) ; debug ( '%s destroy with timeout error' , socket [ SOCKET_NAME ] ) ; } } }
start socket timeout handler
start socket timeout handler