Open Journal Systems  3.3.0
BackoffPlugin.php
1 <?php
2 
3 namespace Guzzle\Plugin\Backoff;
4 
12 
17 {
19  const RETRY_PARAM = 'plugins.backoff.retry_count';
20  const RETRY_EVENT = 'plugins.backoff.retry';
21 
23  protected $strategy;
24 
29  public function __construct(BackoffStrategyInterface $strategy = null)
30  {
31  $this->strategy = $strategy;
32  }
33 
43  public static function getExponentialBackoff(
44  $maxRetries = 3,
45  array $httpCodes = null,
46  array $curlCodes = null
47  ) {
48  return new self(new TruncatedBackoffStrategy($maxRetries,
49  new HttpBackoffStrategy($httpCodes,
50  new CurlBackoffStrategy($curlCodes,
52  )
53  )
54  ));
55  }
56 
57  public static function getAllEvents()
58  {
59  return array(self::RETRY_EVENT);
60  }
61 
62  public static function getSubscribedEvents()
63  {
64  return array(
65  'request.sent' => 'onRequestSent',
66  'request.exception' => 'onRequestSent',
67  CurlMultiInterface::POLLING_REQUEST => 'onRequestPoll'
68  );
69  }
70 
76  public function onRequestSent(Event $event)
77  {
78  $request = $event['request'];
79  $response = $event['response'];
80  $exception = $event['exception'];
81 
82  $params = $request->getParams();
83  $retries = (int) $params->get(self::RETRY_PARAM);
84  $delay = $this->strategy->getBackoffPeriod($retries, $request, $response, $exception);
85 
86  if ($delay !== false) {
87  // Calculate how long to wait until the request should be retried
88  $params->set(self::RETRY_PARAM, ++$retries)
89  ->set(self::DELAY_PARAM, microtime(true) + $delay);
90  // Send the request again
91  $request->setState(RequestInterface::STATE_TRANSFER);
92  $this->dispatch(self::RETRY_EVENT, array(
93  'request' => $request,
94  'response' => $response,
95  'handle' => ($exception && $exception instanceof CurlException) ? $exception->getCurlHandle() : null,
96  'retries' => $retries,
97  'delay' => $delay
98  ));
99  }
100  }
101 
107  public function onRequestPoll(Event $event)
108  {
109  $request = $event['request'];
110  $delay = $request->getParams()->get(self::DELAY_PARAM);
111 
112  // If the duration of the delay has passed, retry the request using the pool
113  if (null !== $delay && microtime(true) >= $delay) {
114  // Remove the request from the pool and then add it back again. This is required for cURL to know that we
115  // want to retry sending the easy handle.
116  $request->getParams()->remove(self::DELAY_PARAM);
117  // Rewind the request body if possible
118  if ($request instanceof EntityEnclosingRequestInterface && $request->getBody()) {
119  $request->getBody()->seek(0);
120  }
121  $multi = $event['curl_multi'];
122  $multi->remove($request);
123  $multi->add($request);
124  }
125  }
126 }
Guzzle\Plugin\Backoff\BackoffPlugin\getExponentialBackoff
static getExponentialBackoff( $maxRetries=3, array $httpCodes=null, array $curlCodes=null)
Definition: BackoffPlugin.php:46
Guzzle\Http\Exception\CurlException
Definition: CurlException.php:10
Guzzle\Http\Message\RequestInterface
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Http/Message/RequestInterface.php:16
Guzzle\Http\Curl\CurlMultiInterface\POLLING_REQUEST
const POLLING_REQUEST
Definition: CurlMultiInterface.php:14
Guzzle\Http\Curl\CurlMultiInterface\BLOCKING
const BLOCKING
Definition: CurlMultiInterface.php:18
Guzzle\Plugin\Backoff\BackoffPlugin\getAllEvents
static getAllEvents()
Definition: BackoffPlugin.php:60
Symfony\Component\EventDispatcher\EventSubscriberInterface
Definition: lib/vendor/symfony/event-dispatcher/EventSubscriberInterface.php:25
Guzzle\Http\Message\EntityEnclosingRequestInterface
Definition: EntityEnclosingRequestInterface.php:12
Guzzle\Http\Message\RequestInterface\STATE_TRANSFER
const STATE_TRANSFER
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Http/Message/RequestInterface.php:20
Guzzle\Plugin\Backoff\BackoffPlugin\RETRY_EVENT
const RETRY_EVENT
Definition: BackoffPlugin.php:20
Guzzle\Plugin\Backoff
Definition: AbstractBackoffStrategy.php:3
Guzzle\Plugin\Backoff\BackoffPlugin\onRequestPoll
onRequestPoll(Event $event)
Definition: BackoffPlugin.php:110
Guzzle\Plugin\Backoff\BackoffPlugin
Definition: BackoffPlugin.php:16
Guzzle\Common\Event
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Common/Event.php:10
Guzzle\Plugin\Backoff\BackoffPlugin\getSubscribedEvents
static getSubscribedEvents()
Definition: BackoffPlugin.php:65
Guzzle\Plugin\Backoff\BackoffPlugin\RETRY_PARAM
const RETRY_PARAM
Definition: BackoffPlugin.php:19
Guzzle\Plugin\Backoff\TruncatedBackoffStrategy
Definition: TruncatedBackoffStrategy.php:12
Guzzle\Http\Curl\CurlMultiInterface
Definition: CurlMultiInterface.php:12
Guzzle\Common\AbstractHasDispatcher\dispatch
dispatch($eventName, array $context=array())
Definition: AbstractHasDispatcher.php:41
Guzzle\Plugin\Backoff\HttpBackoffStrategy
Definition: HttpBackoffStrategy.php:14
Guzzle\Plugin\Backoff\BackoffPlugin\__construct
__construct(BackoffStrategyInterface $strategy=null)
Definition: BackoffPlugin.php:32
Guzzle\Common\AbstractHasDispatcher
Definition: AbstractHasDispatcher.php:12
Guzzle\Plugin\Backoff\BackoffPlugin\onRequestSent
onRequestSent(Event $event)
Definition: BackoffPlugin.php:79
Guzzle\Plugin\Backoff\BackoffPlugin\DELAY_PARAM
const DELAY_PARAM
Definition: BackoffPlugin.php:18
Guzzle\Plugin\Backoff\CurlBackoffStrategy
Definition: CurlBackoffStrategy.php:13
Guzzle\Plugin\Backoff\BackoffStrategyInterface
Definition: BackoffStrategyInterface.php:12
Guzzle\Plugin\Backoff\BackoffPlugin\$strategy
$strategy
Definition: BackoffPlugin.php:26
Guzzle\Http\Message\EntityEnclosingRequestInterface\getBody
getBody()
Guzzle\Plugin\Backoff\ExponentialBackoffStrategy
Definition: ExponentialBackoffStrategy.php:14