Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: timeout support humanize ms #48

Merged
merged 1 commit into from Feb 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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