diff --git a/lib/base/RequestClient.d.ts b/lib/base/RequestClient.d.ts index 341e893495..1bb4222933 100644 --- a/lib/base/RequestClient.d.ts +++ b/lib/base/RequestClient.d.ts @@ -5,7 +5,7 @@ declare class RequestClient { constructor(opts?: RequestClient.RequestClientOptions); /** * Make an HTTP request - * @param opts The request options + * @param opts The options https.Agent takes in */ request( opts: RequestClient.RequestOptions @@ -59,9 +59,33 @@ declare namespace RequestClient { export interface RequestClientOptions { /** * A timeout in milliseconds. This will be used as the HTTPS agent's socket - * timeout, and as the default request timeout. + * timeout, AND as the default request timeout. */ timeout?: number; + /** + * https.Agent keepAlive option + */ + keepAlive?: boolean; + /** + * https.Agent keepAliveMSecs option + */ + keepAliveMsecs?: number; + /** + * https.Agent maxSockets option + */ + maxSockets?: number; + /** + * https.Agent maxTotalSockets option + */ + maxTotalSockets?: number; + /** + * https.Agent maxFreeSockets option + */ + maxFreeSockets?: number; + /** + * https.Agent scheduling option + */ + scheduling?: string; } export interface Headers { diff --git a/lib/base/RequestClient.js b/lib/base/RequestClient.js index 1d9e4d5ca1..6ebc16831b 100644 --- a/lib/base/RequestClient.js +++ b/lib/base/RequestClient.js @@ -16,8 +16,14 @@ const DEFAULT_TIMEOUT = 30000; /** * Make http request - * @param {object} opts - The options argument - * @param {string} opts.timeout - A custom timeout to use. This will be used as the socket timeout, and as the default request timeout. + * @param {object} opts - The options passed to https.Agent + * @param {number} opts.timeout - https.Agent timeout option. Used as the socket timeout, AND as the default request timeout. + * @param {boolean} opts.keepAlive - https.Agent keepAlive option + * @param {number} opts.keepAliveMsecs - https.Agent keepAliveMsecs option + * @param {number} opts.maxSockets - https.Agent maxSockets option + * @param {number} opts.maxTotalSockets - https.Agent maxTotalSockets option + * @param {number} opts.maxFreeSockets - https.Agent maxFreeSockets option + * @param {string} opts.scheduling - https.Agent scheduling option */ var RequestClient = function (opts) { opts = opts || {}; @@ -26,6 +32,12 @@ var RequestClient = function (opts) { // construct an https agent let agentOpts = { timeout: this.defaultTimeout, + keepAlive: opts.keepAlive, + keepAliveMsecs: opts.keepAliveMsecs, + maxSockets: opts.maxSockets, + maxTotalSockets: opts.maxTotalSockets, + maxFreeSockets: opts.maxFreeSockets, + scheduling: opts.scheduling, }; let agent; diff --git a/spec/unit/base/RequestClient.spec.js b/spec/unit/base/RequestClient.spec.js index d718d5804b..8e5ffd6765 100644 --- a/spec/unit/base/RequestClient.spec.js +++ b/spec/unit/base/RequestClient.spec.js @@ -53,6 +53,12 @@ describe('RequestClient constructor', function() { }); expect(requestClient.axios.defaults.httpsAgent).not.toBeInstanceOf(HttpsProxyAgent); expect(requestClient.axios.defaults.httpsAgent.options.timeout).toEqual(30000); + expect(requestClient.axios.defaults.httpsAgent.options.keepAlive).toBe(undefined); + expect(requestClient.axios.defaults.httpsAgent.options.keepAliveMsecs).toBe(undefined); + expect(requestClient.axios.defaults.httpsAgent.options.maxSockets).toBe(undefined); + expect(requestClient.axios.defaults.httpsAgent.options.maxTotalSockets).toBe(undefined); + expect(requestClient.axios.defaults.httpsAgent.options.maxFreeSockets).toBe(undefined); + expect(requestClient.axios.defaults.httpsAgent.options.scheduling).toBe(undefined); }); it('should initialize with a proxy', function() { @@ -67,14 +73,36 @@ describe('RequestClient constructor', function() { expect(requestClient.axios.defaults.httpsAgent.proxy.host).toEqual('example.com'); }); - it('should initialize with a timeout', function() { - const requestClient = new RequestClientMock({ timeout: 5000 }); + it('should initialize custom https settings (all settings customized)', function() { + const requestClient = new RequestClientMock({ timeout: 5000, keepAlive: true, keepAliveMsecs: 1500, maxSockets: 100, maxTotalSockets: 1000, maxFreeSockets: 10, scheduling: 'fifo'}); expect(requestClient.defaultTimeout).toEqual(5000); expect(requestClient.axios.defaults.headers.post).toEqual({ 'Content-Type': 'application/x-www-form-urlencoded', }); expect(requestClient.axios.defaults.httpsAgent).not.toBeInstanceOf(HttpsProxyAgent); expect(requestClient.axios.defaults.httpsAgent.options.timeout).toEqual(5000); + expect(requestClient.axios.defaults.httpsAgent.options.keepAlive).toBe(true); + expect(requestClient.axios.defaults.httpsAgent.options.keepAliveMsecs).toEqual(1500); + expect(requestClient.axios.defaults.httpsAgent.options.maxSockets).toEqual(100); + expect(requestClient.axios.defaults.httpsAgent.options.maxTotalSockets).toEqual(1000); + expect(requestClient.axios.defaults.httpsAgent.options.maxFreeSockets).toEqual(10); + expect(requestClient.axios.defaults.httpsAgent.options.scheduling).toEqual('fifo'); + }); + + it('should initialize custom https settings (some settings customized)', function() { + const requestClient = new RequestClientMock({ timeout: 5000, keepAlive: false, maxTotalSockets: 1500, scheduling: 'lifo'}); + expect(requestClient.defaultTimeout).toEqual(5000); + expect(requestClient.axios.defaults.headers.post).toEqual({ + 'Content-Type': 'application/x-www-form-urlencoded', + }); + expect(requestClient.axios.defaults.httpsAgent).not.toBeInstanceOf(HttpsProxyAgent); + expect(requestClient.axios.defaults.httpsAgent.options.timeout).toEqual(5000); + expect(requestClient.axios.defaults.httpsAgent.options.keepAlive).toBe(false); + expect(requestClient.axios.defaults.httpsAgent.options.keepAliveMsecs).toBe(undefined); + expect(requestClient.axios.defaults.httpsAgent.options.maxSockets).toBe(undefined); + expect(requestClient.axios.defaults.httpsAgent.options.maxTotalSockets).toEqual(1500); + expect(requestClient.axios.defaults.httpsAgent.options.maxFreeSockets).toBe(undefined); + expect(requestClient.axios.defaults.httpsAgent.options.scheduling).toEqual('lifo'); }); });