Skip to content

Commit

Permalink
Merge pull request #134 from csgofloat/fix/bot-request-distribution
Browse files Browse the repository at this point in the history
Distributes Request Load Evenly Across Bots
  • Loading branch information
Step7750 authored Apr 20, 2023
2 parents 35ca4d7 + e547a22 commit b641649
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/bot_controller.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const Bot = require('./bot'),
utils = require('./utils'),
EventEmitter = require('events').EventEmitter,
errors = require('../errors');

Expand Down Expand Up @@ -32,7 +33,8 @@ class BotController extends EventEmitter {
}

getFreeBot() {
for (let bot of this.bots) {
// Shuffle array to evenly distribute requests
for (let bot of utils.shuffleArray(this.bots)) {
if (!bot.busy && bot.ready) return bot;
}

Expand Down
9 changes: 9 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,12 @@ exports.chunkArray = function (arr, size) {
return new Array(Math.ceil(arr.length / size)).fill().map(_ => arr.splice(0,size));
};

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

0 comments on commit b641649

Please sign in to comment.