1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Http\Client\Plugin; |
4
|
|
|
|
5
|
1 |
|
@trigger_error('The '.__NAMESPACE__.'\RetryPlugin class is deprecated since version 1.1 and will be removed in 2.0. Use Http\Client\Common\Plugin\RetryPlugin instead.', E_USER_DEPRECATED); |
6
|
|
|
|
7
|
|
|
use Http\Client\Exception; |
8
|
|
|
use Psr\Http\Message\RequestInterface; |
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
10
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Retry the request if an exception is thrown. |
14
|
|
|
* |
15
|
|
|
* By default will retry only one time. |
16
|
|
|
* |
17
|
|
|
* @author Joel Wurtz <[email protected]> |
18
|
|
|
* |
19
|
|
|
* @deprecated since since version 1.1, and will be removed in 2.0. Use {@link \Http\Client\Common\Plugin\RetryPlugin} instead. |
20
|
|
|
*/ |
21
|
|
|
class RetryPlugin implements Plugin |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Number of retry before sending an exception. |
25
|
|
|
* |
26
|
|
|
* @var int |
27
|
|
|
*/ |
28
|
|
|
private $retry; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Store the retry counter for each request. |
32
|
|
|
* |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
private $retryStorage = []; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param array $config { |
39
|
|
|
* |
40
|
|
|
* @var int $retries Number of retries to attempt if an exception occurs before letting the exception bubble up. |
41
|
|
|
* } |
42
|
|
|
*/ |
43
|
6 |
View Code Duplication |
public function __construct(array $config = []) |
|
|
|
|
44
|
|
|
{ |
45
|
6 |
|
$resolver = new OptionsResolver(); |
46
|
6 |
|
$resolver->setDefaults([ |
47
|
6 |
|
'retries' => 1, |
48
|
6 |
|
]); |
49
|
6 |
|
$resolver->setAllowedTypes('retries', 'int'); |
50
|
6 |
|
$options = $resolver->resolve($config); |
51
|
|
|
|
52
|
6 |
|
$this->retry = $options['retries']; |
53
|
6 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritdoc} |
57
|
|
|
*/ |
58
|
4 |
|
public function handleRequest(RequestInterface $request, callable $next, callable $first) |
59
|
|
|
{ |
60
|
4 |
|
$chainIdentifier = spl_object_hash((object) $first); |
61
|
|
|
|
62
|
|
|
return $next($request)->then(function (ResponseInterface $response) use ($request, $chainIdentifier) { |
63
|
3 |
|
if (array_key_exists($chainIdentifier, $this->retryStorage)) { |
64
|
2 |
|
unset($this->retryStorage[$chainIdentifier]); |
65
|
2 |
|
} |
66
|
|
|
|
67
|
3 |
|
return $response; |
68
|
4 |
|
}, function (Exception $exception) use ($request, $next, $first, $chainIdentifier) { |
69
|
3 |
|
if (!array_key_exists($chainIdentifier, $this->retryStorage)) { |
70
|
3 |
|
$this->retryStorage[$chainIdentifier] = 0; |
71
|
3 |
|
} |
72
|
|
|
|
73
|
3 |
|
if ($this->retryStorage[$chainIdentifier] >= $this->retry) { |
74
|
1 |
|
unset($this->retryStorage[$chainIdentifier]); |
75
|
|
|
|
76
|
1 |
|
throw $exception; |
77
|
|
|
} |
78
|
|
|
|
79
|
3 |
|
++$this->retryStorage[$chainIdentifier]; |
80
|
|
|
|
81
|
|
|
// Retry in synchrone |
82
|
3 |
|
$promise = $this->handleRequest($request, $next, $first); |
83
|
|
|
|
84
|
3 |
|
return $promise->wait(); |
85
|
4 |
|
}); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.