Skip to content

Connection keep alive behavior. #271

@Borewit

Description

@Borewit

Regarding the 'Connection' header

Unless you're running an old version of Node (< 0.11.4), by default Needle won't set the Connection header on requests, yielding Node's default behaviour of keeping the connection alive with the target server. This speeds up inmensely the process of sending several requests to the same host.

On older versions, however, this has the unwanted behaviour of preventing the runtime from exiting, either because of a bug or 'feature' that was changed on 0.11.4. To overcome this Needle does set the 'Connection' header to 'close' on those versions, however this also means that making new requests to the same host doesn't benefit from Keep-Alive.

So if you're stuck on 0.10 or even lower and want full speed, you can simply set the Connection header to 'Keep-Alive' by using { connection: 'Keep-Alive' }. Please note, though, that an event loop handler will prevent the runtime from exiting so you'll need to manually call process.exit() or the universe will collapse.

Tomas, I don't think that information is accurate.
To me it looks like the default behavior of Node (tested with v10.13.0, seen it with older versions as well) is not to enable keep-alive (sending Connection: close):

GET /ws/2/artist/?query=Stromae&offset=&limit=&fmt=json HTTP/1.1
accept: */*
user-agent: Needle/2.2.4 (Node.js v10.13.0; win32 x64)
host: test.musicbrainz.org
Connection: close

Tested both with & without HTTP-proxy assigned, consistent behavior.

The only way I found to keep the connection alive is by assigning an Http(s)Agent:

const httpAgent = new http.Agent({keepAlive: true});
needle('get', 'http://something', null, {
        user_agent: httpAgent
      });

const httpsAgent = new https.Agent({keepAlive: true});
needle('get', 'https://something', null, {
        user_agent: httpsAgent
      });

This doesn't seem to work in combination with an http-proxy.
Axios suffers from the same limitation: axios/axios#1981

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions