Skip to content

Commit 15bb4c9

Browse files
committed
Changed ConnectionContext::retryAsync to accept generator function.
To reducer boilerplate in async connectors, we now only accept a Closure that returns a Generator, instead of any callable. We cannot accept a Promise because it would most likely be a Coroutine that cannot be cloned because it wraps a generator that cannot be cloned.
1 parent 840dc13 commit 15bb4c9

2 files changed

Lines changed: 15 additions & 9 deletions

File tree

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"require": {
1212
"php": "^7.1",
1313
"scriptfusion/static-class": "^1",
14-
"scriptfusion/retry": "^1.2",
14+
"scriptfusion/retry": "^2",
1515
"scriptfusion/retry-exception-handlers": "^1.1",
1616
"amphp/amp": "^2",
1717
"psr/container": "^1",

src/Connector/ConnectionContext.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
namespace ScriptFUSION\Porter\Connector;
33

4+
use Amp\Coroutine;
45
use Amp\Promise;
56
use ScriptFUSION\Porter\Connector\FetchExceptionHandler\FetchExceptionHandler;
67
use ScriptFUSION\Porter\Connector\FetchExceptionHandler\StatelessFetchExceptionHandler;
@@ -46,8 +47,8 @@ public function mustCache(): bool
4647
}
4748

4849
/**
49-
* Retries the specified callback a predefined number of times with the specified resource fetch exception handler,
50-
* if set, and then the predefined fetch exception handler.
50+
* Retries the specified callback up to a predefined number of times. If it throws a recoverable exception, the
51+
* resource fetch exception handler is invoked, if set, and then the predefined fetch exception handler.
5152
*
5253
* @param callable $callback Callback.
5354
*
@@ -63,16 +64,21 @@ public function retry(callable $callback)
6364
}
6465

6566
/**
66-
* Retries the specified callback asynchronously a predefined number of times with the specified resource fetch
67-
* exception handler, if set, and then the predefined fetch exception handler.
67+
* Closes over the specified async generator function with a static factory method that invokes it as a coroutine
68+
* and retries it up to the predefined number of fetch attempts. If it throws a recoverable exception, the resource
69+
* fetch exception handler is invoked, if set, and then the predefined fetch exception handler.
6870
*
69-
* @param callable $callback
71+
* @param \Closure $asyncGenerator Async generator function.
72+
*
73+
* @return Promise The result returned by the async function.
7074
*/
71-
public function retryAsync(callable $callback): Promise
75+
public function retryAsync(\Closure $asyncGenerator): Promise
7276
{
7377
return \ScriptFUSION\Retry\retryAsync(
7478
$this->maxFetchAttempts,
75-
$callback,
79+
static function () use ($asyncGenerator): Coroutine {
80+
return new Coroutine($asyncGenerator());
81+
},
7682
$this->createExceptionHandler()
7783
);
7884
}
@@ -81,7 +87,7 @@ private function createExceptionHandler(): \Closure
8187
{
8288
$userHandlerCloned = $providerHandlerCloned = false;
8389

84-
return function (\Exception $exception) use (&$userHandlerCloned, &$providerHandlerCloned) {
90+
return function (\Exception $exception) use (&$userHandlerCloned, &$providerHandlerCloned): void {
8591
// Throw exception instead of retrying, if unrecoverable.
8692
if (!$exception instanceof RecoverableConnectorException) {
8793
throw $exception;

0 commit comments

Comments
 (0)