Skip to content

Commit b641649

Browse files
authored
Merge pull request #134 from csgofloat/fix/bot-request-distribution
Distributes Request Load Evenly Across Bots
2 parents 35ca4d7 + e547a22 commit b641649

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

lib/bot_controller.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const Bot = require('./bot'),
2+
utils = require('./utils'),
23
EventEmitter = require('events').EventEmitter,
34
errors = require('../errors');
45

@@ -32,7 +33,8 @@ class BotController extends EventEmitter {
3233
}
3334

3435
getFreeBot() {
35-
for (let bot of this.bots) {
36+
// Shuffle array to evenly distribute requests
37+
for (let bot of utils.shuffleArray(this.bots)) {
3638
if (!bot.busy && bot.ready) return bot;
3739
}
3840

lib/utils.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,12 @@ exports.chunkArray = function (arr, size) {
109109
return new Array(Math.ceil(arr.length / size)).fill().map(_ => arr.splice(0,size));
110110
};
111111

112+
/*
113+
Shuffle array - O(N LOG N) so it shouldn't be used for super-large arrays
114+
*/
115+
exports.shuffleArray = function (arr) {
116+
return arr.map(value => ({ value, sort: Math.random() }))
117+
.sort((a, b) => a.sort - b.sort)
118+
.map(({ value }) => value)
119+
}
120+

0 commit comments

Comments
 (0)