\href{https://npmjs.org/package/agentkeepalive}{\texttt{ }} \href{https://travis-ci.org/node-modules/agentkeepalive}{\texttt{ }} \href{https://ci.appveyor.com/project/fengmk2/agentkeepalive}{\texttt{ }} \href{https://codecov.io/gh/node-modules/agentkeepalive}{\texttt{ }} \href{https://david-dm.org/node-modules/agentkeepalive}{\texttt{ }} \href{https://snyk.io/test/npm/agentkeepalive}{\texttt{ }} \href{https://npmjs.org/package/agentkeepalive}{\texttt{ }}

The Node.\+js\textquotesingle{}s missing {\ttfamily keep alive} {\ttfamily http.\+Agent}. Support {\ttfamily http} and {\ttfamily https}.\hypertarget{md_sfcode_node_modules_agentkeepalive_README_autotoc_md2594}{}\doxysection{What\textquotesingle{}s different from original $<$tt$>$http.\+Agent$<$/tt$>$?}\label{md_sfcode_node_modules_agentkeepalive_README_autotoc_md2594}

\begin{DoxyItemize}
\item {\ttfamily keep\+Alive=true} by default
\item Disable Nagle\textquotesingle{}s algorithm\+: {\ttfamily socket.\+set\+No\+Delay(true)}
\item Add free socket timeout\+: avoid long time inactivity socket leak in the free-\/sockets queue.
\item Add active socket timeout\+: avoid long time inactivity socket leak in the active-\/sockets queue.
\end{DoxyItemize}\hypertarget{md_sfcode_node_modules_agentkeepalive_README_autotoc_md2595}{}\doxysection{Install}\label{md_sfcode_node_modules_agentkeepalive_README_autotoc_md2595}

\begin{DoxyCode}{0}
\DoxyCodeLine{\$ npm install agentkeepalive -\/-\/save }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_agentkeepalive_README_autotoc_md2596}{}\doxysection{new Agent(\mbox{[}options\mbox{]})}\label{md_sfcode_node_modules_agentkeepalive_README_autotoc_md2596}

\begin{DoxyItemize}
\item {\ttfamily options} \{Object\} Set of configurable options to set on the agent. Can have the following fields\+:
\begin{DoxyItemize}
\item {\ttfamily keep\+Alive} \{Boolean\} Keep sockets around in a pool to be used by other requests in the future. Default = {\ttfamily true}.
\item {\ttfamily keep\+Alive\+Msecs} \{Number\} When using the keep\+Alive option, specifies the initial delay for TCP Keep-\/\+Alive packets. Ignored when the keep\+Alive option is false or undefined. Defaults to 1000. Default = {\ttfamily 1000}. Only relevant if {\ttfamily keep\+Alive} is set to {\ttfamily true}.
\item {\ttfamily free\+Socket\+Keep\+Alive\+Timeout}\+: \{Number\} Sets the free socket to timeout after {\ttfamily free\+Socket\+Keep\+Alive\+Timeout} milliseconds of inactivity on the free socket. Default is {\ttfamily 15000}. Only relevant if {\ttfamily keep\+Alive} is set to {\ttfamily true}.
\item {\ttfamily timeout}\+: \{Number\} Sets the working socket to timeout after {\ttfamily timeout} milliseconds of inactivity on the working socket. Default is {\ttfamily free\+Socket\+Keep\+Alive\+Timeout $\ast$ 2}.
\item {\ttfamily max\+Sockets} \{Number\} Maximum number of sockets to allow per host. Default = {\ttfamily Infinity}.
\item {\ttfamily max\+Free\+Sockets} \{Number\} Maximum number of sockets (per host) to leave open in a free state. Only relevant if {\ttfamily keep\+Alive} is set to {\ttfamily true}. Default = {\ttfamily 256}.
\item {\ttfamily socket\+Active\+TTL} \{Number\} Sets the socket active time to live, even if it\textquotesingle{}s in use. If not setted the behaviour continues the same (the socket will be released only when free) Default = {\ttfamily null}.
\end{DoxyItemize}
\end{DoxyItemize}\hypertarget{md_sfcode_node_modules_agentkeepalive_README_autotoc_md2597}{}\doxysection{Usage}\label{md_sfcode_node_modules_agentkeepalive_README_autotoc_md2597}

\begin{DoxyCode}{0}
\DoxyCodeLine{const http = require('http');}
\DoxyCodeLine{const Agent = require('agentkeepalive');}
\DoxyCodeLine{}
\DoxyCodeLine{const keepaliveAgent = new Agent(\{}
\DoxyCodeLine{  maxSockets: 100,}
\DoxyCodeLine{  maxFreeSockets: 10,}
\DoxyCodeLine{  timeout: 60000,}
\DoxyCodeLine{  freeSocketKeepAliveTimeout: 30000, // free socket keepalive for 30 seconds}
\DoxyCodeLine{\});}
\DoxyCodeLine{}
\DoxyCodeLine{const options = \{}
\DoxyCodeLine{  host: 'cnodejs.org',}
\DoxyCodeLine{  port: 80,}
\DoxyCodeLine{  path: '/',}
\DoxyCodeLine{  method: 'GET',}
\DoxyCodeLine{  agent: keepaliveAgent,}
\DoxyCodeLine{\};}
\DoxyCodeLine{}
\DoxyCodeLine{const req = http.request(options, res => \{}
\DoxyCodeLine{  console.log('STATUS: ' + res.statusCode);}
\DoxyCodeLine{  console.log('HEADERS: ' + JSON.stringify(res.headers));}
\DoxyCodeLine{  res.setEncoding('utf8');}
\DoxyCodeLine{  res.on('data', function (chunk) \{}
\DoxyCodeLine{    console.log('BODY: ' + chunk);}
\DoxyCodeLine{  \});}
\DoxyCodeLine{\});}
\DoxyCodeLine{req.on('error', e => \{}
\DoxyCodeLine{  console.log('problem with request: ' + e.message);}
\DoxyCodeLine{\});}
\DoxyCodeLine{req.end();}
\DoxyCodeLine{}
\DoxyCodeLine{setTimeout(() => \{}
\DoxyCodeLine{  if (keepaliveAgent.statusChanged) \{}
\DoxyCodeLine{    console.log('[\%s] agent status changed: \%j', Date(), keepaliveAgent.getCurrentStatus());}
\DoxyCodeLine{  \}}
\DoxyCodeLine{\}, 2000);}

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_agentkeepalive_README_autotoc_md2598}{}\doxysubsection{$<$tt$>$getter agent.\+status\+Changed$<$/tt$>$}\label{md_sfcode_node_modules_agentkeepalive_README_autotoc_md2598}
counters have change or not after last checkpoint.\hypertarget{md_sfcode_node_modules_agentkeepalive_README_autotoc_md2599}{}\doxysubsection{$<$tt$>$agent.\+get\+Current\+Status()$<$/tt$>$}\label{md_sfcode_node_modules_agentkeepalive_README_autotoc_md2599}
{\ttfamily agent.\+get\+Current\+Status()} will return a object to show the status of this agent\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{\{}
\DoxyCodeLine{  createSocketCount: 10,}
\DoxyCodeLine{  closeSocketCount: 5,}
\DoxyCodeLine{  timeoutSocketCount: 0,}
\DoxyCodeLine{  requestCount: 5,}
\DoxyCodeLine{  freeSockets: \{ 'localhost:57479:': 3 \},}
\DoxyCodeLine{  sockets: \{ 'localhost:57479:': 5 \},}
\DoxyCodeLine{  requests: \{\}}
\DoxyCodeLine{\} }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_agentkeepalive_README_autotoc_md2600}{}\doxysubsection{Support $<$tt$>$https$<$/tt$>$}\label{md_sfcode_node_modules_agentkeepalive_README_autotoc_md2600}

\begin{DoxyCode}{0}
\DoxyCodeLine{const https = require('https');}
\DoxyCodeLine{const HttpsAgent = require('agentkeepalive').HttpsAgent;}
\DoxyCodeLine{}
\DoxyCodeLine{const keepaliveAgent = new HttpsAgent();}
\DoxyCodeLine{// https://www.google.com/search?q=nodejs\&sugexp=chrome,mod=12\&sourceid=chrome\&ie=UTF-\/8}
\DoxyCodeLine{const options = \{}
\DoxyCodeLine{  host: 'www.google.com',}
\DoxyCodeLine{  port: 443,}
\DoxyCodeLine{  path: '/search?q=nodejs\&sugexp=chrome,mod=12\&sourceid=chrome\&ie=UTF-\/8',}
\DoxyCodeLine{  method: 'GET',}
\DoxyCodeLine{  agent: keepaliveAgent,}
\DoxyCodeLine{\};}
\DoxyCodeLine{}
\DoxyCodeLine{const req = https.request(options, res => \{}
\DoxyCodeLine{  console.log('STATUS: ' + res.statusCode);}
\DoxyCodeLine{  console.log('HEADERS: ' + JSON.stringify(res.headers));}
\DoxyCodeLine{  res.setEncoding('utf8');}
\DoxyCodeLine{  res.on('data', chunk => \{}
\DoxyCodeLine{    console.log('BODY: ' + chunk);}
\DoxyCodeLine{  \});}
\DoxyCodeLine{\});}
\DoxyCodeLine{}
\DoxyCodeLine{req.on('error', e => \{}
\DoxyCodeLine{  console.log('problem with request: ' + e.message);}
\DoxyCodeLine{\});}
\DoxyCodeLine{req.end();}
\DoxyCodeLine{}
\DoxyCodeLine{setTimeout(() => \{}
\DoxyCodeLine{  console.log('agent status: \%j', keepaliveAgent.getCurrentStatus());}
\DoxyCodeLine{\}, 2000); }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_agentkeepalive_README_autotoc_md2601}{}\doxysection{$<$a href=\char`\"{}https\+://github.\+com/node-\/modules/agentkeepalive/tree/master/benchmark\char`\"{}$>$\+Benchmark$<$/a$>$}\label{md_sfcode_node_modules_agentkeepalive_README_autotoc_md2601}
run the benchmark\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{cd benchmark}
\DoxyCodeLine{sh start.sh }

\end{DoxyCode}


Intel(\+R) Core(\+TM)2 Duo CPU P8600 @ 2.\+40\+GHz

node@v0.8.\+9

50 max\+Sockets, 60 concurrent, 1000 requests per concurrent, 5ms delay

Keep alive agent (30 seconds)\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{Transactions:          60000 hits}
\DoxyCodeLine{Availability:         100.00 \%}
\DoxyCodeLine{Elapsed time:          29.70 secs}
\DoxyCodeLine{Data transferred:        14.88 MB}
\DoxyCodeLine{Response time:            0.03 secs}
\DoxyCodeLine{Transaction rate:      2020.20 trans/sec}
\DoxyCodeLine{Throughput:           0.50 MB/sec}
\DoxyCodeLine{Concurrency:           59.84}
\DoxyCodeLine{Successful transactions:       60000}
\DoxyCodeLine{Failed transactions:             0}
\DoxyCodeLine{Longest transaction:          0.15}
\DoxyCodeLine{Shortest transaction:         0.01 }

\end{DoxyCode}


Normal agent\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{Transactions:          60000 hits}
\DoxyCodeLine{Availability:         100.00 \%}
\DoxyCodeLine{Elapsed time:          46.53 secs}
\DoxyCodeLine{Data transferred:        14.88 MB}
\DoxyCodeLine{Response time:            0.05 secs}
\DoxyCodeLine{Transaction rate:      1289.49 trans/sec}
\DoxyCodeLine{Throughput:           0.32 MB/sec}
\DoxyCodeLine{Concurrency:           59.81}
\DoxyCodeLine{Successful transactions:       60000}
\DoxyCodeLine{Failed transactions:             0}
\DoxyCodeLine{Longest transaction:          0.45}
\DoxyCodeLine{Shortest transaction:         0.00 }

\end{DoxyCode}


Socket created\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{[proxy.js:120000] keepalive, 50 created, 60000 requestFinished, 1200 req/socket, 0 requests, 0 sockets, 0 unusedSockets, 50 timeout}
\DoxyCodeLine{\{"{} <10ms"{}:662,"{} <15ms"{}:17825,"{} <20ms"{}:20552,"{} <30ms"{}:17646,"{} <40ms"{}:2315,"{} <50ms"{}:567,"{} <100ms"{}:377,"{} <150ms"{}:56,"{} <200ms"{}:0,"{} >=200ms+"{}:0\}}
\DoxyCodeLine{-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/}
\DoxyCodeLine{[proxy.js:120000] normal   , 53866 created, 84260 requestFinished, 1.56 req/socket, 0 requests, 0 sockets}
\DoxyCodeLine{\{"{} <10ms"{}:75,"{} <15ms"{}:1112,"{} <20ms"{}:10947,"{} <30ms"{}:32130,"{} <40ms"{}:8228,"{} <50ms"{}:3002,"{} <100ms"{}:4274,"{} <150ms"{}:181,"{} <200ms"{}:18,"{} >=200ms+"{}:33\} }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_agentkeepalive_README_autotoc_md2602}{}\doxysection{License}\label{md_sfcode_node_modules_agentkeepalive_README_autotoc_md2602}

\begin{DoxyCode}{0}
\DoxyCodeLine{(The MIT License)}
\DoxyCodeLine{}
\DoxyCodeLine{Copyright(c) node-\/modules and other contributors.}
\DoxyCodeLine{Copyright(c) 2012 -\/ 2015 fengmk2 <fengmk2@gmail.com>}
\DoxyCodeLine{}
\DoxyCodeLine{Permission is hereby granted, free of charge, to any person obtaining}
\DoxyCodeLine{a copy of this software and associated documentation files (the}
\DoxyCodeLine{'Software'), to deal in the Software without restriction, including}
\DoxyCodeLine{without limitation the rights to use, copy, modify, merge, publish,}
\DoxyCodeLine{distribute, sublicense, and/or sell copies of the Software, and to}
\DoxyCodeLine{permit persons to whom the Software is furnished to do so, subject to}
\DoxyCodeLine{the following conditions:}
\DoxyCodeLine{}
\DoxyCodeLine{The above copyright notice and this permission notice shall be}
\DoxyCodeLine{included in all copies or substantial portions of the Software.}
\DoxyCodeLine{}
\DoxyCodeLine{THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,}
\DoxyCodeLine{EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF}
\DoxyCodeLine{MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.}
\DoxyCodeLine{IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY}
\DoxyCodeLine{CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,}
\DoxyCodeLine{TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE}
\DoxyCodeLine{SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. }

\end{DoxyCode}
 