protected function tryCall($httpRequest, $attempts = 3, $interval = 200)
{
try {
$httpResponse = \Scalr::getContainer()->http->sendRequest($httpRequest);
if (preg_match('/^<html.+ Service Unavailable/', $httpResponse->getBody()->toString()) && --$attempts > 0) {
usleep($interval);
return $this->tryCall($httpRequest, $attempts, $interval * 2);
}
//Increments the queries quantity
$this->_incrementQueriesQuantity();
$response = new QueryClientResponse($httpResponse);
$response->setRequest($httpRequest);
$response->setQueryNumber($this->getQueriesQuantity());
if ($response->hasError()) {
$eventObserver = $this->getAws()->getEventObserver();
/* @var $clientException ClientException */
$clientException = $response->getException();
//It does not need anymore
//$response->setEventObserver($eventObserver);
if (isset($eventObserver) && $eventObserver->isSubscribed(EventType::EVENT_ERROR_RESPONSE)) {
$eventObserver->fireEvent(new ErrorResponseEvent(array('exception' => $clientException, 'apicall' => $clientException->getApiCall())));
}
if ($clientException->getErrorData() instanceof ErrorData && $clientException->getErrorData()->getCode() == ErrorData::ERR_REQUEST_LIMIT_EXCEEDED) {
if (--$attempts > 0) {
//Tries to handle RequestLimitExceeded AWS Response
$sleepTimeout = 3 * (3 - $attempts);
sleep($sleepTimeout > 0 ? $sleepTimeout : 3);
return $this->tryCall($httpRequest, $attempts, $interval);
}
}
}
if (is_callable($this->callback)) {
call_user_func($this->callback, $httpRequest, $httpResponse);
}
} catch (\http\Exception $e) {
$error = new ErrorData();
$error->message = 'Cannot establish connection to AWS server. ' . (isset($e->innerException) ? preg_replace('/(\\(.*\\))/', '', $e->innerException->getMessage()) : $e->getMessage());
throw new ClientException($error);
}
return $response;
}