Skip to content

Commit 0aa4b5e

Browse files
committed
Put floor on exponential backoff
1 parent fd32547 commit 0aa4b5e

File tree

4 files changed

+36
-4
lines changed

4 files changed

+36
-4
lines changed

amplitude.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -537,9 +537,14 @@ Amplitude.prototype.sendEvents = function() {
537537
}
538538
} else if (status === 413) {
539539
//log('request too large');
540+
// Can't even get this one massive event through. Drop it.
541+
if (scope.options.uploadBatchSize === 1) {
542+
scope.removeEvents(maxEventId);
543+
}
544+
540545
// The server complained about the length of the request.
541546
// Backoff and try again.
542-
scope.options.uploadBatchSize = Math.floor(numEvents / 2);
547+
scope.options.uploadBatchSize = Math.ceil(numEvents / 2);
543548
scope.sendEvents();
544549
}
545550
} catch (e) {

amplitude.min.js

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/amplitude.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,9 +425,14 @@ Amplitude.prototype.sendEvents = function() {
425425
}
426426
} else if (status === 413) {
427427
//log('request too large');
428+
// Can't even get this one massive event through. Drop it.
429+
if (scope.options.uploadBatchSize === 1) {
430+
scope.removeEvents(maxEventId);
431+
}
432+
428433
// The server complained about the length of the request.
429434
// Backoff and try again.
430-
scope.options.uploadBatchSize = Math.floor(numEvents / 2);
435+
scope.options.uploadBatchSize = Math.ceil(numEvents / 2);
431436
scope.sendEvents();
432437
}
433438
} catch (e) {

test/amplitude.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,27 @@ describe('Amplitude', function() {
313313
assert.deepEqual(events[0].event_properties, {index: 0});
314314
assert.deepEqual(events[4].event_properties, {index: 4});
315315
});
316+
317+
it('should back off on 413 status all the way to 1 event with drops', function() {
318+
amplitude.init(apiKey, null, {uploadBatchSize: 9});
319+
320+
amplitude._sending = true;
321+
for (var i = 0; i < 10; i++) {
322+
amplitude.logEvent('Event', {index: i});
323+
}
324+
amplitude._sending = false;
325+
amplitude.logEvent('Event', {index: 100});
326+
327+
for (var i = 0; i < 6; i++) {
328+
assert.lengthOf(server.requests, i+1);
329+
server.respondWith([413, {}, ""]);
330+
server.respond();
331+
}
332+
333+
var events = JSON.parse(querystring.parse(server.requests[6].requestBody).e);
334+
assert.lengthOf(events, 1);
335+
assert.deepEqual(events[0].event_properties, {index: 2});
336+
});
316337
});
317338

318339
describe('optOut', function() {

0 commit comments

Comments
 (0)