From 43666d616d286ba64ce237facd7e247da7fb9d3c Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Sun, 23 Oct 2016 19:14:28 +0200 Subject: [PATCH] Server: randomize the requests list We don't want to stuck with the same failing requests --- server/models/request.js | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/server/models/request.js b/server/models/request.js index f11c20b52..34a4287ea 100644 --- a/server/models/request.js +++ b/server/models/request.js @@ -128,7 +128,9 @@ function makeRequest (toPod, requestsToMake, callback) { function makeRequests () { const self = this - listWithLimit.call(self, constants.REQUESTS_LIMIT, function (err, requests) { + // We limit the size of the requests (REQUESTS_LIMIT) + // We don't want to stuck with the same failing requests so we get a random list + listWithLimitAndRandom.call(self, constants.REQUESTS_LIMIT, function (err, requests) { if (err) { logger.error('Cannot get the list of requests.', { err: err }) return // Abort @@ -249,8 +251,17 @@ function updatePodsScore (goodPods, badPods) { }) } -function listWithLimit (limit, callback) { - this.find({ }, { _id: 1, request: 1, to: 1 }).sort({ _id: 1 }).limit(limit).exec(callback) +function listWithLimitAndRandom (limit, callback) { + const self = this + + self.count(function (err, count) { + if (err) return callback(err) + + let start = Math.floor(Math.random() * count) - limit + if (start < 0) start = 0 + + self.find({ }, { _id: 1, request: 1, to: 1 }).sort({ _id: 1 }).skip(start).limit(limit).exec(callback) + }) } function removeAll (callback) { -- 2.25.1