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

feat(http-client): retry closed connection errors #1336

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
12 changes: 10 additions & 2 deletions lib/StripeResource.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,15 @@ StripeResource.prototype = {
},

// For more on when and how to retry API requests, see https://stripe.com/docs/error-handling#safely-retrying-requests-with-idempotency
_shouldRetry(res, numRetries, maxRetries) {
_shouldRetry(res, numRetries, maxRetries, error) {
if (
error &&
numRetries === 0 &&
HttpClient.CONNECTION_CLOSED_ERROR_CODES.includes(error.code)
) {
return true;
}

// Do not retry if we are out of retries.
if (numRetries >= maxRetries) {
return false;
Expand Down Expand Up @@ -502,7 +510,7 @@ StripeResource.prototype = {
}
})
.catch((error) => {
if (this._shouldRetry(null, requestRetries, maxRetries)) {
if (this._shouldRetry(null, requestRetries, maxRetries, error)) {
return retryRequest(
makeRequest,
apiVersion,
Expand Down
1 change: 1 addition & 0 deletions lib/net/HttpClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class HttpClient {
}
}

HttpClient.CONNECTION_CLOSED_ERROR_CODES = ['ECONNRESET', 'EPIPE'];
HttpClient.TIMEOUT_ERROR_CODE = 'ETIMEDOUT';

class HttpClientResponse {
Expand Down
33 changes: 33 additions & 0 deletions test/StripeResource.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,39 @@ describe('StripeResource', () => {
);
});

it('retries closed connection errors once', (done) => {
nock(`https://${options.host}`)
.post(options.path, options.params)
.replyWithError({
code: 'ECONNRESET',
errno: 'ECONNRESET',
})
.post(options.path, options.params)
.reply(200, {
id: 'ch_123',
object: 'charge',
amount: 1000,
});

realStripe.charges.create(options.data, (err, charge) => {
expect(charge.id).to.equal('ch_123');
done(err);
});
});

it('throws on multiple closed connection errors', (done) => {
nock(`https://${options.host}`)
.post(options.path, options.params)
.replyWithError({code: 'ECONNRESET'})
.post(options.path, options.params)
.replyWithError({code: 'ECONNRESET'});

realStripe.charges.create(options.data, (err) => {
expect(err.detail.code).to.deep.equal('ECONNRESET');
done();
});
});

it('should retry the request if max retries are set', (done) => {
nock(`https://${options.host}`)
.post(options.path, options.params)
Expand Down