1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Guzzle\Plugin\Backoff; |
4
|
|
|
|
5
|
|
|
use Guzzle\Http\Message\RequestInterface; |
6
|
|
|
use Guzzle\Http\Message\Response; |
7
|
|
|
use Guzzle\Http\Exception\HttpException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Abstract backoff strategy that allows for a chain of responsibility |
11
|
|
|
*/ |
12
|
|
|
abstract class AbstractBackoffStrategy implements BackoffStrategyInterface |
13
|
|
|
{ |
14
|
|
|
/** @var AbstractBackoffStrategy Next strategy in the chain */ |
15
|
|
|
protected $next; |
16
|
|
|
|
17
|
|
|
/** @param AbstractBackoffStrategy $next Next strategy in the chain */ |
18
|
|
|
public function setNext(AbstractBackoffStrategy $next) |
19
|
|
|
{ |
20
|
|
|
$this->next = $next; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Get the next backoff strategy in the chain |
25
|
|
|
* |
26
|
|
|
* @return AbstractBackoffStrategy|null |
27
|
|
|
*/ |
28
|
|
|
public function getNext() |
29
|
|
|
{ |
30
|
|
|
return $this->next; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function getBackoffPeriod( |
34
|
|
|
$retries, |
35
|
|
|
RequestInterface $request, |
36
|
|
|
Response $response = null, |
37
|
|
|
HttpException $e = null |
38
|
|
|
) { |
39
|
|
|
$delay = $this->getDelay($retries, $request, $response, $e); |
40
|
|
|
if ($delay === false) { |
41
|
|
|
// The strategy knows that this must not be retried |
42
|
|
|
return false; |
43
|
|
|
} elseif ($delay === null) { |
44
|
|
|
// If the strategy is deferring a decision and the next strategy will not make a decision then return false |
45
|
|
|
return !$this->next || !$this->next->makesDecision() |
46
|
|
|
? false |
47
|
|
|
: $this->next->getBackoffPeriod($retries, $request, $response, $e); |
48
|
|
|
} elseif ($delay === true) { |
49
|
|
|
// if the strategy knows that it must retry but is deferring to the next to determine the delay |
50
|
|
|
if (!$this->next) { |
51
|
|
|
return 0; |
52
|
|
|
} else { |
53
|
|
|
$next = $this->next; |
54
|
|
|
while ($next->makesDecision() && $next->getNext()) { |
55
|
|
|
$next = $next->getNext(); |
56
|
|
|
} |
57
|
|
|
return !$next->makesDecision() ? $next->getBackoffPeriod($retries, $request, $response, $e) : 0; |
58
|
|
|
} |
59
|
|
|
} else { |
60
|
|
|
return $delay; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Check if the strategy does filtering and makes decisions on whether or not to retry. |
66
|
|
|
* |
67
|
|
|
* Strategies that return false will never retry if all of the previous strategies in a chain defer on a backoff |
68
|
|
|
* decision. |
69
|
|
|
* |
70
|
|
|
* @return bool |
71
|
|
|
*/ |
72
|
|
|
abstract public function makesDecision(); |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Implement the concrete strategy |
76
|
|
|
* |
77
|
|
|
* @param int $retries Number of retries of the request |
78
|
|
|
* @param RequestInterface $request Request that was sent |
79
|
|
|
* @param Response $response Response that was received. Note that there may not be a response |
80
|
|
|
* @param HttpException $e Exception that was encountered if any |
81
|
|
|
* |
82
|
|
|
* @return bool|int|null Returns false to not retry or the number of seconds to delay between retries. Return true |
83
|
|
|
* or null to defer to the next strategy if available, and if not, return 0. |
84
|
|
|
*/ |
85
|
|
|
abstract protected function getDelay( |
86
|
|
|
$retries, |
87
|
|
|
RequestInterface $request, |
88
|
|
|
Response $response = null, |
89
|
|
|
HttpException $e = null |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
|