Skip to content

Commit

Permalink
Make one download queue
Browse files Browse the repository at this point in the history
  • Loading branch information
akondas committed Sep 2, 2020
1 parent 5de9cb7 commit d56e155
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
4 changes: 4 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ parameters:
message: "#^Property .+\\:\\:\\$key is unused\\.$#"
count: 1
path: src/Entity/Config.php
-
message: "#^Variable method call on React\\\\Http\\\\Browser\\.$#"
count: 1
path: src/Service/Downloader/ReactDownloader.php

bootstrapFiles:
- vendor/twig/twig/src/Extension/CoreExtension.php # twig global functions
Expand Down
14 changes: 5 additions & 9 deletions src/Service/Downloader/ReactDownloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,14 @@ final class ReactDownloader implements Downloader
{
private LoopInterface $loop;
private Browser $browser;
private Queue $headQueue;
private Queue $getQueue;
private Queue $queue;

public function __construct()
{
$this->loop = Factory::create();
$this->browser = new Browser($this->loop, new Connector($this->loop, ['timeout' => 10]));
$this->headQueue = new Queue(50, null, function (string $url): PromiseInterface {
return $this->browser->head($url, ['User-Agent' => $this->userAgent()]);
});
$this->getQueue = new Queue(50, null, function (string $url, array $headers): PromiseInterface {
return $this->browser->get($url, array_merge($headers, ['User-Agent' => $this->userAgent()]));
$this->queue = new Queue(100, null, function (string $type, string $url, array $headers = []): PromiseInterface {
return $this->browser->{$type}($url, array_merge($headers, ['User-Agent' => $this->userAgent()]));
});
}

Expand Down Expand Up @@ -59,7 +55,7 @@ public function getContents(string $url, array $headers = [], callable $notFound

public function getAsyncContents(string $url, array $headers, callable $onFulfilled): void
{
($this->getQueue)($url, $headers)
($this->queue)('get', $url, $headers)
->then(function (ResponseInterface $response) use ($onFulfilled): void {
$stream = $response->getBody()->detach();
if (!is_resource($stream)) {
Expand All @@ -74,7 +70,7 @@ public function getAsyncContents(string $url, array $headers, callable $onFulfil
*/
public function getLastModified(string $url, callable $onFulfilled): void
{
($this->headQueue)($url)->then(function (ResponseInterface $response) use ($onFulfilled): void {
($this->queue)('head', $url)->then(function (ResponseInterface $response) use ($onFulfilled): void {
$lastModified = $response->getHeader('Last-Modified');
if ($lastModified !== []) {
$onFulfilled((int) strtotime($lastModified[0]));
Expand Down

0 comments on commit d56e155

Please sign in to comment.