Skip to content

Commit

Permalink
Merge pull request #527 from nerg4l/fix/memory-consumption
Browse files Browse the repository at this point in the history
Reduced memory consumption
  • Loading branch information
oscarotero committed Nov 30, 2023
2 parents 530593a + a28ea08 commit 8a5d7a1
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
22 changes: 17 additions & 5 deletions src/Http/CurlDispatcher.php
Expand Up @@ -7,18 +7,23 @@
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Message\StreamInterface;

/**
* Class to fetch html pages
*/
final class CurlDispatcher
{
private static int $contentLengthThreshold = 5000000;

private RequestInterface $request;
private StreamFactoryInterface $streamFactory;
private $curl;
private $result;
private array $headers = [];
private $isBinary = false;
private $body;
private ?StreamInterface $body = null;
private ?int $error = null;
private array $settings;

Expand Down Expand Up @@ -77,11 +82,12 @@ public static function fetch(array $settings, ResponseFactoryInterface $response
);
}

private function __construct(array $settings, RequestInterface $request)
private function __construct(array $settings, RequestInterface $request, StreamFactoryInterface $streamFactory = null)
{
$this->request = $request;
$this->curl = curl_init((string) $request->getUri());
$this->settings = $settings;
$this->streamFactory = $streamFactory ?? FactoryDiscovery::getStreamFactory();

$cookies = $settings['cookies_path'] ?? str_replace('//', '/', sys_get_temp_dir().'/embed-cookies.txt');

Expand Down Expand Up @@ -136,7 +142,9 @@ private function exec(ResponseFactoryInterface $responseFactory): ResponseInterf

if ($this->body) {
//5Mb max
$response->getBody()->write(stream_get_contents($this->body, 5000000, 0));
$this->body->rewind();
$response = $response->withBody($this->body);
$this->body = null;
}

return $response;
Expand Down Expand Up @@ -199,9 +207,13 @@ private function writeBody($curl, $string): int
}

if (!$this->body) {
$this->body = fopen('php://temp', 'w+');
$this->body = $this->streamFactory->createStreamFromFile('php://temp', 'w+');
}

if ($this->body->getSize() > self::$contentLengthThreshold) {
return count($string);
}

return fwrite($this->body, $string);
return $this->body->write($string);
}
}
20 changes: 20 additions & 0 deletions src/Http/FactoryDiscovery.php
Expand Up @@ -5,6 +5,7 @@

use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Message\UriFactoryInterface;
use RuntimeException;

Expand Down Expand Up @@ -34,6 +35,14 @@ abstract class FactoryDiscovery
'Sunrise\Http\Message\UriFactory',
];

private const STREAM = [
'Laminas\Diactoros\StreamFactory',
'GuzzleHttp\Psr7\HttpFactory',
'Slim\Psr7\Factory\StreamFactory',
'Nyholm\Psr7\Factory\Psr17Factory',
'Sunrise\Http\Message\StreamFactory',
];

public static function getRequestFactory(): RequestFactoryInterface
{
if ($class = self::searchClass(self::REQUEST)) {
Expand All @@ -57,6 +66,17 @@ public static function getUriFactory(): UriFactoryInterface
if ($class = self::searchClass(self::URI)) {
return new $class();
}

throw new RuntimeException('No UriFactoryInterface detected');
}

public static function getStreamFactory(): StreamFactoryInterface
{
if ($class = self::searchClass(self::STREAM)) {
return new $class();
}

throw new RuntimeException('No StreamFactoryInterface detected');
}

private static function searchClass($classes): ?string
Expand Down

0 comments on commit 8a5d7a1

Please sign in to comment.