From 30b8d3d0ab62a846fb075c0fc25a8697346d0e3e Mon Sep 17 00:00:00 2001 From: LouisLam Date: Thu, 19 Aug 2021 18:33:52 +0800 Subject: [PATCH] prevent all monitors making requests at the same moment when start the server --- server/server.js | 7 +++++-- src/util.js | 12 +++++++++++- src/util.ts | 22 ++++++++++++++++++++++ 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/server/server.js b/server/server.js index 5920d1bb1..e65117627 100644 --- a/server/server.js +++ b/server/server.js @@ -1,7 +1,7 @@ console.log("Welcome to Uptime Kuma"); console.log("Node Env: " + process.env.NODE_ENV); -const { sleep, debug, TimeLogger } = require("../src/util"); +const { sleep, debug, TimeLogger, getRandomInt } = require("../src/util"); console.log("Importing Node libraries") const fs = require("fs"); @@ -755,8 +755,11 @@ async function startMonitors() { let list = await R.find("monitor", " active = 1 ") for (let monitor of list) { - monitor.start(io) + monitor.start(io); monitorList[monitor.id] = monitor; + + // Give some delays, so all monitors won't make request at the same moment when just start the server. + await sleep(getRandomInt(300, 1000)); } } diff --git a/src/util.js b/src/util.js index 44eaebc69..14c683218 100644 --- a/src/util.js +++ b/src/util.js @@ -1,6 +1,6 @@ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); -exports.TimeLogger = exports.polyfill = exports.debug = exports.ucfirst = exports.sleep = exports.flipStatus = exports.PENDING = exports.UP = exports.DOWN = exports.appName = exports.isDev = void 0; +exports.getRandomInt = exports.getRandomArbitrary = exports.TimeLogger = exports.polyfill = exports.debug = exports.ucfirst = exports.sleep = exports.flipStatus = exports.PENDING = exports.UP = exports.DOWN = exports.appName = exports.isDev = void 0; const _dayjs = require("dayjs"); const dayjs = _dayjs; exports.isDev = process.env.NODE_ENV === "development"; @@ -58,3 +58,13 @@ class TimeLogger { } } exports.TimeLogger = TimeLogger; +function getRandomArbitrary(min, max) { + return Math.random() * (max - min) + min; +} +exports.getRandomArbitrary = getRandomArbitrary; +function getRandomInt(min, max) { + min = Math.ceil(min); + max = Math.floor(max); + return Math.floor(Math.random() * (max - min + 1)) + min; +} +exports.getRandomInt = getRandomInt; diff --git a/src/util.ts b/src/util.ts index add9f1935..7d7b8e01d 100644 --- a/src/util.ts +++ b/src/util.ts @@ -81,3 +81,25 @@ export class TimeLogger { } } } + +/** + * Returns a random number between min (inclusive) and max (exclusive) + */ +export function getRandomArbitrary(min, max) { + return Math.random() * (max - min) + min; +} + +/** + * From: https://stackoverflow.com/questions/1527803/generating-random-whole-numbers-in-javascript-in-a-specific-range + * + * Returns a random integer between min (inclusive) and max (inclusive). + * The value is no lower than min (or the next integer greater than min + * if min isn't an integer) and no greater than max (or the next integer + * lower than max if max isn't an integer). + * Using Math.round() will give you a non-uniform distribution! + */ +export function getRandomInt(min, max) { + min = Math.ceil(min); + max = Math.floor(max); + return Math.floor(Math.random() * (max - min + 1)) + min; +}