Skip to content
This repository has been archived by the owner on Dec 5, 2022. It is now read-only.

Commit

Permalink
feat: use keep alive agents for all fragment requests
Browse files Browse the repository at this point in the history
  • Loading branch information
vigneshshanmugam committed Jan 28, 2019
1 parent 963ca82 commit 19c69e8
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 1 deletion.
20 changes: 20 additions & 0 deletions lib/agent-store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';
const Agent = require('agentkeepalive');
const { SOCKET_TTL } = require('./constants');

class AgentStore {
constructor() {
const defaultAgentOpts = { socketActiveTTL: SOCKET_TTL };
this.httpAgent = new Agent(defaultAgentOpts);
this.httpsAgent = new Agent.HttpsAgent(defaultAgentOpts);
}

getAgent(protocol) {
if (protocol === 'http:') {
return this.httpAgent;
}
return this.httpsAgent;
}
}

module.exports = new AgentStore();
10 changes: 10 additions & 0 deletions lib/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';

/**
* Sets the timeout for the socket active time to live even if its in use.
*/
const SOCKET_TTL = 60 * 1000;

module.exports = {
SOCKET_TTL
};
4 changes: 3 additions & 1 deletion lib/request-fragment.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ const http = require('http');
const https = require('https');
const url = require('url');
const { globalTracer, FORMAT_HTTP_HEADERS } = require('opentracing');
const tracer = globalTracer();
const agentStore = require('./agent-store');

const tracer = globalTracer();
// By default tailor supports gzipped response from fragments
const requiredHeaders = {
'accept-encoding': 'gzip, deflate'
Expand Down Expand Up @@ -33,6 +34,7 @@ module.exports = filterHeaders => (
const parsedUrl = url.parse(fragmentUrl);
const options = Object.assign(
{
agent: agentStore.getAgent(parsedUrl.protocol),
headers: Object.assign(
filterHeaders(fragmentAttributes, request),
requiredHeaders
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"author": "Andrey Kuzmin",
"license": "MIT",
"dependencies": {
"agentkeepalive": "^4.0.0",
"opentracing": "^0.14.3",
"parse5": "^3.0.3",
"util.promisify": "^1.0.0"
Expand Down
18 changes: 18 additions & 0 deletions tests/agent-store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';
const { parse } = require('url');
const Agent = require('agentkeepalive');
const assert = require('assert');
const agentStore = require('../lib/agent-store');

describe('Agent Store', () => {
it('should get https agent by default', () => {
const agent = agentStore.getAgent();
assert(agent instanceof Agent.HttpsAgent);
});

it('shoulg get http agent for http endpoints', () => {
const { protocol } = parse('http://test.com');
const agent = agentStore.getAgent(protocol);
assert(agent instanceof Agent);
});
});
33 changes: 33 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ agentkeepalive@^2.0.3:
version "2.2.0"
resolved "https://registry.yarnpkg.com/agentkeepalive/-/agentkeepalive-2.2.0.tgz#c5d1bd4b129008f1163f236f86e5faea2026e2ef"

agentkeepalive@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/agentkeepalive/-/agentkeepalive-4.0.0.tgz#777e7e4c828bde2690cc7a374ede587d45ba4271"
integrity sha512-McCjHBAI3FGTy9XOMDr/8qzXK4VBL3h+O/TqlBdsyiwX21SdPCQhPIX0Y1HS5ctO5m2maVQRJWEn0IrzKRB6oQ==
dependencies:
debug "^4.1.0"
depd "^1.1.2"
humanize-ms "^1.2.1"

ajv-keywords@^1.0.0:
version "1.5.1"
resolved "https://registry.yarnpkg.com/ajv-keywords/-/ajv-keywords-1.5.1.tgz#314dd0a4b3368fad3dfcdc54ede6171b886daf3c"
Expand Down Expand Up @@ -423,6 +432,13 @@ debug@^3.1.0:
dependencies:
ms "2.0.0"

debug@^4.1.0:
version "4.1.1"
resolved "https://registry.yarnpkg.com/debug/-/debug-4.1.1.tgz#3b72260255109c6b589cee050f1d516139664791"
integrity sha512-pYAIzeRo8J6KPEaJ0VWOh5Pzkbw/RetuzehGM7QRRX5he4fPHx2rdKMB256ehJCkX+XRQm16eZLqLNS8RSZXZw==
dependencies:
ms "^2.1.1"

decamelize@^1.0.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/decamelize/-/decamelize-1.2.0.tgz#f6534d15148269b20352e7bee26f501f9a191290"
Expand Down Expand Up @@ -464,6 +480,11 @@ delayed-stream@~1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/delayed-stream/-/delayed-stream-1.0.0.tgz#df3ae199acadfb7d440aaae0b29e2272b24ec619"

depd@^1.1.2:
version "1.1.2"
resolved "https://registry.yarnpkg.com/depd/-/depd-1.1.2.tgz#9bcd52e14c097763e749b274c4346ed2e560b5a9"
integrity sha1-m81S4UwJd2PnSbJ0xDRu0uVgtak=

diff@3.2.0:
version "3.2.0"
resolved "https://registry.yarnpkg.com/diff/-/diff-3.2.0.tgz#c9ce393a4b7cbd0b058a725c93df299027868ff9"
Expand Down Expand Up @@ -893,6 +914,13 @@ https-proxy-agent@^2.1.0:
agent-base "^4.1.0"
debug "^3.1.0"

humanize-ms@^1.2.1:
version "1.2.1"
resolved "https://registry.yarnpkg.com/humanize-ms/-/humanize-ms-1.2.1.tgz#c46e3159a293f6b896da29316d8b6fe8bb79bbed"
integrity sha1-xG4xWaKT9riW2ikxbYtv6Lt5u+0=
dependencies:
ms "^2.0.0"

iamdee@^0.4.0:
version "0.4.0"
resolved "https://registry.yarnpkg.com/iamdee/-/iamdee-0.4.0.tgz#4c4872db5e82229a361eabad543c160d7f16959d"
Expand Down Expand Up @@ -1299,6 +1327,11 @@ ms@2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8"

ms@^2.0.0, ms@^2.1.1:
version "2.1.1"
resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.1.tgz#30a5864eb3ebb0a66f2ebe6d727af06a09d86e0a"
integrity sha512-tgp+dl5cGk28utYktBsrFqA7HKgrhgPsg6Z/EfhWI4gl1Hwq8B/GmY/0oXZ6nF8hDVesS/FpnYaD/kOWhYQvyg==

mute-stream@0.0.7:
version "0.0.7"
resolved "https://registry.yarnpkg.com/mute-stream/-/mute-stream-0.0.7.tgz#3075ce93bc21b8fab43e1bc4da7e8115ed1e7bab"
Expand Down

0 comments on commit 19c69e8

Please sign in to comment.