Skip to content

Commit 6928c14

Browse files
authored
Update Retry.php
1 parent 5725fa0 commit 6928c14

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

src/Retry/Retry.php

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,8 @@
44

55
use Closure;
66
use YdbPlatform\Ydb\Exception;
7-
use YdbPlatform\Ydb\Exceptions\Grpc\DeadlineExceededException;
8-
use YdbPlatform\Ydb\Exceptions\NonRetryableException;
97
use YdbPlatform\Ydb\Exceptions\RetryableException;
10-
use YdbPlatform\Ydb\Exceptions\Ydb\AbortedException;
11-
use YdbPlatform\Ydb\Exceptions\Ydb\BadSessionException;
12-
use YdbPlatform\Ydb\Exceptions\Ydb\SessionBusyException;
13-
use YdbPlatform\Ydb\Exceptions\Ydb\UnavailableException;
14-
use YdbPlatform\Ydb\Exceptions\Ydb\UndeterminedException;
8+
use Psr\Log\LoggerInterface;
159

1610
class Retry
1711
{
@@ -22,9 +16,14 @@ class Retry
2216

2317
protected $fastBackOff;
2418
protected $noBackOff;
19+
/**
20+
* @var LoggerInterface
21+
*/
22+
protected $logger;
2523

26-
public function __construct()
24+
public function __construct(LoggerInterface $logger)
2725
{
26+
$this->logger = $logger;
2827
$this->timeoutMs = 2000;
2928
$this->fastBackOff = new Backoff(6, 5);
3029
$this->slowBackOff = new Backoff(6, 1000);
@@ -79,11 +78,14 @@ public function retry(Closure $closure, bool $idempotent)
7978
$retryCount = 0;
8079
$lastException = null;
8180
while (microtime(true) < $startTime + $this->timeoutMs / 1000) {
81+
$this->logger->debug("YDB: Run user function. Retry count: $retryCount. Ms: ".(microtime(true) - $startTime));
8282
try {
8383
return $closure();
8484
} catch (Exception $e) {
85+
$this->logger->warning("YDB: Received exception: ".$e->getMessage());
8586
if (!$this->canRetry($e, $idempotent)){
86-
throw $e;
87+
$lastException = $e;
88+
break;
8789
}
8890
$retryCount++;
8991
$this->retryDelay($retryCount, $this->backoffType($e));
@@ -99,18 +101,20 @@ public function retry(Closure $closure, bool $idempotent)
99101
*/
100102
protected function backoffType(string $e): Backoff
101103
{
102-
return in_array($e, self::$immediatelyBackoff)?$this->noBackOff:
103-
(in_array($e, self::$fastBackoff)?$this->fastBackOff:$this->slowBackOff);
104+
return in_array($e, self::$immediatelyBackoff) ? $this->noBackOff :
105+
(in_array($e, self::$fastBackoff) ? $this->fastBackOff : $this->slowBackOff);
104106
}
105107

106-
protected function alwaysRetry(string $exception){
108+
protected function alwaysRetry(string $exception)
109+
{
107110
return in_array($exception, self::$alwaysRetry);
108111
}
109112

110113
protected function canRetry(Exception $e, bool $idempotent)
111114
{
112-
return is_a($e, RetryableException::class)&&($this->alwaysRetry(get_class($e)) || $idempotent);
115+
return is_a($e, RetryableException::class) && ($this->alwaysRetry(get_class($e)) || $idempotent);
113116
}
117+
114118
private static $immediatelyBackoff = [
115119
\YdbPlatform\Ydb\Exceptions\Grpc\AbortedException::class,
116120
\YdbPlatform\Ydb\Exceptions\Ydb\BadSessionException::class,

0 commit comments

Comments
 (0)