From ac873f8db8bf8ee50cbc778f5ef6d2a07dc174ee Mon Sep 17 00:00:00 2001 From: Aleksey Konstantinov Date: Wed, 4 Sep 2024 15:26:19 +0300 Subject: [PATCH] Updated backoff --- package-lock.json | 4 ++-- package.json | 2 +- src/backoff.js | 32 ++++++++++---------------------- 3 files changed, 13 insertions(+), 25 deletions(-) diff --git a/package-lock.json b/package-lock.json index 94e5469..f439e73 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@ulms/api-clients", - "version": "7.16.0", + "version": "7.17.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@ulms/api-clients", - "version": "7.16.0", + "version": "7.17.0", "license": "MIT", "dependencies": { "axios": "1.6.2", diff --git a/package.json b/package.json index d58792b..6825b38 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@ulms/api-clients", - "version": "7.16.0", + "version": "7.17.0", "description": "JavaScript API clients for ULMS platform", "keywords": [], "homepage": "https://github.com/foxford/ulms-api-clients-js#readme", diff --git a/src/backoff.js b/src/backoff.js index 4349eb3..4c4904f 100644 --- a/src/backoff.js +++ b/src/backoff.js @@ -1,18 +1,10 @@ -const FACTOR = 2 +const MIN_DELAY = 500 // 500ms const MAX_DELAY = 5 * 60 * 1000 // 5m -const RANDOM_ADDITIVE_MAX = 1000 // 1s +const MAX_RANDOM_ADDITIVE = 2000 // 2s // get milliseconds as value const MILLISECONDS_IN_SECOND = 1000 -function getRandomIntInclusive(min, max) { - const minValue = Math.ceil(min) - const maxValue = Math.floor(max) - - // The maximum is inclusive and the minimum is inclusive - return Math.floor(Math.random() * (maxValue - minValue + 1) + minValue) -} - class Backoff { constructor() { this.counter = 0 @@ -26,18 +18,14 @@ class Backoff { } next() { - if (this.delay < MAX_DELAY) { - const randomAdditive = getRandomIntInclusive(0, RANDOM_ADDITIVE_MAX) - - // first delay always equals to randomAdditive - this.delay = - this.counter === 0 - ? randomAdditive - : Math.min( - MILLISECONDS_IN_SECOND * FACTOR ** this.counter + randomAdditive, - MAX_DELAY, - ) - } + this.delay = Math.min( + Math.round( + MIN_DELAY + + MILLISECONDS_IN_SECOND * Math.expm1(this.counter) + + MAX_RANDOM_ADDITIVE * Math.random(), + ), + MAX_DELAY, + ) this.counter += 1 }