agentkeepalive icon indicating copy to clipboard operation
agentkeepalive copied to clipboard

algorithm: freeSockets pop/shift

Open syndr0m opened this issue 11 years ago • 7 comments

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.

syndr0m avatar Sep 01 '14 08:09 syndr0m

@syndr0m good point! Can you impl this with a pr?

fengmk2 avatar Sep 02 '14 07:09 fengmk2

@syndr0m this sounds awesome!

FredKSchott avatar Oct 27 '14 16:10 FredKSchott

Any progress?

fengmk2 avatar Jan 29 '15 17:01 fengmk2

I think use pop instead of shift is enough

fengmk2 avatar Jan 29 '15 18:01 fengmk2

Did this ever happen?

tony-gutierrez avatar Oct 20 '18 18:10 tony-gutierrez

fyi, the referenced code is not currently in this project:

https://github.com/nodejs/node/blob/master/lib/_http_agent.js#L165

You'd probably need to replace addRequest and curry the selected algorithm into the original'ish version.

A quick thought here... While it would be nice to be able to have the algorithm choice, I believe the intended behavior of keep-alives is to keep connections alive to reduce the overhead of establishing connections. The max number of free connections is configurable, so I'm not sure if this functionality has a ton of value.

bfdill avatar Mar 15 '19 02:03 bfdill

Maybe related/solved by this https://github.com/nodejs/node/pull/33278 ?

dkMorlok avatar Jun 27 '20 15:06 dkMorlok