Skip to content

Commit 955d057

Browse files
authored
Merge pull request #142 from php-http/rename-retry-exception-callback
rename decider to exception_decider to make room for response_decider in version 2
2 parents 00f4c37 + c2ed0d6 commit 955d057

File tree

3 files changed

+60
-12
lines changed

3 files changed

+60
-12
lines changed

CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Change Log
22

3+
## 1.9.0 - [unreleased]
4+
5+
### Changed
6+
7+
- [RetryPlugin] Renamed the configuration options for the exception retry callback from `decider` to `exception_decider`
8+
and `delay` to `exception_delay`. The old names still work but are deprecated.
9+
310
## 1.8.2 - 2018-12-14
411

512
### Changed

spec/Plugin/RetryPluginSpec.php

+24
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,30 @@ function it_returns_response_on_second_try(RequestInterface $request, ResponseIn
8080
$promise->wait()->shouldReturn($response);
8181
}
8282

83+
function it_respects_custom_exception_decider(RequestInterface $request, ResponseInterface $response)
84+
{
85+
$this->beConstructedWith([
86+
'exception_decider' => function (RequestInterface $request, Exception $e) {
87+
return false;
88+
}
89+
]);
90+
$exception = new Exception\NetworkException('Exception', $request->getWrappedObject());
91+
92+
$called = false;
93+
$next = function (RequestInterface $receivedRequest) use($exception, &$called) {
94+
if ($called) {
95+
throw new \RuntimeException('Did not expect to be called multiple times');
96+
}
97+
$called = true;
98+
99+
return new HttpRejectedPromise($exception);
100+
};
101+
102+
$promise = $this->handleRequest($request, $next, function () {});
103+
$promise->shouldReturnAnInstanceOf('Http\Client\Promise\HttpRejectedPromise');
104+
$promise->shouldThrow($exception)->duringWait();
105+
}
106+
83107
function it_does_not_keep_history_of_old_failure(RequestInterface $request, ResponseInterface $response)
84108
{
85109
$exception = new Exception\NetworkException('Exception 1', $request->getWrappedObject());

src/Plugin/RetryPlugin.php

+29-12
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ final class RetryPlugin implements Plugin
2727
/**
2828
* @var callable
2929
*/
30-
private $delay;
30+
private $exceptionDelay;
3131

3232
/**
3333
* @var callable
3434
*/
35-
private $decider;
35+
private $exceptionDecider;
3636

3737
/**
3838
* Store the retry counter for each request.
@@ -45,28 +45,45 @@ final class RetryPlugin implements Plugin
4545
* @param array $config {
4646
*
4747
* @var int $retries Number of retries to attempt if an exception occurs before letting the exception bubble up.
48-
* @var callable $decider A callback that gets a request and an exception to decide after a failure whether the request should be retried.
49-
* @var callable $delay A callback that gets a request, an exception and the number of retries and returns how many microseconds we should wait before trying again.
48+
* @var callable $exception_decider A callback that gets a request and an exception to decide after a failure whether the request should be retried.
49+
* @var callable $exception_delay A callback that gets a request, an exception and the number of retries and returns how many microseconds we should wait before trying again.
5050
* }
5151
*/
5252
public function __construct(array $config = [])
5353
{
54+
if (array_key_exists('decider', $config)) {
55+
if (array_key_exists('exception_decider', $config)) {
56+
throw new \InvalidArgumentException('Do not set both the old "decider" and new "exception_decider" options');
57+
}
58+
trigger_error('The "decider" option has been deprecated in favour of "exception_decider"', E_USER_DEPRECATED);
59+
$config['exception_decider'] = $config['decider'];
60+
unset($config['decider']);
61+
}
62+
if (array_key_exists('delay', $config)) {
63+
if (array_key_exists('exception_delay', $config)) {
64+
throw new \InvalidArgumentException('Do not set both the old "delay" and new "exception_delay" options');
65+
}
66+
trigger_error('The "delay" option has been deprecated in favour of "exception_delay"', E_USER_DEPRECATED);
67+
$config['exception_delay'] = $config['delay'];
68+
unset($config['delay']);
69+
}
70+
5471
$resolver = new OptionsResolver();
5572
$resolver->setDefaults([
5673
'retries' => 1,
57-
'decider' => function (RequestInterface $request, Exception $e) {
74+
'exception_decider' => function (RequestInterface $request, Exception $e) {
5875
return true;
5976
},
60-
'delay' => __CLASS__.'::defaultDelay',
77+
'exception_delay' => __CLASS__.'::defaultDelay',
6178
]);
6279
$resolver->setAllowedTypes('retries', 'int');
63-
$resolver->setAllowedTypes('decider', 'callable');
64-
$resolver->setAllowedTypes('delay', 'callable');
80+
$resolver->setAllowedTypes('exception_decider', 'callable');
81+
$resolver->setAllowedTypes('exception_delay', 'callable');
6582
$options = $resolver->resolve($config);
6683

6784
$this->retry = $options['retries'];
68-
$this->decider = $options['decider'];
69-
$this->delay = $options['delay'];
85+
$this->exceptionDecider = $options['exception_decider'];
86+
$this->exceptionDelay = $options['exception_delay'];
7087
}
7188

7289
/**
@@ -93,11 +110,11 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
93110
throw $exception;
94111
}
95112

96-
if (!call_user_func($this->decider, $request, $exception)) {
113+
if (!call_user_func($this->exceptionDecider, $request, $exception)) {
97114
throw $exception;
98115
}
99116

100-
$time = call_user_func($this->delay, $request, $exception, $this->retryStorage[$chainIdentifier]);
117+
$time = call_user_func($this->exceptionDelay, $request, $exception, $this->retryStorage[$chainIdentifier]);
101118
usleep($time);
102119

103120
// Retry in synchrone

0 commit comments

Comments
 (0)