The Node.js's missing keep alive
http.Agent
. Support http
and https
. With NTLM Support
- takes in a new config parameter 'cookieName' to uniquely identify a usersession. The cookieName defined has to be present in req.headers. This should ideally be a usersession cookie or can be any cookieName that uniquely identifies a user.
The main motivation for this fork from agentkeepalive
was to support user specific NTLM sessions.
For NTLM to work, the TCP connection has to be authorized for a user. When we use the base 'agentkeepalive',
sockets are authorized using a combination of the host
+ port
. eg yahoo.com:443
. However if multiple users
are trying to access a NTLM enabled site, the socket connections were getting mixed up between users.
Changed the implementation to include 'host+port+cookieName' while assigning sockets
Another issue that was a problem for NTLM was that the socket pool was defaulted to 100 or Infinity. Since NTLM works
on the principal of the same user connecting over a authorized socket, we should reuse the same socket per URL for a user. That means at a given time, for a host:port:user
combination, there should only be 1 socket alive.
e.g create/reuse 1 socket for yahoo.com:433:user_1
and create/reuse another socket for gmail.com:433:user_1
.
$ npm install agentkeepalive-ntlm --save
options
{Object} Set of configurable options to set on the agent. Can have the following fields:keepAlive
{Boolean} Keep sockets around in a pool to be used by other requests in the future. Default =true
.keepAliveMsecs
{Number} When using HTTP KeepAlive, how often to send TCP KeepAlive packets over sockets being kept alive. Default =1000
. Only relevant ifkeepAlive
is set totrue
.freeSocketKeepAliveTimeout
: {Number} Sets the free socket to timeout afterfreeSocketKeepAliveTimeout
milliseconds of inactivity on the free socket. Default is15000
. Only relevant ifkeepAlive
is set totrue
.timeout
: {Number} Sets the working socket to timeout aftertimeout
milliseconds of inactivity on the working socket. Default isfreeSocketKeepAliveTimeout * 2
.maxSockets
{Number} Maximum number of sockets to allow per host. Default =Infinity
.maxFreeSockets
{Number} Maximum number of sockets to leave open in a free state. Only relevant ifkeepAlive
is set totrue
. Default =256
.cookieName
{string} The name of a cookie in the http request header that clearly identifies a unique user session. Defaults to ''.
const http = require('http');
const Agent = require('agentkeepalive-ntlm');
const keepaliveAgentNTLM = new Agent({
maxSockets: 100,
maxFreeSockets: 10,
timeout: 60000,
freeSocketKeepAliveTimeout: 30000, // free socket keepalive for 30 seconds
cookieName: 'mycookie'
});
const options = {
host: 'cnodejs.org',
port: 80,
path: '/',
method: 'GET',
agent: keepaliveAgentNTLM,
};
const req = http.request(options, res => {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
});
req.on('error', e => {
console.log('problem with request: ' + e.message);
});
req.end();
setTimeout(() => {
console.log('agent status: %j', keepaliveAgentNTLM.getCurrentStatus());
}, 2000);
agent.getCurrentStatus()
will return a object to show the status of this agent:
{
createSocketCount: 10,
closeSocketCount: 5,
timeoutSocketCount: 0,
requestCount: 5,
freeSockets: { 'localhost:57479:': 3 },
sockets: { 'localhost:57479:': 5 },
requests: {}
}
const https = require('https');
const HttpsAgent = require('agentkeepalive-ntlm').HttpsAgent;
const keepaliveAgentNTLM = new HttpsAgent();
// https://www.google.com/search?q=nodejs&sugexp=chrome,mod=12&sourceid=chrome&ie=UTF-8
const options = {
host: 'www.google.com',
port: 443,
path: '/search?q=nodejs&sugexp=chrome,mod=12&sourceid=chrome&ie=UTF-8',
method: 'GET',
agent: keepaliveAgentNTLM,
};
const req = https.request(options, res => {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', chunk => {
console.log('BODY: ' + chunk);
});
});
req.on('error', e => {
console.log('problem with request: ' + e.message);
});
req.end();
setTimeout(() => {
console.log('agent status: %j', keepaliveAgentNTLM.getCurrentStatus());
}, 2000);
## License
Copyright © 2017, [Paddy Viswanathan](https://github.com/pappan123).
Released under the [MIT license]