Skip to content

Commit

Permalink
Close inactive requests
Browse files Browse the repository at this point in the history
This builds on top of #405 and further builds out #423 by also
close connections with inactive requests.
  • Loading branch information
WyriHaximus committed Nov 9, 2021
1 parent 997d5b6 commit 7b3f5b6
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 60 deletions.
39 changes: 38 additions & 1 deletion src/Io/RequestHeaderParser.php
Expand Up @@ -4,6 +4,7 @@

use Evenement\EventEmitter;
use Psr\Http\Message\ServerRequestInterface;
use React\EventLoop\LoopInterface;
use React\Http\Message\Response;
use React\Http\Message\ServerRequest;
use React\Socket\ConnectionInterface;
Expand All @@ -24,12 +25,45 @@ 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('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 @@ -43,6 +77,7 @@ public function handle(ConnectionInterface $conn)
new \OverflowException("Maximum header size of {$maxSize} exceeded.", Response::STATUS_REQUEST_HEADER_FIELDS_TOO_LARGE),
$conn
));
$loop->cancelTimer($timer);
return;
}

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

Expand Down Expand Up @@ -105,6 +141,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
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

0 comments on commit 7b3f5b6

Please sign in to comment.