From 1be78e4cd38e4e576cbdd5e520a78bad81c6f69e Mon Sep 17 00:00:00 2001 From: Bel LaPointe Date: Mon, 13 Dec 2021 13:03:17 -0700 Subject: [PATCH] dont run intervals stacked up --- nullboard.html | 94 ++++++++++++++++++++++++++++++++++---------------- 1 file changed, 64 insertions(+), 30 deletions(-) diff --git a/nullboard.html b/nullboard.html index 6331eeb..592ba8b 100644 --- a/nullboard.html +++ b/nullboard.html @@ -1338,19 +1338,70 @@ this.databases.push(arguments[i]); } this.deltas = new Deltas(this.cache()); - this.intervals = []; this.scheduleSync(); - this.syncCacheWithPrimary(); + this.syncIntervalCacheWithPrimary(); } - syncCacheWithPrimary() + redundantSyncInterval(key) + { + let last_run = this.intervals_last_run[key]; + let now = Date.now(); + let redundant = last_run != undefined && now - last_run < 1000; + console.log(`redundant ${key}? last=${last_run}, now=${now}, redundant=${redundant}`); + if (!redundant) + this.intervals_last_run[key] = now; + return redundant; + } + + syncIntervalDeltas() + { + if (!this.redundantSyncInterval("syncIntervalDeltas")) + return; + this._syncIntervalDeltas(); + } + + _syncIntervalDeltas() + { + var n = this.pendingSyncCount(); + try + { + this.deltas.popEach((key) => { + console.log("syncing: " + key); + this.replicate(key); + }); + } + catch (e) + { + console.log("failed to sync: " + e); + } + finally + { + this.writePendingSyncCount(); + } + } + + syncIntervalCacheWithPrimary() + { + if (!this.redundantSyncInterval("syncIntervalCacheWithPrimary")) + return; + this._syncIntervalCacheWithPrimary(); + } + + _syncIntervalCacheWithPrimary() { if (this.pendingSyncCount()) return; this.syncAWithB(this.cache(), this.primary()); } - syncReplicasWithCache() + syncIntervalReplicasWithCache() + { + if (!this.redundantSyncInterval("syncIntervalReplicasWithCache")) + return; + this._syncIntervalReplicasWithCache(); + } + + _syncIntervalReplicasWithCache() { this.replicas().slice(1).forEach((replica) => { this.syncAWithB(replica, this.cache()); @@ -1383,43 +1434,26 @@ scheduleSync() { + this.close(); this.intervals.push( - setInterval(() => { this.syncDeltas(); }, 10 * 1000) + setInterval(() => { this.syncIntervalDeltas(); }, 10 * 1000) ); this.intervals.push( - setInterval(() => { this.syncCacheWithPrimary(); }, 120 * 1000) + setInterval(() => { this.syncIntervalCacheWithPrimary(); }, 120 * 1000) ); this.intervals.push( - setInterval(() => { this.syncReplicasWithCache(); }, 123 * 1000) + setInterval(() => { this.syncIntervalReplicasWithCache(); }, 123 * 1000) ); } close() { - this.intervals.forEach((i) => { - clearInterval(i); - }); - this.intervals = []; - } - - syncDeltas() - { - var n = this.pendingSyncCount(); - try - { - this.deltas.popEach((key) => { - console.log("syncing: " + key); - this.replicate(key); + if (this.intervals) + this.intervals.forEach((i) => { + clearInterval(i); }); - } - catch (e) - { - console.log("failed to sync: " + e); - } - finally - { - this.writePendingSyncCount(); - } + this.intervals = []; + this.intervals_last_run = {}; } pendingSyncCount()