Skip to content

Commit

Permalink
feat: timeout support humanize ms (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
dead-horse authored and fengmk2 committed Feb 20, 2017
1 parent fe733e7 commit d0b9b18
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
5 changes: 4 additions & 1 deletion lib/agent.js
Expand Up @@ -9,6 +9,7 @@
'use strict';

const OriginalAgent = require('./_http_agent').Agent;
const ms = require('humanize-ms');

class Agent extends OriginalAgent {
constructor(options) {
Expand All @@ -22,16 +23,18 @@ class Agent extends OriginalAgent {
if (options.keepAliveTimeout) {
options.freeSocketKeepAliveTimeout = options.keepAliveTimeout;
}
options.freeSocketKeepAliveTimeout = ms(options.freeSocketKeepAliveTimeout);

// Sets the socket to timeout after timeout milliseconds of inactivity on the socket.
// By default is double free socket keepalive timeout.
if (options.timeout === undefined) {
options.timeout = options.freeSocketKeepAliveTimeout * 2;
// make sure socket default inactivity timeout >= 30000
// make sure socket default inactivity timeout >= 30s
if (options.timeout < 30000) {
options.timeout = 30000;
}
}
options.timeout = ms(options.timeout);

super(options);

Expand Down
4 changes: 3 additions & 1 deletion package.json
Expand Up @@ -29,7 +29,9 @@
"keepalive",
"agentkeepalive"
],
"dependencies": {},
"dependencies": {
"humanize-ms": "^1.2.0"
},
"devDependencies": {
"egg-bin": "^1.9.1",
"egg-ci": "^1.1.0",
Expand Down
4 changes: 2 additions & 2 deletions test/agent.test.js
Expand Up @@ -108,8 +108,8 @@ describe('test/agent.test.js', () => {
it('should inactivity socket timeout', done => {
const name = 'localhost:' + port + ':';
const agentkeepalive = new Agent({
freeSocketKeepAliveTimeout: 5000,
timeout: 1000,
freeSocketKeepAliveTimeout: '5s',
timeout: '1s',
});
assert(!agentkeepalive.sockets[name]);
assert(!agentkeepalive.freeSockets[name]);
Expand Down

0 comments on commit d0b9b18

Please sign in to comment.