Skip to content

Commit 19e0b7e

Browse files
committed
Update Retry.php, Table.php, RetryOnBadSessionTest.php
1 parent a008365 commit 19e0b7e

File tree

3 files changed

+30
-27
lines changed

3 files changed

+30
-27
lines changed

src/Retry/Retry.php

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function __construct()
3131

3232
protected function retryDelay(int $retryCount, Backoff $backoff)
3333
{
34-
return $backoff->getBackoffSlotMillis()*(1<<min($retryCount, $backoff->getBackoffCeiling()));
34+
return $backoff->getBackoffSlotMillis() * (1 << min($retryCount, $backoff->getBackoffCeiling()));
3535
}
3636

3737
/**
@@ -72,21 +72,22 @@ public function withParams(?RetryParams $params): Retry
7272
* @throws NonRetryableException
7373
* @throws RetryableException
7474
*/
75-
public function retry(Closure $closure, bool $idempotent){
75+
public function retry(Closure $closure, bool $idempotent)
76+
{
7677
$startTime = microtime(true);
7778
$retryCount = 0;
7879
$lastException = null;
79-
while (microtime(true) < $startTime+$this->timeoutMs/1000){
80+
while (microtime(true) < $startTime + $this->timeoutMs / 1000) {
8081
try {
8182
return $closure();
82-
} catch (RetryableException $e){
83-
if (!isset(self::$idempotentOnly[get_class($e)])){
83+
} catch (RetryableException $e) {
84+
if (isset(self::$idempotentOnly[get_class($e)]) && $idempotent) {
8485
throw $e;
8586
}
8687
$retryCount++;
87-
$this->retryDelay($retryCount,$this->backoffType($e));
88+
$this->retryDelay($retryCount, $this->backoffType($e));
8889
$lastException = $e;
89-
} catch (Exception $e){
90+
} catch (Exception $e) {
9091
throw $e;
9192
}
9293
}
@@ -99,23 +100,24 @@ public function retry(Closure $closure, bool $idempotent){
99100
*/
100101
protected function backoffType(RetryableException $e): Backoff
101102
{
102-
if ($e instanceof AbortedException){
103-
return $this->fastBackOff;
104-
} elseif ($e instanceof BadSessionException) {
105-
return $this->fastBackOff;
106-
} elseif ($e instanceof SessionBusyException) {
107-
return $this->fastBackOff;
108-
} elseif ($e instanceof UndeterminedException) {
109-
return $this->fastBackOff;
110-
} elseif ($e instanceof UnavailableException) {
111-
return $this->fastBackOff;
112-
} elseif ($e instanceof UndeterminedException) {
113-
return $this->fastBackOff;
114-
} elseif ($e instanceof DeadlineExceededException){
115-
return $this->fastBackOff;
116-
} else {
117-
return $this->slowBackOff;
118-
}
103+
return $this->fastBackOff;
104+
// if ($e instanceof AbortedException) {
105+
// return $this->fastBackOff;
106+
// } elseif ($e instanceof BadSessionException) {
107+
// return $this->fastBackOff;
108+
// } elseif ($e instanceof SessionBusyException) {
109+
// return $this->fastBackOff;
110+
// } elseif ($e instanceof UndeterminedException) {
111+
// return $this->fastBackOff;
112+
// } elseif ($e instanceof UnavailableException) {
113+
// return $this->fastBackOff;
114+
// } elseif ($e instanceof UndeterminedException) {
115+
// return $this->fastBackOff;
116+
// } elseif ($e instanceof DeadlineExceededException) {
117+
// return $this->fastBackOff;
118+
// } else {
119+
// return $this->slowBackOff;
120+
// }
119121
}
120122

121123
private static $idempotentOnly = [

src/Table.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ public function retrySession(Closure $userFunc, bool $idempotent = false, RetryP
457457
$session = $this->session();
458458
return $userFunc($session);
459459
} catch (Exception $exception){
460-
if (!is_null($session) && isset(self::$deleteSession[get_class($exception)])){
460+
if ($session != null && in_array(get_class($exception), self::$deleteSession)){
461461
$this->dropSession($session->id());
462462
}
463463
throw $exception;

tests/RetryOnBadSessionTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PHPUnit\Framework\TestCase;
66
use YdbPlatform\Ydb\Auth\Implement\AnonymousAuthentication;
7+
use YdbPlatform\Ydb\Session;
78
use YdbPlatform\Ydb\Table;
89
use YdbPlatform\Ydb\Ydb;
910

@@ -71,14 +72,14 @@ private function backwardCompatibility(Table $table)
7172
private function retryTest(Table $table)
7273
{
7374
$i = 0;
74-
$table->retrySession(function ($session) use (&$i){
75+
$table->retrySession(function (Session $session) use (&$i){
7576
$i++;
7677
if($i==1)SessionManager::setSessionId($session, $this->oldSessionId);
7778
$tres = $session->query('select 1 as res')->rows()[0]['res'];
7879
self::assertEquals(
7980
1,
8081
$tres
8182
);
82-
});
83+
}, true);
8384
}
8485
}

0 commit comments

Comments
 (0)