Issues (200)

src/Inspector/RetryingLoopProvider.php (1 issue)

Severity
1
<?php
2
3
/**
4
 * This file is part of the sj-i/php-profiler package.
5
 *
6
 * (c) sji <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace PhpProfiler\Inspector;
15
16
use PhpProfiler\Lib\Loop\LoopBuilder;
17
use PhpProfiler\Lib\Loop\LoopMiddleware\CallableMiddleware;
18
use PhpProfiler\Lib\Loop\LoopMiddleware\NanoSleepMiddleware;
19
use PhpProfiler\Lib\Loop\LoopMiddleware\RetryOnExceptionMiddleware;
20
21
class RetryingLoopProvider
22
{
23
    public function __construct(
24
        private LoopBuilder $loop_builder
25
    ) {
26
    }
27
28
    /**
29
     * @template T
30
     * @param callable():T $try
31
     * @param class-string<\Throwable>[] $retry_on
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<\Throwable>[] at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<\Throwable>[].
Loading history...
32
     * @return T
33
     */
34
    public function do(
35
        callable $try,
36
        array $retry_on,
37
        int $max_retry,
38
        int $interval_on_retry_ns,
39
    ) {
40
        $result = null;
41
42
        $loop_canceller =
43
            /** @param-out T $result */
44
            function () use (&$result, $try): bool {
45
                $result = $try();
46
                // one successful execution is enough
47
                return false;
48
            }
49
        ;
50
51
        $this->loop_builder
52
            ->addProcess(RetryOnExceptionMiddleware::class, [$max_retry, $retry_on])
53
            ->addProcess(NanoSleepMiddleware::class, [$interval_on_retry_ns])
54
            ->addProcess(CallableMiddleware::class, [$loop_canceller])
55
            ->build()
56
            ->invoke();
57
58
        /** @var T */
59
        return $result;
60
    }
61
}
62