Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional backoff #33

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add optional max backoff argument
- Sometimes JS runs when wifi chip is off (during sleep) causing massive backoffs for no reason
  • Loading branch information
rtod committed Dec 22, 2020
commit dc90dee2c2c12318218bb309a619cdfecf1bbda5
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -19,7 +19,7 @@ Queue That is built primarily to queue XHR requests for reporting.

- When more than one tab is open, only one active queue will process tasks.
- If `options.process` calls back with an error, the tasks will be passed to `options.process`
again after a second. This backoff time doubles for each sequential error. The backoff timer is
again after a second. This backoff time doubles for each sequential error, up to `options.maxBackoff` (5 minutes by default). The backoff timer is

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we add this config option into the API documentation section below?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stored in localStorage and so will not reset when Queue That is reinitialised.
- There is a **2 second** timeout for the process function, after which the queue will back off and retry later, ignoring any success/error callback from the first `options.process` call.
- The batch array passed to `options.process` contains a `containsRepeatedItems` property, useful for deduplicating requests sent to a server.
10 changes: 9 additions & 1 deletion lib/queue-that.js
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@ var BACKOFF_TIME = 1000
var QUEUE_GROUP_TIME = 100
var PROCESS_TIMEOUT = 2000
var DEFAULT_BATCH_SIZE = 20
var DEFAULT_MAX_BACKOFF = 5 * 60 * 1000
var ACTIVE_QUEUE_TIMEOUT = 2500

module.exports = createQueueThat
@@ -20,6 +21,7 @@ function createQueueThat (options) {
options.trim = options.trim || identity
options.queueGroupTime = options.queueGroupTime || QUEUE_GROUP_TIME
options.backoffTime = options.backoffTime || BACKOFF_TIME
options.maxBackoff = options.maxBackoff || DEFAULT_MAX_BACKOFF
options.processTimeout = options.processTimeout || PROCESS_TIMEOUT
options.activeQueueTimeout = options.activeQueueTimeout || ACTIVE_QUEUE_TIMEOUT

@@ -166,7 +168,13 @@ function createQueueThat (options) {
log.error('Process error, backing off (' + err.message + ')')
var errorCount = storageAdapter.getErrorCount() + 1
storageAdapter.setErrorCount(errorCount)
storageAdapter.setBackoffTime(now() + options.backoffTime * Math.pow(2, errorCount - 1))
storageAdapter.setBackoffTime(
now() +
Math.min(
options.maxBackoff,
options.backoffTime * Math.pow(2, errorCount - 1)
)
)
log.warn('backoff time ' + (storageAdapter.getBackoffTime() - now()) + 'ms')
}

29 changes: 26 additions & 3 deletions test/test-queue-that.js
Original file line number Diff line number Diff line change
@@ -349,18 +349,41 @@ describe('createQueueThat', function () {
clock.tick(QUEUE_GROUP_TIME)

options.process.getCall(0).args[1]('error')
clock.tick(BACKOFF_TIME + QUEUE_GROUP_TIME)
clock.tick(BACKOFF_TIME)

expect(options.process.callCount).to.be(2)
options.process.getCall(1).args[1]('error')

clock.tick(BACKOFF_TIME + QUEUE_GROUP_TIME)
clock.tick(BACKOFF_TIME)
expect(options.process.callCount).to.be(2)

clock.tick(BACKOFF_TIME + QUEUE_GROUP_TIME)
clock.tick(BACKOFF_TIME)
expect(options.process.callCount).to.be(3)
options.process.getCall(2).args[1]('error')

// backoff should wait to BACKOFF_TIME * 4
clock.tick(BACKOFF_TIME * 2)
expect(options.process.callCount).to.be(3)
})

it('should backoff to options.maxBackoff', function () {
options.maxBackoff = 2 * BACKOFF_TIME
localStorageAdapter.setQueue(_.range(4))
queueThat('A')
clock.tick(QUEUE_GROUP_TIME)

options.process.getCall(0).args[1]('error')
clock.tick(BACKOFF_TIME)

options.process.getCall(1).args[1]('error')

clock.tick(2 * (BACKOFF_TIME))
options.process.getCall(2).args[1]('error')

clock.tick(2 * (BACKOFF_TIME))
expect(options.process.callCount).to.be(4)
})

it('should backoff on timeout', function () {
localStorageAdapter.setQueue(_.range(4))
queueThat('A')