Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/Lib/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ class Configuration
*/
private int $logLevel;

/**
* @var bool
*/
private bool $randomizeNodes;

/**
* Configuration constructor.
*
Expand All @@ -83,6 +88,11 @@ public function __construct(array $config)
$this->nodes[] = new Node($node['host'], $node['port'], $node['path'] ?? '', $node['protocol']);
}

$this->randomizeNodes = $config['randomize_nodes'] ?? true;
if ($this->randomizeNodes) {
$this->shuffleNodes();
}

$nearestNode = $config['nearest_node'] ?? null;
$this->nearestNode = null;
if (null !== $nearestNode) {
Expand Down Expand Up @@ -237,4 +247,18 @@ public function getClient(): ClientInterface | HttpMethodsClient
}
return $this->client;
}

/**
* Shuffles the nodes array using Fisher-Yates algorithm
*/
private function shuffleNodes(): void
Comment thread
tharropoulos marked this conversation as resolved.
Outdated
{
$count = count($this->nodes);
for ($i = $count - 1; $i > 0; $i--) {
$j = random_int(0, $i);
$temp = $this->nodes[$i];
$this->nodes[$i] = $this->nodes[$j];
$this->nodes[$j] = $temp;
}
}
}