selector = $selector; $this->resurrect = $resurrect; } public function setHosts(array $hosts): self { $this->nodes = []; foreach ($hosts as $host) { $this->nodes[] = new Node($host); } shuffle($this->nodes); // randomize for use different hosts on each execution $this->selector->setNodes($this->nodes); return $this; } public function nextNode(): Node { $totNodes = count($this->nodes); $dead = 0; while ($dead < $totNodes) { $next = $this->selector->nextNode(); if ($next->isAlive()) { return $next; } if ($this->resurrect->ping($next)) { $next->markAlive(true); return $next; } $dead++; } throw new NoNodeAvailableException(sprintf( 'No alive nodes. All the %d nodes seem to be down.', $totNodes )); } }