Skip to content

Commit

Permalink
feat(http-client): Retry requests that failed with closed connection
Browse files Browse the repository at this point in the history
Requests that fail with closed connection errors (ECONNRESET, EPIPE) are
automatically retried.

- `ECONNRESET` (Connection reset by peer): A connection was forcibly
  closed by a peer.closed by a peer. This normally results from a loss
  of the connection on the remote socket due to a timeout or reboot.
  Commonly encountered via the http and net modules.

- `EPIPE` (Broken pipe): A write on a pipe, socket, or FIFO for which
  there is no process to read the data. Commonly encountered at the net
  and http layers, indicative that the remote side of the stream being
  written to has been closed.

Fixes: stripe#1040
  • Loading branch information
bpinto committed Jan 21, 2022
1 parent 87d1aba commit e69f78f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
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 @@ -356,6 +356,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

0 comments on commit e69f78f

Please sign in to comment.