Skip to content

Commit d375665

Browse files
committed
Merge remote-tracking branch 'origin/master' into 2.x
2 parents 9472484 + 955d057 commit d375665

File tree

4 files changed

+62
-14
lines changed

4 files changed

+62
-14
lines changed

CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@
1414
### Removed
1515
- Deprecated option `debug_plugins` has been removed from `PluginClient`
1616

17+
## 1.9.0 - [unreleased]
18+
19+
### Changed
20+
21+
- [RetryPlugin] Renamed the configuration options for the exception retry callback from `decider` to `exception_decider`
22+
and `delay` to `exception_delay`. The old names still work but are deprecated.
23+
1724
## 1.8.2 - 2018-12-14
1825

1926
### Changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
"extra": {
4747
"branch-alias": {
4848
"dev-2.x": "2.x-dev",
49-
"dev-master": "1.8-dev"
49+
"dev-master": "1.9.x-dev"
5050
}
5151
}
5252
}

spec/Plugin/RetryPluginSpec.php

+24
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,30 @@ public function it_returns_response_on_second_try(RequestInterface $request, Res
104104
$promise->wait()->shouldReturn($response);
105105
}
106106

107+
public function it_respects_custom_exception_decider(RequestInterface $request, ResponseInterface $response)
108+
{
109+
$this->beConstructedWith([
110+
'exception_decider' => function (RequestInterface $request, Exception $e) {
111+
return false;
112+
}
113+
]);
114+
$exception = new Exception\NetworkException('Exception', $request->getWrappedObject());
115+
116+
$called = false;
117+
$next = function (RequestInterface $receivedRequest) use($exception, &$called) {
118+
if ($called) {
119+
throw new \RuntimeException('Did not expect to be called multiple times');
120+
}
121+
$called = true;
122+
123+
return new HttpRejectedPromise($exception);
124+
};
125+
126+
$promise = $this->handleRequest($request, $next, function () {});
127+
$promise->shouldReturnAnInstanceOf('Http\Client\Promise\HttpRejectedPromise');
128+
$promise->shouldThrow($exception)->duringWait();
129+
}
130+
107131
public function it_does_not_keep_history_of_old_failure(RequestInterface $request, ResponseInterface $response)
108132
{
109133
$exception = new Exception\NetworkException('Exception 1', $request->getWrappedObject());

src/Plugin/RetryPlugin.php

+30-13
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ final class RetryPlugin implements Plugin
3131
/**
3232
* @var callable
3333
*/
34-
private $delay;
34+
private $exceptionDelay;
3535

3636
/**
3737
* @var callable
3838
*/
39-
private $decider;
39+
private $exceptionDecider;
4040

4141
/**
4242
* Store the retry counter for each request.
@@ -48,30 +48,47 @@ final class RetryPlugin implements Plugin
4848
/**
4949
* @param array $config {
5050
*
51-
* @var int $retries Number of retries to attempt if an exception occurs before letting the exception bubble up
52-
* @var callable $decider A callback that gets a request and an exception to decide after a failure whether the request should be retried
53-
* @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.
51+
* @var int $retries Number of retries to attempt if an exception occurs before letting the exception bubble up.
52+
* @var callable $exception_decider A callback that gets a request and an exception to decide after a failure whether the request should be retried.
53+
* @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.
5454
* }
5555
*/
5656
public function __construct(array $config = [])
5757
{
58+
if (array_key_exists('decider', $config)) {
59+
if (array_key_exists('exception_decider', $config)) {
60+
throw new \InvalidArgumentException('Do not set both the old "decider" and new "exception_decider" options');
61+
}
62+
trigger_error('The "decider" option has been deprecated in favour of "exception_decider"', E_USER_DEPRECATED);
63+
$config['exception_decider'] = $config['decider'];
64+
unset($config['decider']);
65+
}
66+
if (array_key_exists('delay', $config)) {
67+
if (array_key_exists('exception_delay', $config)) {
68+
throw new \InvalidArgumentException('Do not set both the old "delay" and new "exception_delay" options');
69+
}
70+
trigger_error('The "delay" option has been deprecated in favour of "exception_delay"', E_USER_DEPRECATED);
71+
$config['exception_delay'] = $config['delay'];
72+
unset($config['delay']);
73+
}
74+
5875
$resolver = new OptionsResolver();
5976
$resolver->setDefaults([
6077
'retries' => 1,
61-
'decider' => function (RequestInterface $request, Exception $e) {
78+
'exception_decider' => function (RequestInterface $request, Exception $e) {
6279
// do not retry client errors
6380
return !$e instanceof HttpException || $e->getCode() >= 500;
6481
},
65-
'delay' => __CLASS__.'::defaultDelay',
82+
'exception_delay' => __CLASS__.'::defaultDelay',
6683
]);
6784
$resolver->setAllowedTypes('retries', 'int');
68-
$resolver->setAllowedTypes('decider', 'callable');
69-
$resolver->setAllowedTypes('delay', 'callable');
85+
$resolver->setAllowedTypes('exception_decider', 'callable');
86+
$resolver->setAllowedTypes('exception_delay', 'callable');
7087
$options = $resolver->resolve($config);
7188

7289
$this->retry = $options['retries'];
73-
$this->decider = $options['decider'];
74-
$this->delay = $options['delay'];
90+
$this->exceptionDecider = $options['exception_decider'];
91+
$this->exceptionDelay = $options['exception_delay'];
7592
}
7693

7794
/**
@@ -98,11 +115,11 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
98115
throw $exception;
99116
}
100117

101-
if (!call_user_func($this->decider, $request, $exception)) {
118+
if (!call_user_func($this->exceptionDecider, $request, $exception)) {
102119
throw $exception;
103120
}
104121

105-
$time = call_user_func($this->delay, $request, $exception, $this->retryStorage[$chainIdentifier]);
122+
$time = call_user_func($this->exceptionDelay, $request, $exception, $this->retryStorage[$chainIdentifier]);
106123
usleep($time);
107124

108125
// Retry synchronously

0 commit comments

Comments
 (0)