nodes; foreach ($nodes as $node) { if (! $node instanceof Node) { continue; } $weight = $node->weight; $totalWeight += $weight; if ($lastWeight !== null && $isSameWeight && $weight !== $lastWeight) { $isSameWeight = false; } $lastWeight = $weight; } if ($totalWeight > 0 && ! $isSameWeight) { $offset = mt_rand(0, $totalWeight - 1); foreach ($nodes as $node) { $offset -= $node->weight; if ($offset < 0) { return $node; } } } if (! $nodes) { throw new NoNodesAvailableException('Cannot select any node from load balancer.'); } return $nodes[array_rand($nodes)]; } }