Skip to content

Commit

Permalink
Fixes/3649 use same http agent for requests (#3748)
Browse files Browse the repository at this point in the history
  • Loading branch information
gravityvi committed May 26, 2023
1 parent c838606 commit 525e4c1
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 16 deletions.
56 changes: 40 additions & 16 deletions lib/http/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ const __defaultSettings__ = {

let __globalSettings__ = null;

let __httpKeepAliveAgent__ = null;

class HttpRequest extends EventEmitter {
static get USER_AGENT() {
const version = require('../../package.json').version;
Expand Down Expand Up @@ -54,6 +56,24 @@ class HttpRequest extends EventEmitter {
__globalSettings__ = val;
}

static resetHttpKeepAliveAgents() {
__httpKeepAliveAgent__ = null;
}

static getAgent({secure, keepAliveMsecs, maxSockets}) {
if (__httpKeepAliveAgent__) {
return __httpKeepAliveAgent__;
}

const protocol = secure ? https : http;

return __httpKeepAliveAgent__ = new protocol.Agent({
keepAlive: true,
keepAliveMsecs,
maxSockets
});
}

static get globalSettings() {
return __globalSettings__ || HttpOptions.global.settings;
}
Expand Down Expand Up @@ -128,6 +148,25 @@ class HttpRequest extends EventEmitter {
return this;
}

addKeepAliveOptions(reqOptions) {
if (this.httpOpts.keep_alive) {
let keepAliveMsecs = 3000;
let enabled = true;
if (Utils.isObject(this.httpOpts.keep_alive)) {
keepAliveMsecs = Number(this.httpOpts.keep_alive.keepAliveMsecs);
enabled = JSON.parse(this.httpOpts.keep_alive.enabled);
}

if (enabled) {
reqOptions.agent = HttpRequest.getAgent({
secure: this.httpOpts.port === 443,
keepAliveMsecs,
maxSockets: 1
});
}
}
}

createHttpOptions(options) {
const reqOptions = {
path: this.defaultPathPrefix + (options.path || ''),
Expand All @@ -147,22 +186,7 @@ class HttpRequest extends EventEmitter {

this.auth = options.auth || null;

if (this.httpOpts.keep_alive) {
let keepAliveMsecs = 3000;
let enabled = true;
if (Utils.isObject(this.httpOpts.keep_alive)) {
keepAliveMsecs = Number(this.httpOpts.keep_alive.keepAliveMsecs);
enabled = JSON.parse(this.httpOpts.keep_alive.enabled);
}

if (enabled) {
reqOptions.agent = new (reqOptions.port === 443 ? https: http)['Agent']({
keepAlive: enabled,
maxSockets: 1,
keepAliveMsecs
});
}
}
this.addKeepAliveOptions(reqOptions);

if (options.sessionId) {
reqOptions.path = reqOptions.path.replace(':sessionId', options.sessionId);
Expand Down
33 changes: 33 additions & 0 deletions test/src/index/testRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ describe('test HttpRequest', function() {
});

afterEach(function () {
HttpRequest.resetHttpKeepAliveAgents();
mockery.deregisterAll();
mockery.resetCache();
mockery.disable();
Expand Down Expand Up @@ -287,6 +288,38 @@ describe('test HttpRequest', function() {
assert.ok('agent' in opts);
});

it('keep alive uses single instance of agent across requests', function() {
const options = {
path: '/session',
method: 'POST',
port: 4444,
data: {
desiredCapabilities: {
browserName: 'firefox'
}
}
};

HttpRequest.globalSettings = {
default_path: '/wd/hub',
port: 4444,
keep_alive: {
keepAliveMsecs: 1000,
enabled: true
}
};

const request = new HttpRequest(options);
const secondRequest = new HttpRequest(options);

const opts = request.reqOptions;
const http = require('http');
assert.ok(opts.agent instanceof http.Agent);
assert.strictEqual(opts.agent.keepAliveMsecs, 1000);
assert.ok('agent' in opts);
assert.strictEqual(secondRequest.reqOptions.agent, opts.agent);
});

it('test send post request with keep alive extended - disabled', function (done) {
const options = {
path: '/session',
Expand Down

0 comments on commit 525e4c1

Please sign in to comment.