Skip to content

Commit

Permalink
Close inactive connections and requests
Browse files Browse the repository at this point in the history
This new middleware introduces a timeout of closing inactive
connections between requests after a configured amount of seconds.

This builds on top of #405 and partially on #422
  • Loading branch information
WyriHaximus committed Mar 26, 2024
1 parent cca3125 commit 1844947
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2683,7 +2683,8 @@ access its underlying response object.
#### InactiveConnectionTimeoutMiddleware

The `React\Http\Middleware\InactiveConnectionTimeoutMiddleware` is purely a configuration middleware to configure the
`HttpServer` to close any inactive connections between requests to close the connection and not leave them needlessly open.
`HttpServer` to close any inactive connections between requests to close the connection and not leave them needlessly
open. The default is `60` seconds of inactivity and should only be changed if you know what you are doing.

The following example configures the `HttpServer` to close any inactive connections after one and a half second:

Expand Down
6 changes: 5 additions & 1 deletion src/Io/StreamingServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,11 @@ public function parseRequest(ConnectionInterface $connection)
};
$connection->once('close', $closeTimerCanceler);
$connection->once('data', $dataTimerCanceler);
$removeTimerHandler = function () use ($parser, $connection, $closeTimerCanceler, $dataTimerCanceler, &$removeTimerHandler) {
$removeTimerHandler = function ($_, $conn) use ($parser, $connection, $closeTimerCanceler, $dataTimerCanceler, &$removeTimerHandler) {
if (\spl_object_hash($conn) !== \spl_object_hash($connection)) {
return;
}

$connection->removeListener('close', $closeTimerCanceler);
$connection->removeListener('data', $dataTimerCanceler);
$parser->removeListener('headers', $removeTimerHandler);
Expand Down

0 comments on commit 1844947

Please sign in to comment.