Skip to content

algorithm: freeSockets pop/shift #8

@syndr0m

Description

@syndr0m

the current algorithm maximize the number of sockets opened, by re-using the first stacked free socket using freesockets.shift()

@see https://github.com/node-modules/agentkeepalive/blob/master/lib/_http_agent.js#L170

if (freeLen) {
    // we have a free socket, so use that.
    var socket = this.freeSockets[name].shift();
    debug('have free socket');
freesockets = [1][2][3][4][5]
# a socket is freed
freesockets = [1][2][3][4][5][6]
# new request arrive, current algorithm use "shift"
freesockets = [2][3][4][5][6]
# there might rarely be a timeout of any sockets.

an alternative could be to use freesockets.pop() to minimize the number of sockets opened

freesockets = [1][2][3][4][5]
# a socket is freed
freesockets = [1][2][3][4][5][6]
# new request arrive, using pop
freesockets = [1][2][3][4][5]
# first socket timeout
freesockets = [2][3][4][5]

implementation:

var algo = (this.options.algo === 'pop') ? 'pop' : 'shift';
var socket = this.freeSockets[name][algo]();

I tested this for 1K request/s, it divided the pool size by 5.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions