UNPKG

pandora-hub

Version:

pandora.js messenge hub

31 lines (24 loc) 574 B
import {SelectedInfo} from '../domain'; /** * Balancer */ export class Balancer { protected clients: Array<SelectedInfo>; constructor(clients) { this.clients = clients; } /** * Pick a random client * @return {SelectedInfo} */ pick(): SelectedInfo { if(this.clients.length === 1) { return this.clients[0]; } const randomInt = Balancer.getRandomInt(0, this.clients.length - 1); return this.clients[randomInt]; } public static getRandomInt(min, max) { return Math.floor(Math.random() * (max - min + 1)) + min; } }