Commit ead12a90 authored by Fatih Acet's avatar Fatih Acet

Merge branch 'improve-backoff-algo' into 'master'

Improves backoff algo with maxInterval between requests

See merge request !9548
parents 9c3d21e7 aa1f7de9
...@@ -329,17 +329,18 @@ ...@@ -329,17 +329,18 @@
* ``` * ```
*/ */
w.gl.utils.backOff = (fn, timeout = 60000) => { w.gl.utils.backOff = (fn, timeout = 60000) => {
const maxInterval = 32000;
let nextInterval = 2000; let nextInterval = 2000;
const startTime = (+new Date()); const startTime = Date.now();
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const stop = arg => ((arg instanceof Error) ? reject(arg) : resolve(arg)); const stop = arg => ((arg instanceof Error) ? reject(arg) : resolve(arg));
const next = () => { const next = () => {
if (new Date().getTime() - startTime < timeout) { if (Date.now() - startTime < timeout) {
setTimeout(fn.bind(null, next, stop), nextInterval); setTimeout(fn.bind(null, next, stop), nextInterval);
nextInterval *= 2; nextInterval = Math.min(nextInterval + nextInterval, maxInterval);
} else { } else {
reject(new Error('BACKOFF_TIMEOUT')); reject(new Error('BACKOFF_TIMEOUT'));
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment