Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Network retries #559

Merged
merged 21 commits into from
Feb 14, 2019
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 81 additions & 6 deletions lib/StripeResource.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
var http = require('http');
var https = require('https');
var path = require('path');
var uuid = require('uuid/v4');

var utils = require('./utils');
var Error = require('./Error');
Expand Down Expand Up @@ -182,7 +183,11 @@ StripeResource.prototype = {
};
},

_errorHandler: function(req, callback) {
_generateConnectionErrorMessage: function(requestRetries) {
return 'An error occurred with our connection to Stripe.' + (requestRetries > 0 ? ' Request was retried ' + requestRetries + ' times.' : '');
},

_errorHandler: function(req, requestRetries, callback) {
var self = this;
return function(error) {
if (req._isAborted) {
Expand All @@ -192,14 +197,49 @@ StripeResource.prototype = {
callback.call(
self,
new Error.StripeConnectionError({
message: 'An error occurred with our connection to Stripe',
message: self._generateConnectionErrorMessage(requestRetries),
paulasjes-stripe marked this conversation as resolved.
Show resolved Hide resolved
detail: error,
}),
null
);
}
},

_shouldRetry: function(res, numRetries) {
// Retry on conflict or rate limit error, if we have retries left.
if (res && (res.statusCode === 409 || res.statusCode === 429)) {
paulasjes-stripe marked this conversation as resolved.
Show resolved Hide resolved
return numRetries < this._stripe.getMaxNetworkRetries();
}

// Retry on connection error, if we have retries left.
if (!res && (numRetries < this._stripe.getMaxNetworkRetries())) {
return true;
paulasjes-stripe marked this conversation as resolved.
Show resolved Hide resolved
}

paulasjes-stripe marked this conversation as resolved.
Show resolved Hide resolved
return false;
},

_getSleepTime: function(numRetries) {
var initialNetworkRetryDelay = this._stripe.getInitialNetworkRetryDelay();
var maxNetworkRetryDelay = this._stripe.getMaxNetworkRetryDelay();

// Apply exponential backoff with initialNetworkRetryDelay on the
// number of numRetries so far as inputs. Do not allow the number to exceed
// maxNetworkRetryDelay.
var sleepSeconds = Math.min(
initialNetworkRetryDelay * Math.pow(numRetries - 1, 2),
maxNetworkRetryDelay
);

// Apply some jitter.
sleepSeconds *= 0.5 * (1 + Math.random());
paulasjes-stripe marked this conversation as resolved.
Show resolved Hide resolved

// Never sleep more than the max allowed.
sleepSeconds = Math.max(initialNetworkRetryDelay, sleepSeconds);
paulasjes-stripe marked this conversation as resolved.
Show resolved Hide resolved

return sleepSeconds;
},

_defaultHeaders: function(auth, contentLength, apiVersion) {
var userAgentString = 'Stripe/v1 NodeBindings/' + this._stripe.getConstant('PACKAGE_VERSION');

Expand Down Expand Up @@ -258,7 +298,19 @@ StripeResource.prototype = {
makeRequestWithData(null, utils.stringifyRequestData(data || {}));
}

function makeRequest(apiVersion, headers) {
function retryRequest(requestFn, apiVersion, headers, requestRetries) {
requestRetries += 1;

return setTimeout(
requestFn,
self._getSleepTime(requestRetries) * 1000,
paulasjes-stripe marked this conversation as resolved.
Show resolved Hide resolved
apiVersion,
headers,
requestRetries
);
}

function makeRequest(apiVersion, headers, numRetries) {
var timeout = self._stripe.getApiField('timeout');
var isInsecureConnection = self._stripe.getApiField('protocol') == 'http';

Expand All @@ -274,6 +326,14 @@ StripeResource.prototype = {
ciphers: 'DEFAULT:!aNULL:!eNULL:!LOW:!EXPORT:!SSLv2:!MD5',
});

// If this is a POST and we allow multiple retries, set a idempotency key if one is not
// already provided.
ob-stripe marked this conversation as resolved.
Show resolved Hide resolved
if (method === 'POST' && self._stripe.getMaxNetworkRetries() > 0) {
if (!headers.hasOwnProperty('Idempotency-Key')) {
headers['Idempotency-Key'] = uuid();
}
}

var requestEvent = utils.removeEmpty({
api_version: apiVersion,
account: headers['Stripe-Account'],
Expand All @@ -282,15 +342,31 @@ StripeResource.prototype = {
path: path,
});

var requestRetries = numRetries || 0;

req._requestEvent = requestEvent;

req._requestStart = Date.now();

self._stripe._emitter.emit('request', requestEvent);

req.setTimeout(timeout, self._timeoutHandler(timeout, req, callback));
req.on('response', self._responseHandler(req, callback));
req.on('error', self._errorHandler(req, callback));

req.on('response', function(res) {
paulasjes-stripe marked this conversation as resolved.
Show resolved Hide resolved
if (self._shouldRetry(res, requestRetries)) {
retryRequest(makeRequest, apiVersion, headers, requestRetries);
} else {
return self._responseHandler(req, callback)(res);
}
});

req.on('error', function(error) {
if (self._shouldRetry(null, requestRetries)) {
retryRequest(makeRequest, apiVersion, headers, requestRetries);
paulasjes-stripe marked this conversation as resolved.
Show resolved Hide resolved
} else {
return self._errorHandler(req, requestRetries, callback)(error);
}
});

req.on('socket', function(socket) {
if (socket.connecting) {
Expand All @@ -307,7 +383,6 @@ StripeResource.prototype = {
});
}
},

};

module.exports = StripeResource;
24 changes: 24 additions & 0 deletions lib/stripe.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ Stripe.USER_AGENT = {

Stripe.USER_AGENT_SERIALIZED = null;

Stripe.MAX_NETWORK_RETRY_DELAY = 2;
Stripe.INITIAL_NETWORK_RETRY_DELAY = 0.5;
paulasjes-stripe marked this conversation as resolved.
Show resolved Hide resolved

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

var EventEmitter = require('events').EventEmitter;
Expand Down Expand Up @@ -142,6 +145,7 @@ function Stripe(key, version) {
timeout: Stripe.DEFAULT_TIMEOUT,
agent: null,
dev: false,
maxNetworkRetries: 0,
};

this._prepResources();
Expand Down Expand Up @@ -247,6 +251,26 @@ Stripe.prototype = {
return Stripe[c];
},

getMaxNetworkRetries: function() {
return this.getApiField('maxNetworkRetries');
},

setMaxNetworkRetries: function(maxNetworkRetries) {
if ((maxNetworkRetries && typeof maxNetworkRetries !== 'number') || arguments.length < 1) {
throw new Error('maxNetworkRetries must be a number.');
}

this._setApiField('maxNetworkRetries', maxNetworkRetries);
},

getMaxNetworkRetryDelay: function() {
return this.getConstant('MAX_NETWORK_RETRY_DELAY');
},

getInitialNetworkRetryDelay: function() {
return this.getConstant('INITIAL_NETWORK_RETRY_DELAY');
},

// Gets a JSON version of a User-Agent and uses a cached version for a slight
// speed advantage.
getClientUserAgent: function(cb) {
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@
"eslint": "^4.19.1",
"eslint-plugin-chai-friendly": "^0.4.0",
"mocha": "~5.0.5",
"nock": "^9.0.0",
"nyc": "^11.3.0"
},
"dependencies": {
"lodash.isplainobject": "^4.0.6",
"qs": "~6.5.1",
"safe-buffer": "^5.1.1"
"safe-buffer": "^5.1.1",
"uuid": "^3.3.2"
},
"license": "MIT",
"scripts": {
Expand Down
Loading