Skip to content

Commit d9b773d

Browse files
committed
Making use of the LimitConcurrentRequestsMiddleware by allowing half of the memory to be used by RequestBodyBufferMiddleware for buffering and the other half to be used for request parsing and handling
1 parent 73a005b commit d9b773d

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/Server.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace React\Http;
44

5+
use React\Http\Middleware\LimitConcurrentRequestsMiddleware;
56
use React\Http\Middleware\RequestBodyBufferMiddleware;
67
use React\Http\Middleware\RequestBodyParserMiddleware;
78
use React\Socket\ServerInterface;
@@ -40,7 +41,10 @@ public function __construct($callback)
4041
throw new \InvalidArgumentException();
4142
}
4243

44+
$concurrentRequestsLimit = ceil(($this->iniLimit('memory_limit') / 2) / $this->iniLimit('post_max_size'));
45+
4346
$this->streamingServer = new StreamingServer(new MiddlewareRunner(array(
47+
new LimitConcurrentRequestsMiddleware($concurrentRequestsLimit),
4448
new RequestBodyBufferMiddleware(),
4549
new RequestBodyParserMiddleware(),
4650
$callback,
@@ -54,4 +58,24 @@ public function listen(ServerInterface $server)
5458
{
5559
$this->streamingServer->listen($server);
5660
}
61+
62+
private function iniLimit($iniSetting)
63+
{
64+
$size = ini_get($iniSetting);
65+
$suffix = strtoupper(substr($size, -1));
66+
if ($suffix === 'K') {
67+
return substr($size, 0, -1) * 1024;
68+
}
69+
if ($suffix === 'M') {
70+
return substr($size, 0, -1) * 1024 * 1024;
71+
}
72+
if ($suffix === 'G') {
73+
return substr($size, 0, -1) * 1024 * 1024 * 1024;
74+
}
75+
if ($suffix === 'T') {
76+
return substr($size, 0, -1) * 1024 * 1024 * 1024 * 1024;
77+
}
78+
79+
return $size;
80+
}
5781
}

0 commit comments

Comments
 (0)