From 8f04cdb27d8ab6df276f210d81de31d0b53483a3 Mon Sep 17 00:00:00 2001 From: Cees-Jan Kiewiet Date: Tue, 26 Mar 2024 11:13:30 +0100 Subject: [PATCH] Close inactive connections and requests 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 --- README.md | 5 ++++- src/Io/StreamingServer.php | 6 +++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 02202594..9d1f133e 100644 --- a/README.md +++ b/README.md @@ -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: @@ -2695,6 +2696,8 @@ $http = new React\Http\HttpServer( ``` > Internally, this class is used as a "value object" to override the default timeout of one minute. As such it doesn't have any behavior internally, that is all in the internal "StreamingServer". + This timeout is only in effect if we expect data from the client, not when we are writing data to + the client. #### StreamingRequestMiddleware diff --git a/src/Io/StreamingServer.php b/src/Io/StreamingServer.php index 4e1907e8..fcf1fa19 100644 --- a/src/Io/StreamingServer.php +++ b/src/Io/StreamingServer.php @@ -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);