考虑以下代码:
public function getItemHistoryForRegion(array $items, array $regions) {
$client = new Client(['expect' => false]);
$acceptedResponses = [];
$rejectedResponses = [];
$createdRequests = [];
$regionAndItem = [];
foreach($items as $item) {
foreach ($regions as $region) {
array_push($regionAndItem, [$item, $region]);
array_push($createdRequests, new Request('GET', 'https://public-crest.eveonline.com/market/'.$region.'/types/'.$item.'/history/'));
}
}
$pool = new Pool($client, $createdRequests, [
'concurrency' => 10,
'fulfilled' => function ($response, $index) use (&$acceptedResponses) {
EveLogHandler::requestLog($response, 'eve_online_region_item_history_responses.log');
$acceptedResponses[$index] = json_decode($response->getBody()->getContents());
},
'rejected' => function ($reason, $index) use(&$rejectedResponses) {
EveLogHandler::messageLog($reason, 'eve_online_region_item_history_rejected_responses.log');
},
]);
$promise = $pool->promise();
$promise->wait();
$historyDetails = new HistoryDetails($acceptedResponses, $regionAndItem);
$historyDetails->createHistoryDetails();
return $historyDetails->getHistoryDetails();
}
我创建了一个请求数组,然后将它们汇集在一起。通常有 10,000 个请求通过这里。
问题之一是被拒绝的部分:
'rejected' => function ($reason, $index) use(&$rejectedResponses) {
EveLogHandler::messageLog($reason, 'eve_online_region_item_history_rejected_responses.log');
},
在这里,我只是注销了被拒绝的消息,它返回为:
[2016-01-27 16:35:00] production.INFO: Message ["[object] (GuzzleHttp\\Exception\\ConnectException(code: 0): cURL error 52: Empty reply from server (see http://curl.haxx.se/libcurl/c/libcurl-errors.html) at /home/ccccc/test_site/vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php:186)"] []
[2016-01-27 16:35:57] production.INFO: Message ["[object] (GuzzleHttp\\Exception\\ConnectException(code: 0): cURL error 52: Empty reply from server (see http://curl.haxx.se/libcurl/c/libcurl-errors.html) at /home/ccccc/test_site/vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php:186)"] []
其中有很多,因为可能有超过 13 个作业排队,每个作业发送 10,000 个请求,正如您所见,我将它们汇集在一起,所以我一次做 10 个。
此 api 的速率限制为每秒 150 个请求。所以我知道我没有达到 150,或者至少我不应该达到。
我的问题是如何重新尝试被拒绝的请求?有没有办法说,失败重试最多 x 次?guzzle 有内置的吗?