Skip to content

Commit c7d8de0

Browse files
committed
Merge pull request #29 from amplitude/fix_batching
Add check to prevent scheduling multiple uploads
2 parents ac5dd43 + 1495d77 commit c7d8de0

File tree

5 files changed

+60
-5
lines changed

5 files changed

+60
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
* Add support for passing callback function to init.
44
* Fix bug to check that Window localStorage is available for use.
5+
* Fix bug to prevent scheduling multiple event uploads.
56

67
## 2.4.0 (September 4, 2015)
78

amplitude.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ Amplitude.prototype._sending = false;
165165
Amplitude.prototype._lastEventTime = null;
166166
Amplitude.prototype._sessionId = null;
167167
Amplitude.prototype._newSession = false;
168+
Amplitude.prototype._updateScheduled = false;
168169

169170
/**
170171
* Initializes Amplitude.
@@ -282,7 +283,16 @@ Amplitude.prototype._sendEventsIfReady = function(callback) {
282283
return true;
283284
}
284285

285-
setTimeout(this.sendEvents.bind(this), this.options.eventUploadPeriodMillis);
286+
if (!this._updateScheduled) {
287+
this._updateScheduled = true;
288+
setTimeout(
289+
function() {
290+
this._updateScheduled = false;
291+
this.sendEvents();
292+
}.bind(this), this.options.eventUploadPeriodMillis
293+
);
294+
}
295+
286296
return false;
287297
};
288298

amplitude.min.js

Lines changed: 2 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: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ Amplitude.prototype._sending = false;
5353
Amplitude.prototype._lastEventTime = null;
5454
Amplitude.prototype._sessionId = null;
5555
Amplitude.prototype._newSession = false;
56+
Amplitude.prototype._updateScheduled = false;
5657

5758
/**
5859
* Initializes Amplitude.
@@ -170,7 +171,16 @@ Amplitude.prototype._sendEventsIfReady = function(callback) {
170171
return true;
171172
}
172173

173-
setTimeout(this.sendEvents.bind(this), this.options.eventUploadPeriodMillis);
174+
if (!this._updateScheduled) {
175+
this._updateScheduled = true;
176+
setTimeout(
177+
function() {
178+
this._updateScheduled = false;
179+
this.sendEvents();
180+
}.bind(this), this.options.eventUploadPeriodMillis
181+
);
182+
}
183+
174184
return false;
175185
};
176186

test/amplitude.js

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,41 @@ describe('Amplitude', function() {
387387
assert.lengthOf(amplitude._unsentEvents, 0);
388388
clock.tick(eventUploadPeriodMillis);
389389
assert.lengthOf(server.requests, 1);
390-
})
390+
});
391+
392+
it('should not schedule more than one upload', function() {
393+
var eventUploadPeriodMillis = 5*1000; // 5s
394+
amplitude.init(apiKey, null, {
395+
batchEvents: true,
396+
eventUploadThreshold: 30,
397+
eventUploadPeriodMillis: eventUploadPeriodMillis
398+
});
399+
400+
// log 2 events, 1 millisecond apart, second event should not schedule upload
401+
amplitude.logEvent('Event1');
402+
clock.tick(1);
403+
amplitude.logEvent('Event2');
404+
assert.lengthOf(amplitude._unsentEvents, 2);
405+
assert.lengthOf(server.requests, 0);
406+
407+
// advance to upload period millis, and should have 1 server request
408+
// from the first scheduled upload
409+
clock.tick(eventUploadPeriodMillis-1);
410+
assert.lengthOf(server.requests, 1);
411+
server.respondWith('success');
412+
server.respond();
413+
414+
// log 3rd event, advance 1 more millisecond, verify no 2nd server request
415+
amplitude.logEvent('Event3');
416+
clock.tick(1);
417+
assert.lengthOf(server.requests, 1);
418+
419+
// the 3rd event, however, should have scheduled another upload after 5s
420+
clock.tick(eventUploadPeriodMillis-2);
421+
assert.lengthOf(server.requests, 1);
422+
clock.tick(1);
423+
assert.lengthOf(server.requests, 2);
424+
});
391425

392426
it('should back off on 413 status', function() {
393427
amplitude.init(apiKey, null, {uploadBatchSize: 10});

0 commit comments

Comments
 (0)