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 8f04cdb
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
5 changes: 4 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 All @@ -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

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 8f04cdb

Please sign in to comment.