http-client icon indicating copy to clipboard operation
http-client copied to clipboard

Over file descriptor when not get body for response

Open xpader opened this issue 1 year ago • 3 comments

Payload will not been destruct from memory when not read body.

while (true) {
    $url = 'Some short url';
    $response = $client->request(new Request($url));
    echo $response->getHeader('Location');
}

You can add a static counter on Payload construct and destruct, will find out.

How is it currently fixed in the own code?

Call $response->getBody()->buffer() or $response->getBody()->close() after read response.

xpader avatar Nov 15 '24 16:11 xpader

May be should use WeakReference in some stream resource, to make it can auto destruct.

xpader avatar Nov 15 '24 16:11 xpader

I can't reproduce this with your script, same for the counters, seems to work well and the body is properly GCed.

<?php declare(strict_types=1);

use Amp\Http\Client\HttpClientBuilder;
use Amp\Http\Client\HttpException;
use Amp\Http\Client\Request;

require __DIR__ . '/../.helper/functions.php';

while (true) {
    try {
        // Instantiate the HTTP client
        $client = HttpClientBuilder::buildDefault();

        // Make an asynchronous HTTP request
        $response = $client->request(new Request($argv[1] ?? 'https://httpbin.org/user-agent'));
        $header = $response->getHeader('Location');

        echo memory_get_usage(true) . PHP_EOL;
    } catch (HttpException $error) {
        echo $error;
    }
}

kelunik avatar Nov 19 '24 20:11 kelunik

Try this, use a single pool instance.

I'm now using php8.1 with ev extension on ubuntu 20.04 amd64.

<?php declare(strict_types=1);

use Amp\Http\Client\HttpClientBuilder;
use Amp\Http\Client\HttpException;
use Amp\Http\Client\Request;

require __DIR__ . '/../.helper/functions.php';


// Instantiate the HTTP client
$client = HttpClientBuilder::buildDefault();

while (true) {
    try {
        // Make an asynchronous HTTP request
        $response = $client->request(new Request($argv[1] ?? 'https://httpbin.org/user-agent'));
        $header = $response->getHeader('Location');

        echo memory_get_usage(true) . PHP_EOL;
    } catch (HttpException $error) {
        echo $error;
    }
}

xpader avatar Nov 20 '24 10:11 xpader