Skip to content

Commit d607e80

Browse files
committed
Close inactive requests
This builds on top of reactphp#405 and further builds out reactphp#423 by also close connections with inactive requests.
1 parent f33f3ac commit d607e80

File tree

4 files changed

+120
-60
lines changed

4 files changed

+120
-60
lines changed

src/Io/RequestHeaderParser.php

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Evenement\EventEmitter;
66
use Psr\Http\Message\ServerRequestInterface;
7+
use React\EventLoop\LoopInterface;
78
use React\Http\Message\ServerRequest;
89
use React\Socket\ConnectionInterface;
910
use Exception;
@@ -23,12 +24,48 @@ class RequestHeaderParser extends EventEmitter
2324
{
2425
private $maxSize = 8192;
2526

27+
/**
28+
* @var LoopInterface
29+
*/
30+
private $loop;
31+
32+
/**
33+
* @var float
34+
*/
35+
private $idleConnectionTimeout;
36+
37+
/**
38+
* @param LoopInterface $loop
39+
* @param float $idleConnectionTimeout
40+
*/
41+
public function __construct(LoopInterface $loop, $idleConnectionTimeout)
42+
{
43+
$this->loop = $loop;
44+
$this->idleConnectionTimeout = $idleConnectionTimeout;
45+
}
46+
2647
public function handle(ConnectionInterface $conn)
2748
{
49+
$loop = $this->loop;
50+
$idleConnectionTimeout = $this->idleConnectionTimeout;
51+
$createTimer = function () use ($conn, $loop, $idleConnectionTimeout) {
52+
return $loop->addTimer($idleConnectionTimeout, function () use ($conn) {
53+
$conn->close();
54+
});
55+
};
56+
$timer = $createTimer();
57+
$conn->on('end', function () use ($loop, $timer) {
58+
$loop->cancelTimer($timer);
59+
});
60+
$conn->on('close', function () use ($loop, $timer) {
61+
$loop->cancelTimer($timer);
62+
});
2863
$buffer = '';
2964
$maxSize = $this->maxSize;
3065
$that = $this;
31-
$conn->on('data', $fn = function ($data) use (&$buffer, &$fn, $conn, $maxSize, $that) {
66+
$conn->on('data', $fn = function ($data) use (&$buffer, &$fn, $conn, $maxSize, $that, $loop, &$timer, $createTimer) {
67+
$loop->cancelTimer($timer);
68+
$timer = $createTimer();
3269
// append chunk of data to buffer and look for end of request headers
3370
$buffer .= $data;
3471
$endOfHeader = \strpos($buffer, "\r\n\r\n");
@@ -42,6 +79,7 @@ public function handle(ConnectionInterface $conn)
4279
new \OverflowException("Maximum header size of {$maxSize} exceeded.", 431),
4380
$conn
4481
));
82+
$loop->cancelTimer($timer);
4583
return;
4684
}
4785

@@ -66,6 +104,7 @@ public function handle(ConnectionInterface $conn)
66104
$exception,
67105
$conn
68106
));
107+
$loop->cancelTimer($timer);
69108
return;
70109
}
71110

@@ -104,6 +143,7 @@ public function handle(ConnectionInterface $conn)
104143
if ($contentLength === 0) {
105144
$stream->emit('end');
106145
$stream->close();
146+
$loop->cancelTimer($timer);
107147
}
108148
});
109149
}

src/Io/StreamingServer.php

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public function __construct(LoopInterface $loop, $requestHandler, $idleConnectTi
111111
$this->idleConnectionTimeout = $idleConnectTimeout;
112112

113113
$this->callback = $requestHandler;
114-
$this->parser = new RequestHeaderParser();
114+
$this->parser = new RequestHeaderParser($this->loop, $this->idleConnectionTimeout);
115115

116116
$that = $this;
117117
$this->parser->on('headers', function (ServerRequestInterface $request, ConnectionInterface $conn) use ($that) {
@@ -144,20 +144,6 @@ public function listen(ServerInterface $socket)
144144
/** @internal */
145145
public function handle(ConnectionInterface $conn)
146146
{
147-
$timer = $this->loop->addTimer($this->idleConnectionTimeout, function () use ($conn) {
148-
$conn->close();
149-
});
150-
$loop = $this->loop;
151-
$conn->once('data', function () use ($loop, $timer) {
152-
$loop->cancelTimer($timer);
153-
});
154-
$conn->on('end', function () use ($loop, $timer) {
155-
$loop->cancelTimer($timer);
156-
});
157-
$conn->on('close', function () use ($loop, $timer) {
158-
$loop->cancelTimer($timer);
159-
});
160-
161147
$this->parser->handle($conn);
162148
}
163149

0 commit comments

Comments
 (0)