• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

predis / predis / 20351138744

18 Dec 2025 09:00PM UTC coverage: 92.603% (-0.2%) from 92.788%
20351138744

Pull #1616

github

web-flow
Merge 4023b4e91 into 0ae180c94
Pull Request #1616: Added retry support

301 of 358 new or added lines in 18 files covered. (84.08%)

3 existing lines in 3 files now uncovered.

8213 of 8869 relevant lines covered (92.6%)

112.14 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.94
/src/Retry/Retry.php
1
<?php
2

3
/*
4
 * This file is part of the Predis package.
5
 *
6
 * (c) 2009-2020 Daniele Alessandri
7
 * (c) 2021-2025 Till Krüss
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12

13
namespace Predis\Retry;
14

15
use Predis\Connection\ConnectionException;
16
use Predis\Connection\Resource\Exception\StreamInitException;
17
use Predis\Retry\Strategy\RetryStrategyInterface;
18
use Predis\TimeoutException;
19
use Throwable;
20

21
class Retry
22
{
23
    /**
24
     * @var RetryStrategyInterface
25
     */
26
    protected $backoffStrategy;
27

28
    /**
29
     * @var int
30
     */
31
    protected $retries;
32

33
    /**
34
     * @var array
35
     */
36
    protected $catchableExceptions = [
37
        TimeoutException::class,
38
        ConnectionException::class,
39
        StreamInitException::class,
40
    ];
41

42
    /**
43
     * @param RetryStrategyInterface $backoffStrategy
44
     * @param int                    $retries
45
     * @param array|null             $catchableExceptions A list of exceptions classes that should be caught.
46
     *                                                    Overrides default list of the catchable exceptions.
47
     */
48
    public function __construct(
2,077✔
49
        RetryStrategyInterface $backoffStrategy,
50
        int $retries,
51
        ?array $catchableExceptions = null
52
    ) {
53
        $this->backoffStrategy = $backoffStrategy;
2,077✔
54
        $this->retries = $retries;
2,077✔
55

56
        if (null !== $catchableExceptions) {
2,077✔
57
            $this->catchableExceptions = $catchableExceptions;
9✔
58
        }
59
    }
60

61
    /**
62
     * Update the retry count.
63
     *
64
     * @param  int  $retries
65
     * @return void
66
     */
67
    public function updateRetriesCount(int $retries): void
4✔
68
    {
69
        $this->retries = $retries;
4✔
70
    }
71

72
    /**
73
     * Extend catchable exceptions list.
74
     *
75
     * @param  array $catchableExceptions
76
     * @return void
77
     */
78
    public function updateCatchableExceptions(array $catchableExceptions): void
55✔
79
    {
80
        $this->catchableExceptions = array_merge($this->catchableExceptions, $catchableExceptions);
55✔
81
    }
82

83
    /**
84
     * @return int
85
     */
86
    public function getRetries(): int
4✔
87
    {
88
        return $this->retries;
4✔
89
    }
90

91
    /**
92
     * @return RetryStrategyInterface
93
     */
NEW
94
    public function getStrategy(): RetryStrategyInterface
×
95
    {
NEW
96
        return $this->backoffStrategy;
×
97
    }
98

99
    /**
100
     * @param  callable(): mixed              $do
101
     * @param  callable(Throwable): void|null $fail
102
     * @return mixed
103
     * @throws Throwable
104
     */
105
    public function callWithRetry(callable $do, ?callable $fail = null)
1,656✔
106
    {
107
        $failures = 0;
1,656✔
108

109
        while (true) {
1,656✔
110
            try {
111
                return $do();
1,656✔
112
            } catch (Throwable $e) {
43✔
113
                if (null !== $this->catchableExceptions) {
43✔
114
                    $isCatchable = false;
43✔
115
                    foreach ($this->catchableExceptions as $catchableException) {
43✔
116
                        if ($e instanceof $catchableException) {
43✔
117
                            $isCatchable = true;
37✔
118
                        }
119
                    }
120

121
                    if (!$isCatchable) {
43✔
122
                        throw $e;
7✔
123
                    }
124
                }
125

126
                $backoff = $this->backoffStrategy->compute($failures);
37✔
127
                ++$failures;
37✔
128

129
                if ($this->retries >= 0 && $failures > $this->retries) {
37✔
130
                    throw $e;
9✔
131
                }
132

133
                if ($fail !== null) {
37✔
134
                    $fail($e);
33✔
135
                }
136

137
                if ($backoff > 0) {
37✔
138
                    usleep($backoff);
29✔
139
                }
140
            }
141
        }
142
    }
143
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc