Skip to content

Commit

Permalink
Asynchronous requests
Browse files Browse the repository at this point in the history
  • Loading branch information
aivchen committed Jan 23, 2019
1 parent a194a76 commit 396befb
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\TransferException;
use GuzzleHttp\Promise;

class Cache
{
Expand Down Expand Up @@ -44,11 +45,19 @@ public static function create($privateKey)
public function update($url, $contentType = UrlConverter::CONTENT_TYPE_HTML)
{
$failed = [];

$promises = [];
foreach ($this->getCacheServers() as $server) {
try {
$this->client->get($this->urlConverter->convert($url, $server, $contentType),
['timeout' => $this->config->getTimeout()]);
} catch (TransferException $e) {
$promises[$server] = $this->client->getAsync($this->urlConverter->convert($url, $server, $contentType),
['timeout' => $this->config->getTimeout()]);
}

$results = Promise\settle($promises)->wait();

foreach ($results as $server => $result) {
if ($result['state'] === 'rejected') {
/** @var TransferException $e */
$e = $result['reason'];
$failed[] = "Failed to update $url url on $server: {$e->getMessage()}";
}
}
Expand Down

0 comments on commit 396befb

Please sign in to comment.