Skip to content
This repository was archived by the owner on Feb 13, 2024. It is now read-only.

Commit aeda99e

Browse files
authored
Merge pull request #277 from North-Two-Five/payload-size
flushing when payload max size has reached
2 parents d0e0f04 + 2fd5195 commit aeda99e

File tree

6 files changed

+733
-457
lines changed

6 files changed

+733
-457
lines changed

.circleci/config.yml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ workflows:
55
- circle-lint
66
multi-test:
77
jobs:
8-
- test-node8
98
- test-node10
109
- test-node12
1110
test_and_publish:
@@ -43,7 +42,6 @@ workflows:
4342
- master
4443
- scheduled_e2e_testing
4544
jobs:
46-
- test-node8
4745
- test-node10
4846
- test-node12
4947

@@ -74,10 +72,6 @@ jobs:
7472
root: .
7573
paths: [.]
7674

77-
test-node8:
78-
<<: *node-base-test
79-
docker:
80-
- image: circleci/node:8-browsers
8175
test-node10:
8276
<<: *node-base-test
8377
docker:

index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class Analytics {
4545
this.axiosInstance = axiosInstance
4646
this.timeout = options.timeout || false
4747
this.flushAt = Math.max(options.flushAt, 1) || 20
48+
this.maxQueueSize = options.maxQueueSize || 1024 * 500 // defaults to 500kb
4849
this.flushInterval = options.flushInterval || 10000
4950
this.flushed = false
5051
Object.defineProperty(this, 'enable', {
@@ -208,7 +209,9 @@ class Analytics {
208209
return
209210
}
210211

211-
if (this.queue.length >= this.flushAt) {
212+
const hasReachedFlushAt = this.queue.length >= this.flushAt
213+
const hasReachedQueueSize = this.queue.reduce((acc, item) => acc + JSON.stringify(item).length, 0) >= this.maxQueueSize
214+
if (hasReachedFlushAt || hasReachedQueueSize) {
212215
this.flush(callback)
213216
}
214217

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"circle-lint": ".buildscript/circle.sh",
1717
"dependencies": "yarn",
1818
"test": "standard && nyc ava --timeout=20s&& .buildscript/e2e.sh",
19+
"coverage": "nyc npm run test",
1920
"report-coverage": "nyc report --reporter=lcov > coverage.lcov && codecov",
2021
"np": "np --no-publish",
2122
"release": "yarn run np"
@@ -46,12 +47,12 @@
4647
"ava": "^0.25.0",
4748
"basic-auth": "^2.0.1",
4849
"body-parser": "^1.17.1",
49-
"codecov": "^3.0.0",
50+
"codecov": "^3.8.1",
5051
"commander": "^2.9.0",
5152
"delay": "^4.2.0",
5253
"express": "^4.15.2",
5354
"husky": "^3.0.4",
54-
"nyc": "^14.1.1",
55+
"nyc": "^15.1.0",
5556
"pify": "^4.0.1",
5657
"sinon": "^7.3.2",
5758
"snyk": "^1.171.1",

sample.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const os = require('os')
2+
const uuid = require('uuid')
3+
const Analytics = require('.')
4+
const analytics = new Analytics('xemyw6oe3n')
5+
6+
for (let i = 0; i < 10; i++) {
7+
for (let j = 0; j < 10; j++) {
8+
analytics.track({
9+
anonymousId: uuid.v4(),
10+
userId: os.userInfo().username,
11+
event: 'Node Test',
12+
properties: {
13+
count: i + j
14+
}
15+
})
16+
}
17+
}
18+
19+
analytics.flush()

test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,38 @@ test('flush - skip when client is disabled', async t => {
375375
t.false(callback.called)
376376
})
377377

378+
test('flush - flush when reaches max payload size', async t => {
379+
const client = createClient({ flushAt: 1000 })
380+
client.flush = spy()
381+
382+
// each of these messages when stringified to json has 220-ish bytes
383+
// to satisfy our default limit of 1024*500 bytes we need less than 2600 of those messages
384+
const event = {
385+
userId: 1,
386+
event: 'event'
387+
}
388+
for (let i = 0; i < 2600; i++) {
389+
client.track(event)
390+
}
391+
392+
t.true(client.flush.called)
393+
})
394+
395+
test('flush - wont flush when no flush condition has meet', async t => {
396+
const client = createClient({ flushAt: 1000, maxQueueSize: 1024 * 1000 })
397+
client.flush = spy()
398+
399+
const event = {
400+
userId: 1,
401+
event: 'event'
402+
}
403+
for (let i = 0; i < 150; i++) {
404+
client.track(event)
405+
}
406+
407+
t.false(client.flush.called)
408+
})
409+
378410
test('identify - enqueue a message', t => {
379411
const client = createClient()
380412
stub(client, 'enqueue')

0 commit comments

Comments
 (0)