Skip to content

Commit

Permalink
Merge pull request #560 from stripe/ob-persistent-connections
Browse files Browse the repository at this point in the history
Enable persistent connections by default
  • Loading branch information
ob-stripe authored Feb 4, 2019
2 parents db420f3 + d210b7d commit c072d87
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 5 deletions.
3 changes: 2 additions & 1 deletion lib/StripeResource.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ StripeResource.prototype = {
function makeRequest(apiVersion, headers) {
var timeout = self._stripe.getApiField('timeout');
var isInsecureConnection = self._stripe.getApiField('protocol') == 'http';
var agent = isInsecureConnection ? self._stripe.getApiField('http_agent') : self._stripe.getApiField('https_agent');

var req = (
isInsecureConnection ? http : https
Expand All @@ -300,7 +301,7 @@ StripeResource.prototype = {
port: self._stripe.getApiField('port'),
path: path,
method: method,
agent: self._stripe.getApiField('agent'),
agent: agent,
headers: headers,
ciphers: 'DEFAULT:!aNULL:!eNULL:!LOW:!EXPORT:!SSLv2:!MD5',
});
Expand Down
17 changes: 15 additions & 2 deletions lib/stripe.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ Stripe.USER_AGENT_SERIALIZED = null;

var APP_INFO_PROPERTIES = ['name', 'version', 'url', 'partner_id'];

var http = require('http');
var https = require('https');

var EventEmitter = require('events').EventEmitter;
var exec = require('child_process').exec;
var utils = require('./utils');
Expand Down Expand Up @@ -140,7 +143,8 @@ function Stripe(key, version) {
basePath: Stripe.DEFAULT_BASE_PATH,
version: Stripe.DEFAULT_API_VERSION,
timeout: Stripe.DEFAULT_TIMEOUT,
agent: null,
http_agent: this._buildDefaultAgent('http'),
https_agent: this._buildDefaultAgent('https'),
dev: false,
};

Expand Down Expand Up @@ -227,7 +231,11 @@ Stripe.prototype = {
},

setHttpAgent: function(agent) {
this._setApiField('agent', agent);
if (agent instanceof https.Agent) {
this._setApiField('https_agent', agent);
} else {
this._setApiField('http_agent', agent);
}
},

_setApiField: function(key, value) {
Expand Down Expand Up @@ -310,6 +318,11 @@ Stripe.prototype = {
return this._enableTelemetry;
},

_buildDefaultAgent: function(protocol) {
var httpLib = protocol === 'http' ? http : https;
return new httpLib.Agent({keepAlive: true});
},

_prepResources: function() {
for (var name in resources) {
this[utils.pascalToCamelCase(name)] = new resources[name](this);
Expand Down
37 changes: 35 additions & 2 deletions test/stripe.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ var stripe = require('../lib/stripe')(
'latest'
);

var http = require('http');
var https = require('https');

var expect = require('chai').expect;

var CUSTOMER_DETAILS = {
Expand All @@ -23,6 +26,36 @@ describe('Stripe Module', function() {
});
});

describe('setHttpAgent', function() {
var origHttpAgent, origHttpsAgent;
beforeEach(function() {
origHttpAgent = stripe.getApiField('http_agent');
origHttpsAgent = stripe.getApiField('https_agent');
stripe._setApiField('http_agent', null);
stripe._setApiField('https_agent', null);
});
afterEach(function() {
stripe._setApiField('http_agent', origHttpAgent);
stripe._setApiField('https_agent', origHttpsAgent);
});
describe('when given an https.Agent', function() {
it('should save the agent as https_agent', function() {
var agent = new https.Agent();
stripe.setHttpAgent(agent);
expect(stripe.getApiField('https_agent')).to.equal(agent);
expect(stripe.getApiField('http_agent')).to.be.null;
});
});
describe('when given an http.Agent', function() {
it('should save the agent as http_agent', function() {
var agent = new http.Agent();
stripe.setHttpAgent(agent);
expect(stripe.getApiField('http_agent')).to.equal(agent);
expect(stripe.getApiField('https_agent')).to.be.null;
});
});
});

describe('GetClientUserAgent', function() {
it('Should return a user-agent serialized JSON object', function() {
return expect(new Promise(function(resolve, reject) {
Expand Down Expand Up @@ -55,15 +88,15 @@ describe('Stripe Module', function() {

describe('setTimeout', function() {
it('Should define a default equal to the node default', function() {
expect(stripe.getApiField('timeout')).to.equal(require('http').createServer().timeout);
expect(stripe.getApiField('timeout')).to.equal(http.createServer().timeout);
});
it('Should allow me to set a custom timeout', function() {
stripe.setTimeout(900);
expect(stripe.getApiField('timeout')).to.equal(900);
});
it('Should allow me to set null, to reset to the default', function() {
stripe.setTimeout(null);
expect(stripe.getApiField('timeout')).to.equal(require('http').createServer().timeout);
expect(stripe.getApiField('timeout')).to.equal(http.createServer().timeout);
});
});

Expand Down

0 comments on commit c072d87

Please sign in to comment.