Skip to content

Commit

Permalink
Merge pull request segmentio#277 from North-Two-Five/payload-size
Browse files Browse the repository at this point in the history
flushing when payload max size has reached
  • Loading branch information
pooyaj committed Jun 23, 2021
2 parents d0e0f04 + 2fd5195 commit aeda99e
Show file tree
Hide file tree
Showing 6 changed files with 733 additions and 457 deletions.
6 changes: 0 additions & 6 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ workflows:
- circle-lint
multi-test:
jobs:
- test-node8
- test-node10
- test-node12
test_and_publish:
Expand Down Expand Up @@ -43,7 +42,6 @@ workflows:
- master
- scheduled_e2e_testing
jobs:
- test-node8
- test-node10
- test-node12

Expand Down Expand Up @@ -74,10 +72,6 @@ jobs:
root: .
paths: [.]

test-node8:
<<: *node-base-test
docker:
- image: circleci/node:8-browsers
test-node10:
<<: *node-base-test
docker:
Expand Down
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class Analytics {
this.axiosInstance = axiosInstance
this.timeout = options.timeout || false
this.flushAt = Math.max(options.flushAt, 1) || 20
this.maxQueueSize = options.maxQueueSize || 1024 * 500 // defaults to 500kb
this.flushInterval = options.flushInterval || 10000
this.flushed = false
Object.defineProperty(this, 'enable', {
Expand Down Expand Up @@ -208,7 +209,9 @@ class Analytics {
return
}

if (this.queue.length >= this.flushAt) {
const hasReachedFlushAt = this.queue.length >= this.flushAt
const hasReachedQueueSize = this.queue.reduce((acc, item) => acc + JSON.stringify(item).length, 0) >= this.maxQueueSize
if (hasReachedFlushAt || hasReachedQueueSize) {
this.flush(callback)
}

Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"circle-lint": ".buildscript/circle.sh",
"dependencies": "yarn",
"test": "standard && nyc ava --timeout=20s&& .buildscript/e2e.sh",
"coverage": "nyc npm run test",
"report-coverage": "nyc report --reporter=lcov > coverage.lcov && codecov",
"np": "np --no-publish",
"release": "yarn run np"
Expand Down Expand Up @@ -46,12 +47,12 @@
"ava": "^0.25.0",
"basic-auth": "^2.0.1",
"body-parser": "^1.17.1",
"codecov": "^3.0.0",
"codecov": "^3.8.1",
"commander": "^2.9.0",
"delay": "^4.2.0",
"express": "^4.15.2",
"husky": "^3.0.4",
"nyc": "^14.1.1",
"nyc": "^15.1.0",
"pify": "^4.0.1",
"sinon": "^7.3.2",
"snyk": "^1.171.1",
Expand Down
19 changes: 19 additions & 0 deletions sample.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const os = require('os')
const uuid = require('uuid')
const Analytics = require('.')
const analytics = new Analytics('xemyw6oe3n')

for (let i = 0; i < 10; i++) {
for (let j = 0; j < 10; j++) {
analytics.track({
anonymousId: uuid.v4(),
userId: os.userInfo().username,
event: 'Node Test',
properties: {
count: i + j
}
})
}
}

analytics.flush()
32 changes: 32 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,38 @@ test('flush - skip when client is disabled', async t => {
t.false(callback.called)
})

test('flush - flush when reaches max payload size', async t => {
const client = createClient({ flushAt: 1000 })
client.flush = spy()

// each of these messages when stringified to json has 220-ish bytes
// to satisfy our default limit of 1024*500 bytes we need less than 2600 of those messages
const event = {
userId: 1,
event: 'event'
}
for (let i = 0; i < 2600; i++) {
client.track(event)
}

t.true(client.flush.called)
})

test('flush - wont flush when no flush condition has meet', async t => {
const client = createClient({ flushAt: 1000, maxQueueSize: 1024 * 1000 })
client.flush = spy()

const event = {
userId: 1,
event: 'event'
}
for (let i = 0; i < 150; i++) {
client.track(event)
}

t.false(client.flush.called)
})

test('identify - enqueue a message', t => {
const client = createClient()
stub(client, 'enqueue')
Expand Down

0 comments on commit aeda99e

Please sign in to comment.