Skip to content

Commit f55cd08

Browse files
committed
Use Retry-After if available
1 parent bfba649 commit f55cd08

File tree

2 files changed

+50
-4
lines changed

2 files changed

+50
-4
lines changed

src/raven.js

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1326,6 +1326,36 @@ Raven.prototype = {
13261326
return this._backoffDuration && now() - this._backoffStart < this._backoffDuration;
13271327
},
13281328

1329+
_setBackoffState: function(request) {
1330+
// if we are already in a backoff state, don't change anything
1331+
if (this._shouldBackoff())
1332+
return;
1333+
1334+
var status = request.status;
1335+
1336+
// 400 - project_id doesn't exist or some other fatal
1337+
// 401 - nvalid/revoked dsn
1338+
// 429 - too many requests
1339+
if (!(status === 400 || status === 401 || status === 429))
1340+
return;
1341+
1342+
var retry;
1343+
try {
1344+
// If Retry-After is not in Access-Control-Allow-Headers, most
1345+
// browsers will throw an exception trying to access it
1346+
retry = request.getResponseHeader('Retry-After');
1347+
retry = parseInt(retry, 10);
1348+
} catch (e) {
1349+
/* eslint no-empty:0 */
1350+
}
1351+
1352+
this._backoffDuration = retry
1353+
? retry
1354+
: this._backoffDuration * 2 || 1000;
1355+
1356+
this._backoffStart = now();
1357+
},
1358+
13291359
_send: function(data) {
13301360
if (this._shouldBackoff()) {
13311361
return;
@@ -1453,10 +1483,8 @@ Raven.prototype = {
14531483
callback && callback();
14541484
},
14551485
onError: function failure(error) {
1456-
// too many requests
1457-
if (!self._shouldBackoff() && error.request && error.request.status === 429) {
1458-
self._backoffDuration = self._backoffDuration * 2 || 1000;
1459-
self._backoffStart = now();
1486+
if (error.request) {
1487+
self._setBackoffState(error.request);
14601488
}
14611489

14621490
self._triggerEvent('failure', {

test/raven.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,6 +1193,24 @@ describe('globals', function() {
11931193
assert.equal(Raven._backoffDuration, 2000);
11941194
});
11951195

1196+
1197+
it('should set backoffDuration to value of Retry-If header if present', function () {
1198+
this.sinon.stub(Raven, 'isSetup').returns(true);
1199+
this.sinon.stub(Raven, '_makeRequest');
1200+
1201+
Raven._send({message: 'bar'});
1202+
var opts = Raven._makeRequest.lastCall.args[0];
1203+
var mockError = new Error('401: Unauthorized');
1204+
mockError.request = {
1205+
status: 401,
1206+
getResponseHeader: sinon.stub().withArgs('Retry-After').returns('1337')
1207+
};
1208+
opts.onError(mockError);
1209+
1210+
assert.equal(Raven._backoffStart, 100); // clock is at 100ms
1211+
assert.equal(Raven._backoffDuration, 1337); // converted to int
1212+
});
1213+
11961214
it('should reset backoffDuration and backoffStart if onSuccess is fired (200)', function () {
11971215
this.sinon.stub(Raven, 'isSetup').returns(true);
11981216
this.sinon.stub(Raven, '_makeRequest');

0 commit comments

Comments
 (0)