Skip to content

Commit

Permalink
Merge 8d860ef into aec4d03
Browse files Browse the repository at this point in the history
  • Loading branch information
vartanbeno committed Apr 15, 2020
2 parents aec4d03 + 8d860ef commit bc37237
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 28 deletions.
5 changes: 5 additions & 0 deletions history.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# 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

* Added backoff mechanism to any request called with setting totalTimeout.
Expand Down
18 changes: 14 additions & 4 deletions lib/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ module.exports = function (playerOptions, ensureAuthHeaders, self) {
playerOptions.secure;

if (settings.secure) {
protocol = 'https'
protocol = 'https';
} else {
protocol = 'http';
}
Expand All @@ -62,6 +62,11 @@ module.exports = function (playerOptions, ensureAuthHeaders, self) {
throw new Error('socketEventSubscriber must be an instance of event.EventEmitter');
}

let socketOptions = options.socket;
if (validation.isEmpty(options.socket)) {
socketOptions = {};
}

let
authHeaders = yield ensureAuthHeaders(),
authToken,
Expand All @@ -75,7 +80,8 @@ module.exports = function (playerOptions, ensureAuthHeaders, self) {
url = [protocol, '://', options.host || settings.host].join('');
query = ['clientId=', clientId, '&token=', authToken].join('');

socket = new io(url, { 'query' : query});
socketOptions.query = query;
socket = new io(url, socketOptions);

// fired after a successful connection
socket.on('connect', () => {
Expand All @@ -98,10 +104,14 @@ module.exports = function (playerOptions, ensureAuthHeaders, self) {
});

socket.on('error', (err) => {
err = JSON.parse(err);
try {
err = JSON.parse(err);
} catch (e) {
err = e;
}
socketEventSubscriber.emit('error', err);

if (err.code === AUTH_ERROR) {
if (err.code === AUTH_ERROR) {
socket.disconnect();

// reconnect if failed during authorization
Expand Down
45 changes: 24 additions & 21 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ var
qs = require('querystring'),
url = require('url'),
util = require('util'),

validation = require('./validation');

const
Expand Down Expand Up @@ -140,15 +139,16 @@ module.exports = (function (self) {
}

function isRetry(options, startTime, tryCount) {
let retry;
if (tryCount > options.maxRetries) {
return false;
}

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

return retry;
return true;
}

function Request (settings) {
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,16 +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;
retryWait = retryWait * EXPONENT;
return delay(retryWait)
.then(makeRequest);
}

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

return reject(err)
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "playnetwork-sdk",
"version": "2.2.7",
"version": "2.2.8",
"contributors": [
{
"name": "Joshua Thomas",
Expand Down
31 changes: 30 additions & 1 deletion test/lib/request.js
Original file line number Diff line number Diff line change
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 bc37237

Please sign in to comment.