@@ -198,7 +198,7 @@ private function makeRequest(string $method, string $endPoint, bool $asJson, arr
198198 $ lastException = null ;
199199 while ($ numRetries < $ this ->config ->getNumRetries () + 1 ) {
200200 $ numRetries ++;
201- $ node = $ this ->getNode ();
201+ $ node = $ this ->getNode ($ numRetries );
202202
203203 try {
204204 $ url = $ node ->url () . $ endPoint ;
@@ -328,30 +328,54 @@ public function setNodeHealthCheck(Node $node, bool $isHealthy): void
328328 * Returns a healthy host from the pool in a round-robin fashion
329329 * Might return an unhealthy host periodically to check for recovery.
330330 *
331+ * @param int $requestNumber
331332 * @return Node
332333 */
333- public function getNode (): Lib \Node
334+ public function getNode (int $ requestNumber = 0 ): Lib \Node
334335 {
336+ $ this ->logger ->debug ("Request # {$ requestNumber }: Getting next node " );
337+
335338 if ($ this ->nearestNode !== null ) {
339+ $ this ->logger ->debug (
340+ "Request # {$ requestNumber }: Nodes Health: Nearest node is " .
341+ ($ this ->nearestNode ->isHealthy () ? "Healthy " : "Unhealthy " )
342+ );
343+
336344 if ($ this ->nearestNode ->isHealthy () || $ this ->nodeDueForHealthCheck ($ this ->nearestNode )) {
345+ $ this ->logger ->debug (
346+ "Request # {$ requestNumber }: Using nearest node "
347+ );
337348 return $ this ->nearestNode ;
338349 }
350+ $ this ->logger ->debug ("Request # {$ requestNumber }: Falling back to individual nodes " );
339351 }
340- $ i = 0 ;
341- while ($ i < count ($ this ->nodes )) {
342- $ i ++;
343- $ node = $ this ->nodes [$ this ->nodeIndex ];
352+
353+ $ candidateNode = $ this ->nodes [0 ];
354+ for ($ i = 0 ; $ i <= count ($ this ->nodes ); $ i ++) {
344355 $ this ->nodeIndex = ($ this ->nodeIndex + 1 ) % count ($ this ->nodes );
345- if ($ node ->isHealthy () || $ this ->nodeDueForHealthCheck ($ node )) {
346- return $ node ;
356+ $ candidateNode = $ this ->nodes [$ this ->nodeIndex ];
357+
358+ $ this ->logger ->debug (
359+ "Request # {$ requestNumber }: Nodes Health: Node is " .
360+ ($ candidateNode ->isHealthy () ? "Healthy " : "Unhealthy " )
361+ );
362+
363+ if ($ candidateNode ->isHealthy () || $ this ->nodeDueForHealthCheck ($ candidateNode )) {
364+ $ this ->logger ->debug (
365+ "Request # {$ requestNumber }: Updated current node "
366+ );
367+ return $ candidateNode ;
347368 }
348369 }
349370
350371 /**
351372 * None of the nodes are marked healthy, but some of them could have become healthy since last health check.
352373 * So we will just return the next node.
353374 */
354- return $ this ->nodes [$ this ->nodeIndex ];
375+ $ this ->logger ->debug (
376+ "Request # {$ requestNumber }: No healthy nodes were found. Returning the next node "
377+ );
378+ return $ candidateNode ;
355379 }
356380
357381 /**
0 commit comments