Skip to content

Commit 8f04cdb

Browse files
committed
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
1 parent cca3125 commit 8f04cdb

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -2683,7 +2683,8 @@ access its underlying response object.
26832683
#### InactiveConnectionTimeoutMiddleware
26842684

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

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

@@ -2695,6 +2696,8 @@ $http = new React\Http\HttpServer(
26952696
```
26962697
> Internally, this class is used as a "value object" to override the default timeout of one minute.
26972698
As such it doesn't have any behavior internally, that is all in the internal "StreamingServer".
2699+
This timeout is only in effect if we expect data from the client, not when we are writing data to
2700+
the client.
26982701

26992702
#### StreamingRequestMiddleware
27002703

src/Io/StreamingServer.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,11 @@ public function parseRequest(ConnectionInterface $connection)
418418
};
419419
$connection->once('close', $closeTimerCanceler);
420420
$connection->once('data', $dataTimerCanceler);
421-
$removeTimerHandler = function () use ($parser, $connection, $closeTimerCanceler, $dataTimerCanceler, &$removeTimerHandler) {
421+
$removeTimerHandler = function ($_, $conn) use ($parser, $connection, $closeTimerCanceler, $dataTimerCanceler, &$removeTimerHandler) {
422+
if (\spl_object_hash($conn) !== \spl_object_hash($connection)) {
423+
return;
424+
}
425+
422426
$connection->removeListener('close', $closeTimerCanceler);
423427
$connection->removeListener('data', $dataTimerCanceler);
424428
$parser->removeListener('headers', $removeTimerHandler);

0 commit comments

Comments
 (0)