Skip to content

Http Server apparently not handling async #172

@romeufcrosa

Description

@romeufcrosa

Hi,

I'm trying to understand why I can't make an http server on top of react that can handle multiple concurrent requests.
Since the underlying socket server is async I don't understand why the http server that runs on top of it then can only process 1 request at a time?

Considering the following socket server:

$loop = React\EventLoop\Factory::create();

$i = 0;

$server = stream_socket_server("tcp://127.0.0.1:8080");
stream_set_blocking($server, 0);

$loop->addReadStream($server, function ($server) use ($loop, &$i) {
    $conn = stream_socket_accept($server);
    $data = "HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\nHi ".$i."\n";
    $i++;
    $loop->addWriteStream($conn, function ($conn) use (&$data, $loop) {
        $written = fwrite($conn, $data);
        if ($written === strlen($data)) {
            fclose($conn);
            $loop->removeStream($conn);
        } else {
            $data = substr($data, $written);
        }
    });
});

When I run loadtest on this with concurrent requests, I get as response Hi X, where X is not always in sequence. Which is proof of it being asynchronous.

However take the following http server as example:

$i = 0;
$app = function ($request, $response) use (&$i) {
    $text = "This is request number $i.\n";
    $i++;
    $headers = array("Content-Type" => "text/plain");
    $response->writeHead(200, $headers);
    $response->end($text);
};
$loop = React\EventLoop\Factory::create();

$server = new React\Socket\Server($loop);
$http = new React\Http\Server($server);

$http->on("request", $app);
$socket->listen(8080, "0.0.0.0");
$loop->run();

This always returns This is request number X in sequence.
I'm wondering how I could make the http server handle concurrency, I assumed because socket was async that it would just work but apparently not.

Sorry if this is not really an issue, but it's been bugging me for days now and I had to turn here for help.

Thanks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions