reactphp / http

Event-driven, streaming HTTP client and server implementation for ReactPHP.
https://reactphp.org/http/
MIT License
747 stars 143 forks source link

HTTP client on specific network shows poor performance #494

Closed Exanlv closed 1 year ago

Exanlv commented 1 year ago

Depending on network configuration, making HTTP requests can be extremely slow.

Using the code below, I compared React HTTP with Guzzle HTTP. React is far slower

image

This only occurs on my work's network, when doing this test on the exact same machine at home, this is not an issue. (It's about 6km of physical distance so that's negligible)

I can't get access to the router config to look around at the settings, if there's any specific configuration that you'd like to know about this network I could ask.

<?php

use GuzzleHttp\Client;
use React\Http\Browser;
use RingCentral\Psr7\Request;

use function React\Async\await;

require './vendor/autoload.php';

if (count($argv) < 2) {
    die('Specify which HTTP lib to use');
}

$requests = [
    new Request(
        'GET',
        'https://google.com/'
    ),
    new Request(
        'GET',
        'https://postman-echo.com/get'
    ),
];

$time = microtime(true);

if ($argv[1] === 'react') {
    $browser = new Browser();

    foreach ($requests as $request) {
        await($browser->request($request->getMethod(), $request->getUri()));
    }
} elseif ($argv[1] === 'guzzle') {
    $client = new Client();

    foreach ($requests as $request) {
        $client->request($request->getMethod(), $request->getUri());
    }
} else {
    die('Invalid test subject');
}

echo sprintf('Completed in %f seconds', microtime(true) - $time), PHP_EOL;
clue commented 1 year ago

@Exanlv Thanks for reporting, but I can not currently reproduce this locally: Both examples take around ~0.8s here and on a number of different hosts I've tried.

There are a large number of factors that could have an influence on this because this is currently a very broad report:

Can you provide more details about your environment and what steps you've taken to reproduce this? Can you try individual layers or do you need help with adding some debugging steps? Can you try this with other hosts, without DNS resolution, with different DNS settings, with and without HTTPS? Have you tried creating a network dump (Wireshark) to see where the time is spent?

Until we have more information, I don't think this looks like a bug at the moment, so I'll go ahead and label this as question instead. Please report back in either case 👍

WyriHaximus commented 1 year ago

@Exanlv What is your work's network set up like? Any DNS or proxying enforcements in place? This somewhat reads like an exotic bit of configuration we don't pick up for whatever reason that is biting you.

Exanlv commented 1 year ago

Overwriting the DNS with Google/Cloudflare fixes the poor performance.

$dnsConfig = DnsConfig::loadSystemConfigBlocking();
$dnsConfig->nameservers = [
    '1.1.1.1',
    '1.0.0.1',
    '8.8.8.8',
    '8.8.4.4',
];

$dnsFactory = (new DnsFactory())->createCached($dnsConfig);
$browser = new Browser(
    new Connector([
        'dns' => $dnsFactory,
    ])
);

Not sure what the problem is with the default DNS - how would I go about figuring out what's causing the slowdowns?

Fyi, I work remotely most of the time, so won't be able to try stuff immediately. This is also why I didn't get back to this sooner.

WyriHaximus commented 1 year ago

Overwriting the DNS with Google/Cloudflare fixes the poor performance.

$dnsConfig = DnsConfig::loadSystemConfigBlocking();
$dnsConfig->nameservers = [
    '1.1.1.1',
    '1.0.0.1',
    '8.8.8.8',
    '8.8.4.4',
];

$dnsFactory = (new DnsFactory())->createCached($dnsConfig);
$browser = new Browser(
    new Connector([
        'dns' => $dnsFactory,
    ])
);

Not sure what the problem is with the default DNS - how would I go about figuring out what's causing the slowdowns?

Interesting, can you share your resolve.conf? The network might expect specific options to be strictly follow, or there is something in general off on that network.

Fyi, I work remotely most of the time, so won't be able to try stuff immediately. This is also why I didn't get back to this sooner.

No worries, we all have lives :).

clue commented 1 year ago

I'm closing this for now as it hasn't received any input in a while and we can not currently reproduce this. If this problem persists, please come back with more details and we can always reopen this :+1: