Skip to content

Commit

Permalink
Close inactive requests
Browse files Browse the repository at this point in the history
This builds on top of reactphp#405 and further builds out reactphp#423 by also
close connections with inactive requests.
  • Loading branch information
WyriHaximus committed Sep 1, 2021
1 parent f33f3ac commit d607e80
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 60 deletions.
42 changes: 41 additions & 1 deletion src/Io/RequestHeaderParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Evenement\EventEmitter;
use Psr\Http\Message\ServerRequestInterface;
use React\EventLoop\LoopInterface;
use React\Http\Message\ServerRequest;
use React\Socket\ConnectionInterface;
use Exception;
Expand All @@ -23,12 +24,48 @@ class RequestHeaderParser extends EventEmitter
{
private $maxSize = 8192;

/**
* @var LoopInterface
*/
private $loop;

/**
* @var float
*/
private $idleConnectionTimeout;

/**
* @param LoopInterface $loop
* @param float $idleConnectionTimeout
*/
public function __construct(LoopInterface $loop, $idleConnectionTimeout)
{
$this->loop = $loop;
$this->idleConnectionTimeout = $idleConnectionTimeout;
}

public function handle(ConnectionInterface $conn)
{
$loop = $this->loop;
$idleConnectionTimeout = $this->idleConnectionTimeout;
$createTimer = function () use ($conn, $loop, $idleConnectionTimeout) {
return $loop->addTimer($idleConnectionTimeout, function () use ($conn) {
$conn->close();
});
};
$timer = $createTimer();
$conn->on('end', function () use ($loop, $timer) {
$loop->cancelTimer($timer);
});
$conn->on('close', function () use ($loop, $timer) {
$loop->cancelTimer($timer);
});
$buffer = '';
$maxSize = $this->maxSize;
$that = $this;
$conn->on('data', $fn = function ($data) use (&$buffer, &$fn, $conn, $maxSize, $that) {
$conn->on('data', $fn = function ($data) use (&$buffer, &$fn, $conn, $maxSize, $that, $loop, &$timer, $createTimer) {
$loop->cancelTimer($timer);
$timer = $createTimer();
// append chunk of data to buffer and look for end of request headers
$buffer .= $data;
$endOfHeader = \strpos($buffer, "\r\n\r\n");
Expand All @@ -42,6 +79,7 @@ public function handle(ConnectionInterface $conn)
new \OverflowException("Maximum header size of {$maxSize} exceeded.", 431),
$conn
));
$loop->cancelTimer($timer);
return;
}

Expand All @@ -66,6 +104,7 @@ public function handle(ConnectionInterface $conn)
$exception,
$conn
));
$loop->cancelTimer($timer);
return;
}

Expand Down Expand Up @@ -104,6 +143,7 @@ public function handle(ConnectionInterface $conn)
if ($contentLength === 0) {
$stream->emit('end');
$stream->close();
$loop->cancelTimer($timer);
}
});
}
Expand Down
16 changes: 1 addition & 15 deletions src/Io/StreamingServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public function __construct(LoopInterface $loop, $requestHandler, $idleConnectTi
$this->idleConnectionTimeout = $idleConnectTimeout;

$this->callback = $requestHandler;
$this->parser = new RequestHeaderParser();
$this->parser = new RequestHeaderParser($this->loop, $this->idleConnectionTimeout);

$that = $this;
$this->parser->on('headers', function (ServerRequestInterface $request, ConnectionInterface $conn) use ($that) {
Expand Down Expand Up @@ -144,20 +144,6 @@ public function listen(ServerInterface $socket)
/** @internal */
public function handle(ConnectionInterface $conn)
{
$timer = $this->loop->addTimer($this->idleConnectionTimeout, function () use ($conn) {
$conn->close();
});
$loop = $this->loop;
$conn->once('data', function () use ($loop, $timer) {
$loop->cancelTimer($timer);
});
$conn->on('end', function () use ($loop, $timer) {
$loop->cancelTimer($timer);
});
$conn->on('close', function () use ($loop, $timer) {
$loop->cancelTimer($timer);
});

$this->parser->handle($conn);
}

Expand Down
Loading

0 comments on commit d607e80

Please sign in to comment.