From 1df52cc6e85705a917c882e48bb9019b14c958eb Mon Sep 17 00:00:00 2001 From: Andrew Ostroumov Date: Sun, 14 Feb 2021 12:37:31 +0200 Subject: [PATCH] Prevent devices always being pinged at exactly the same time (#6133) * Add availability lag of rand 10% from timeout * Update availability.js * Update utils.js Co-authored-by: Koen Kanters --- .dockerignore | 3 ++- lib/extension/availability.js | 10 +++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/.dockerignore b/.dockerignore index e3082b7dd..01667c49d 100644 --- a/.dockerignore +++ b/.dockerignore @@ -3,6 +3,7 @@ .git .github .gitignore +.idea .npmrc .travis* .travis.yml @@ -12,4 +13,4 @@ images node_modules scripts test -update.sh \ No newline at end of file +update.sh diff --git a/lib/extension/availability.js b/lib/extension/availability.js index b7ac94784..c82b0371d 100644 --- a/lib/extension/availability.js +++ b/lib/extension/availability.js @@ -14,6 +14,13 @@ const pingableEndDevices = [ ]; const Hours25 = 1000 * 60 * 60 * 25; +const AvailabilityLagRatio = 0.1; + + +function timeoutLag(timeout, ratio) { + const lag = timeout * ratio; + return Math.floor(Math.random() * Math.floor(lag)); +} /** * This extensions pings devices to check if they are online. @@ -137,10 +144,11 @@ class Availability extends Extension { } setTimerPingable(device) { + const timeout = this.availability_timeout + timeoutLag(this.availability_timeout, AvailabilityLagRatio); clearTimeout(this.timers[device.ieeeAddr]); this.timers[device.ieeeAddr] = setTimeout(async () => { await this.handleIntervalPingable(device); - }, utils.secondsToMilliseconds(this.availability_timeout)); + }, utils.secondsToMilliseconds(timeout)); } async stop() {