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
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
10 changes: 9 additions & 1 deletion lib/queue-that.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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')
}

Expand Down
Loading