mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-12-23 16:40:36 +00:00
Merge pull request #2709 from WesleyAC/improve-polling-backoff
Improve polling algorithm
This commit is contained in:
commit
2f737efeff
1 changed files with 8 additions and 6 deletions
|
@ -95,7 +95,6 @@ let BookWyrm = new (class {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Update a counter with recurring requests to the API
|
* Update a counter with recurring requests to the API
|
||||||
* The delay is slightly randomized and increased on each cycle.
|
|
||||||
*
|
*
|
||||||
* @param {Object} counter - DOM node
|
* @param {Object} counter - DOM node
|
||||||
* @param {int} delay - frequency for polling in ms
|
* @param {int} delay - frequency for polling in ms
|
||||||
|
@ -104,16 +103,19 @@ let BookWyrm = new (class {
|
||||||
polling(counter, delay) {
|
polling(counter, delay) {
|
||||||
const bookwyrm = this;
|
const bookwyrm = this;
|
||||||
|
|
||||||
delay = delay || 10000;
|
delay = delay || 5 * 60 * 1000 + (Math.random() - 0.5) * 30 * 1000;
|
||||||
delay += Math.random() * 1000;
|
|
||||||
|
|
||||||
setTimeout(
|
setTimeout(
|
||||||
function () {
|
function () {
|
||||||
fetch("/api/updates/" + counter.dataset.poll)
|
fetch("/api/updates/" + counter.dataset.poll)
|
||||||
.then((response) => response.json())
|
.then((response) => response.json())
|
||||||
.then((data) => bookwyrm.updateCountElement(counter, data));
|
.then((data) => {
|
||||||
|
bookwyrm.updateCountElement(counter, data);
|
||||||
bookwyrm.polling(counter, delay * 1.25);
|
bookwyrm.polling(counter);
|
||||||
|
})
|
||||||
|
.catch(() => {
|
||||||
|
bookwyrm.polling(counter, delay * 1.1);
|
||||||
|
});
|
||||||
},
|
},
|
||||||
delay,
|
delay,
|
||||||
counter
|
counter
|
||||||
|
|
Loading…
Reference in a new issue