Skip to content

Commit 387762f

Browse files
committed
Update retry
1 parent 09ad561 commit 387762f

File tree

5 files changed

+89
-62
lines changed

5 files changed

+89
-62
lines changed

src/Exceptions/SdkException.php

Lines changed: 0 additions & 34 deletions
This file was deleted.

src/Retry/Retry.php

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,58 @@
99
class Retry
1010
{
1111

12-
/**
13-
* @var RetryParams
14-
*/
15-
private $params;
12+
protected $timeoutMs;
13+
14+
protected $slowBackOff;
1615

17-
public function __construct(?RetryParams $params)
16+
protected $fastBackOff;
17+
18+
public function __construct()
1819
{
19-
if (is_null($params)){
20-
$params = new RetryParams();
21-
}
22-
$this->params = $params;
20+
$this->timeoutMs = 2000;
21+
$this->fastBackOff = new Backoff(6, 5);
22+
$this->slowBackOff = new Backoff(6, 1000);
2323
}
2424

2525
protected function retryDelay(int $retryCount, Backoff $backoff)
2626
{
2727
return $backoff->getBackoffSlotMillis()*(1<<min($retryCount, $backoff->getBackoffCeiling()));
2828
}
2929

30+
/**
31+
* @param int $timeoutMs
32+
*/
33+
protected function setTimeoutMs(int $timeoutMs): void
34+
{
35+
$this->timeoutMs = $timeoutMs;
36+
}
37+
38+
/**
39+
* @param Backoff $slowBackOff
40+
*/
41+
protected function setSlowBackOff(Backoff $slowBackOff): void
42+
{
43+
$this->slowBackOff = $slowBackOff;
44+
}
45+
46+
/**
47+
* @param Backoff $fastBackOff
48+
*/
49+
protected function setFastBackOff(Backoff $fastBackOff): void
50+
{
51+
$this->fastBackOff = $fastBackOff;
52+
}
53+
54+
public function withParams(?RetryParams $params): Retry
55+
{
56+
if (is_null($params)) return $this;
57+
$retry = clone $this;
58+
if ($params->getTimeoutMs()) $retry->setTimeoutMs($params->getTimeoutMs());
59+
if ($params->getFastBackOff()) $retry->setFastBackOff($params->getFastBackOff());
60+
if ($params->getSlowBackOff()) $retry->setSlowBackOff($params->getSlowBackOff());
61+
return $retry;
62+
}
63+
3064
/**
3165
* @throws NonRetryableException
3266
* @throws RetryableException
@@ -35,7 +69,7 @@ public function retry(Closure $closure, bool $idempotent){
3569
$startTime = microtime(true);
3670
$retryCount = 0;
3771
$lastException = null;
38-
while (microtime(true) < $startTime+$this->params->getTimeoutMs()/1000){
72+
while (microtime(true) < $startTime+$this->timeoutMs/1000){
3973
try {
4074
return $closure();
4175
} catch (RetryableException $e){
@@ -50,5 +84,4 @@ public function retry(Closure $closure, bool $idempotent){
5084
throw $lastException;
5185
}
5286

53-
5487
}

src/Retry/RetryParams.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,15 @@ class RetryParams
1111
protected $slowBackOff;
1212
protected $fastBackOff;
1313

14-
public function __construct($timeoutMs = 2000, Backoff $fastBackOff = null, Backoff $slowBackOff = null)
14+
public function __construct($timeoutMs = null, Backoff $fastBackOff = null, Backoff $slowBackOff = null)
1515
{
1616
$this->timeoutMs = $timeoutMs;
17-
if (!$fastBackOff){
18-
$fastBackOff = new Backoff(6, 5);
19-
}
2017
$this->fastBackOff = $fastBackOff;
21-
if (!$slowBackOff){
22-
$slowBackOff = new Backoff(6, 1000);
23-
}
2418
$this->slowBackOff = $slowBackOff;
2519
}
2620

2721
/**
28-
* @return int|mixed
22+
* @return int|null
2923
*/
3024
public function getTimeoutMs()
3125
{

src/Table.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use YdbPlatform\Ydb\Exceptions\NonRetryableException;
1212
use YdbPlatform\Ydb\Exceptions\RetryableException;
1313
use YdbPlatform\Ydb\Exceptions\Ydb\BadSessionException;
14+
use YdbPlatform\Ydb\Retry\Backoff;
1415
use YdbPlatform\Ydb\Retry\Retry;
1516
use YdbPlatform\Ydb\Retry\RetryParams;
1617

@@ -51,11 +52,16 @@ class Table
5152
*/
5253
protected $credentials;
5354

55+
/**
56+
* @var Retry
57+
*/
58+
private $retry;
59+
5460
/**
5561
* @param Ydb $ydb
5662
* @param LoggerInterface|null $logger
5763
*/
58-
public function __construct(Ydb $ydb, LoggerInterface $logger = null)
64+
public function __construct(Ydb $ydb, LoggerInterface $logger = null, Retry &$retry)
5965
{
6066
$this->client = new ServiceClient($ydb->endpoint(), [
6167
'credentials' => $ydb->iam()->getCredentials(),
@@ -69,6 +75,8 @@ public function __construct(Ydb $ydb, LoggerInterface $logger = null)
6975

7076
$this->logger = $logger;
7177

78+
$this->retry = $retry;
79+
7280
if (empty(static::$session_pool))
7381
{
7482
static::$session_pool = new Sessions\MemorySessionPool;
@@ -426,13 +434,20 @@ protected function streamRequest($method, array $data = [])
426434
return $this->doStreamRequest('Table', $method, $data);
427435
}
428436

437+
/**
438+
* @param RetryParams $params
439+
*/
440+
public function setRetryParams(RetryParams $params): void
441+
{
442+
$this->retry = $this->retry->withParams($params);
443+
}
444+
429445
/**
430446
* @throws NonRetryableException
431447
* @throws RetryableException
432448
*/
433449
public function retrySession(Closure $userFunc, bool $idempotent = false, RetryParams $params = null){
434-
$retry = new Retry($params);
435-
return $retry->retry(function () use ($userFunc){
450+
return $this->retry->withParams($params)->retry(function () use ($userFunc){
436451
$sessionId = null;
437452
try{
438453
$session = $this->session();

src/Ydb.php

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Closure;
66
use Psr\Log\LoggerInterface;
77
use YdbPlatform\Ydb\Exceptions\NonRetryableException;
8-
use YdbPlatform\Ydb\Exceptions\Ydb\BadSessionException;
8+
use YdbPlatform\Ydb\Exceptions\RetryableException;
99
use YdbPlatform\Ydb\Retry\Retry;
1010
use YdbPlatform\Ydb\Retry\RetryParams;
1111

@@ -70,6 +70,11 @@ class Ydb
7070
*/
7171
protected $logger;
7272

73+
/**
74+
* @var Retry
75+
*/
76+
protected $retry;
77+
7378
/**
7479
* @param array $config
7580
* @param LoggerInterface|null $logger
@@ -93,6 +98,8 @@ public function __construct($config = [], LoggerInterface $logger = null)
9398
$this->discover();
9499
}
95100

101+
$this->retry = new Retry();
102+
96103
$this->logger()->info('YDB: Initialized');
97104
}
98105

@@ -219,7 +226,7 @@ public function table()
219226
{
220227
if (!isset($this->table))
221228
{
222-
$this->table = new Table($this, $this->logger);
229+
$this->table = new Table($this, $this->logger, $this->retry);
223230
}
224231

225232
return $this->table;
@@ -263,18 +270,30 @@ public function scripting()
263270

264271
return $this->scripting;
265272
}
273+
266274
/**
275+
* @param RetryParams $params
276+
*/
277+
public function setRetryParams(RetryParams $params): void
278+
{
279+
$this->retry = $this->retry->withParams($params);
280+
}
281+
282+
/**
283+
* @param Closure $userFunc
284+
* @param bool $idempotent
285+
* @param RetryParams|null $params
286+
* @return mixed
267287
* @throws NonRetryableException
288+
* @throws RetryableException
268289
*/
269-
public function retry(Closure $userFunc, bool $idempotent = false, RetryParams $params){
270-
$retry = new Retry($params);
271-
return $retry->retry(function () use ($userFunc){null;
290+
public function retry(Closure $userFunc, bool $idempotent = false, RetryParams $params = null){
291+
return $this->retry->withParams($params)->retry(function () use ($userFunc){
272292
try{
273293
return $userFunc($this);
274294
}catch (\Exception $bse){
275295
throw $bse;
276296
}
277297
}, $idempotent);
278-
279298
}
280299
}

0 commit comments

Comments
 (0)