1
0
mirror of https://github.com/danog/MadelineProto.git synced 2024-11-27 04:35:12 +01:00

Reuse HTTP client for buffering

This commit is contained in:
Daniil Gentili 2023-08-19 17:50:15 +02:00
parent 2537ef5bcd
commit 6f9356b8d0
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7

View File

@ -25,6 +25,7 @@ use Amp\ByteStream\ReadableBuffer;
use Amp\ByteStream\ReadableStream;
use Amp\Cancellation;
use Amp\File\File;
use Amp\Http\Client\HttpClient;
use Amp\Http\Client\HttpClientBuilder;
use Amp\Http\Client\Request;
use ArrayAccess;
@ -613,6 +614,7 @@ abstract class Tools extends AsyncTools
{
return new Pipe(512*1024);
}
private static ?HttpClient $client = null;
/**
* Provide a buffered reader for a file, URL or amp stream.
*
@ -625,7 +627,8 @@ abstract class Tools extends AsyncTools
return fn (int $len): ?string => $stream->read(cancellation: $cancellation, length: $len);
}
if ($stream instanceof RemoteUrl) {
$stream = HttpClientBuilder::buildDefault()->request(new Request($stream->url), $cancellation)->getBody();
self::$client ??= HttpClientBuilder::buildDefault();
$stream = self::$client->request(new Request($stream->url), $cancellation)->getBody();
}
$buffer = '';
return function (int $len) use (&$buffer, $stream, $cancellation): ?string {