Skip to content

Commit

Permalink
Enable retry/exponential backoff for rawStream requests
Browse files Browse the repository at this point in the history
Signed-off-by: Vartan Benohanian <vartanbeno@gmail.com>
  • Loading branch information
vartanbeno committed Apr 15, 2020
1 parent 11eff72 commit 8d860ef
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 18 deletions.
1 change: 1 addition & 0 deletions history.md
@@ -1,6 +1,7 @@
# v2.2.8 - 2020/04/03

* Added support for socket options for player-service socket.
* Use retry/exponential backoff mechanisms for rawStream requests.

# v2.2.7 - 2020/02/19

Expand Down
35 changes: 18 additions & 17 deletions lib/request.js
Expand Up @@ -144,10 +144,10 @@ module.exports = (function (self) {
}

if (options.totalTimeout) {
const timeElapsed = (new Date()) - startTime;
const timeElapsed = new Date() - startTime;
return timeElapsed < options.totalTimeout;
}

return true;
}

Expand Down Expand Up @@ -297,6 +297,19 @@ module.exports = (function (self) {
HTTP_REDIRECT_NEW_CODE_TEMP
].some((code) => (code === context.statusCode));

const retry = context.statusCode >= HTTP_ERROR_CODE_RETRY_THRESHHOLD
&& isRetry(options, startTime, tryCount);

// handle retry if error code is above threshhold
if (retry) {
return delay(retryWait)
.then(() => {
tryCount++;
retryWait *= EXPONENT;
})
.then(makeRequest);
}

// provide response event (as there are response headers)
if (_this.emit) {
_this.emit(EVENT_RESPONSE, context);
Expand Down Expand Up @@ -372,18 +385,6 @@ module.exports = (function (self) {

res.once('end', () => {
let body = chunks.join('');
const retry = context.statusCode >= HTTP_ERROR_CODE_RETRY_THRESHHOLD &&
isRetry(options, startTime, tryCount);

// handle retry if error code is above threshhold
if (retry) {
tryCount += 1;
return delay(retryWait)
.then(makeRequest)
.then(() => {
retryWait *= EXPONENT;
});
}

// attempt to parse the body
if (typeof body === 'string' && body.length) {
Expand Down Expand Up @@ -421,12 +422,12 @@ module.exports = (function (self) {
req.on('error', (err) => {
// retry if below retry count threshhold
if (isRetry(options, startTime, tryCount)) {
tryCount += 1;
return delay(retryWait)
.then(makeRequest)
.then(() => {
tryCount++;
retryWait *= EXPONENT;
});
})
.then(makeRequest);
}

return reject(err)
Expand Down
31 changes: 30 additions & 1 deletion test/lib/request.js
Expand Up @@ -508,12 +508,16 @@ describe('request', () => {
// capture request and response info
req.on('request', (info) => (requestInfo = info));

beforeEach(() => {
delete options.rawStream;
});

afterEach(function() {
nock.cleanAll();
requestInfo = undefined;
});
it('should properly retry on 500s and based on exponential backoff', function(done) {

it('should properly retry on 500s and based on exponential backoff', function(done) {
// intercept outbound request
let responseBody = { message : 'overload', statusCode : 503 };

Expand All @@ -537,6 +541,31 @@ describe('request', () => {
return done();
});
});

it('should properly retry on 500s and based on exponential backoff for rawStream requests', function (done) {
options.rawStream = true;

// intercept outbound request
let responseBody = { message : 'overload', statusCode : 503 };

// fail twice
nock(`https://${options.host}`)[method]('/v0/tests/retry')
.times(2)
.reply(responseBody.statusCode, responseBody);

// succeed on 3rd retry
nock(`https://${options.host}`)[method]('/v0/tests/retry')
.times(1)
.reply(200);

return req[method](
{ path : '/v0/tests/retry' },
function (err, response) {
should.not.exist(err);

return done();
});
});
});
});
});
Expand Down

0 comments on commit 8d860ef

Please sign in to comment.