Exemplo n.º 1
0
 /**
  * @param string $token
  * @param int $maxRetries
  */
 public function __construct($token, $maxRetries = self::MAX_RETRIES)
 {
     $handler = new GuzzleClient(['base_url' => self::BASE_URL, 'defaults' => ['headers' => ['Content-Type' => 'application/json', 'Accept' => 'application/json', 'Authorization' => sprintf('Bearer %s', $token)]]]);
     $retry = new RetrySubscriber(['filter' => RetrySubscriber::createChainFilter([RetrySubscriber::createCurlFilter([CURLE_PARTIAL_FILE]), RetrySubscriber::createStatusFilter([500, 502, 503, 504])]), 'max' => $maxRetries]);
     $handler->getEmitter()->attach($retry);
     $this->client = new Client($handler);
 }
 public function testCreatesCustomCurlFilter()
 {
     $f = RetrySubscriber::createCurlFilter([CURLE_OK]);
     $e = $this->createEvent(null, null, null, ['curl_result' => CURLE_RECV_ERROR]);
     $this->assertFalse($f(1, $e));
     $e = $this->createEvent(null, null, null, ['curl_result' => CURLE_OK]);
     $this->assertTrue($f(0, $e));
 }
Exemplo n.º 3
0
 /**
  * Creates a Guzzle HTTP client and initializes it with the given configuration.
  * @param Collection $config
  * @return HttpClient
  */
 protected function loadHttpClient(Collection $config)
 {
     $client = new HttpClient($config['http_client_options'] ?: []);
     // attach retry subscriber
     $maxRetries = (int) $config['max_retries'];
     if (0 < $maxRetries) {
         $client->getEmitter()->attach(new RetrySubscriber(['max' => $maxRetries, 'filter' => RetrySubscriber::createChainFilter([RetrySubscriber::createStatusFilter(), RetrySubscriber::createCurlFilter()])]));
     }
     return $client;
 }
Exemplo n.º 4
0
 public function getRetryRaw($url, array $headers = [], $tries = 3, array $status = null, $delay = null, array $customChain = null)
 {
     $status = $status ?: [500, 503];
     $retry = new Retry(['filter' => Retry::createChainFilter($customChain ?: [Retry::createIdempotentFilter(), Retry::createCurlFilter(), Retry::createStatusFilter($status)]), 'max' => $tries, 'delay' => function ($retries, AbstractTransferEvent $e) use($delay) {
         info('Delaying...');
         return $delay ?: (int) pow(2, $retries - 1);
     }]);
     $client = new HttpClient();
     $client->getEmitter()->attach($retry);
     return $client->get($url, ['headers' => $headers]);
 }
Exemplo n.º 5
0
 private function getHttpClientFromConfig(array $config)
 {
     // If a client was provided, return it.
     if (isset($config['http_client'])) {
         return $config['http_client'];
     }
     // Create a Guzzle HttpClient.
     $clientOptions = isset($config['http_client_options']) ? $config['http_client_options'] : [];
     $client = new HttpClient($clientOptions);
     // Attach request retry logic.
     $client->getEmitter()->attach(new RetrySubscriber(['max' => $config['max_retries'], 'filter' => RetrySubscriber::createChainFilter([RetrySubscriber::createStatusFilter(), RetrySubscriber::createCurlFilter()])]));
     return $client;
 }
Exemplo n.º 6
0
 /**
  * Create expontential backoff for GuzzleClient
  *
  * options
  *  - maxRetries: (integer) max retries count
  *  - http
  *      - retryHeader (string) header containing retry time header
  *      - codes (array) list of status codes to retry on
  * - curl
  *      - codes (array) list of error codes to retry on
  *
  * @param array $options
  * @return RetrySubscriber
  */
 private static function createBackoff(array $options)
 {
     $headerName = isset($options['http']['retryHeader']) ? $options['http']['retryHeader'] : 'Retry-After';
     $httpRetryCodes = isset($options['http']['codes']) ? $options['http']['codes'] : [500, 502, 503, 504, 408, 420, 429];
     $maxRetries = isset($options['maxRetries']) ? (int) $options['maxRetries'] : 10;
     $curlRetryCodes = isset($options['curl']['codes']) ? $options['curl']['codes'] : [CURLE_OPERATION_TIMEOUTED, CURLE_COULDNT_RESOLVE_HOST, CURLE_COULDNT_CONNECT, CURLE_SSL_CONNECT_ERROR, CURLE_GOT_NOTHING];
     return new RetrySubscriber(['filter' => RetrySubscriber::createChainFilter([RetrySubscriber::createStatusFilter($httpRetryCodes), RetrySubscriber::createCurlFilter($curlRetryCodes)]), 'max' => $maxRetries, 'delay' => function ($retries, AbstractTransferEvent $event) use($headerName) {
         $delay = self::getRetryDelay($retries, $event, $headerName);
         $errData = ["http_code" => !empty($event->getTransferInfo()['http_code']) ? $event->getTransferInfo()['http_code'] : null, "body" => is_null($event->getResponse()) ? null : (string) $event->getResponse()->getBody(), "url" => !empty($event->getTransferInfo()['url']) ? $event->getTransferInfo()['url'] : $event->getRequest()->getUrl()];
         if ($event instanceof ErrorEvent) {
             $errData["message"] = $event->getException()->getMessage();
         }
         Logger::log("DEBUG", "Http request failed, retrying in {$delay}s", $errData);
         // ms > s
         return 1000 * $delay;
     }]);
 }