Skip to content

Commit

Permalink
feat(request): add keep alive option
Browse files Browse the repository at this point in the history
  • Loading branch information
sonicoder86 committed Oct 12, 2018
1 parent fc8a3b1 commit 068e001
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"expect": true
},
"rules": {
"security/detect-object-injection": 0
"security/detect-object-injection": 0,
"no-unused-expressions": 0
},
"extends": [
"emarsys"
Expand Down
11 changes: 10 additions & 1 deletion lib/request.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
'use strict';

const Escher = require('escher-auth');
const http = require('http');
const https = require('https');
const Options = require('./requestOption');
const Wrapper = require('./wrapper');
const SuiteRequestError = require('./requestError');
Expand All @@ -21,6 +23,11 @@ class SuiteRequest {

this._escher = new Escher(escherConfig);
this._options = requestOptions;

if (requestOptions.keepAlive) {
this.httpAgent = new http.Agent({ keepAlive: true });
this.httpsAgent = new https.Agent({ keepAlive: true });
}
}

get(path) {
Expand Down Expand Up @@ -72,7 +79,9 @@ class SuiteRequest {
return Object.assign({}, defaultOptions, {
method: method,
url: realPath,
path: realPath
path: realPath,
httpAgent: this.httpAgent,
httpsAgent: this.httpsAgent
});
}

Expand Down
29 changes: 29 additions & 0 deletions lib/request.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
const SuiteRequest = require('./request');
const axios = require('axios');
const Escher = require('escher-auth');
const http = require('http');
const https = require('https');

describe('SuiteRequest', function() {
const serviceConfig = {
Expand Down Expand Up @@ -138,4 +140,31 @@ describe('SuiteRequest', function() {
const firstCall = Escher.prototype.signRequest.getCall(0);
expect(firstCall.args[1]).to.eql(JSON.stringify(payload));
});

it('should not create http agents by default', function() {
suiteRequest = SuiteRequest.create('key-id', 'secret', requestOptions);

expect(suiteRequest.httpAgent).to.be.undefined;
expect(suiteRequest.httpsAgent).to.be.undefined;
});

it('should create http agents when connection is keep alive', function() {
requestOptions = new SuiteRequest.Options(serviceConfig.host, Object.assign({ keepAlive: true }, serviceConfig));

suiteRequest = SuiteRequest.create('key-id', 'secret', requestOptions);

expect(suiteRequest.httpAgent).to.be.an.instanceOf(http.Agent);
expect(suiteRequest.httpsAgent).to.be.an.instanceOf(https.Agent);
});

it('should pass http agents to wrapper', function*() {
requestOptions = new SuiteRequest.Options(serviceConfig.host, Object.assign({ keepAlive: true }, serviceConfig));
suiteRequest = SuiteRequest.create('key-id', 'secret', requestOptions);

yield suiteRequest.post('/path', { name: 'Almanach' });

const requestArgument = requestStub.args[0][0];
expect(requestArgument.httpAgent).to.eql(suiteRequest.httpAgent);
expect(requestArgument.httpsAgent).to.eql(suiteRequest.httpsAgent);
});
});
1 change: 1 addition & 0 deletions lib/requestOption.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class SuiteRequestOption {
this.timeout = 'timeout' in options ? options.timeout : 15000;
this.allowEmptyResponse = false;
this.maxContentLength = options.maxContentLength || 10 * MEGA_BYTE;
this.keepAlive = !!options.keepAlive;

if (!options) {
options = {};
Expand Down
4 changes: 3 additions & 1 deletion lib/wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ class RequestWrapper {
transformResponse: [body => body],
maxContentLength: this.requestOptions.maxContentLength,
validateStatus: () => true,
cancelToken: source.token
cancelToken: source.token,
httpAgent: this.requestOptions.httpAgent,
httpsAgent: this.requestOptions.httpsAgent
};

return axios
Expand Down

0 comments on commit 068e001

Please sign in to comment.