Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix calculating concurrency when post_max_size ini is unlimited #365

Merged
merged 1 commit into from
Jul 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,9 @@ public function __construct($requestHandler)
}

$middleware = array();
$middleware[] = new LimitConcurrentRequestsMiddleware($this->getConcurrentRequestsLimit());
$middleware[] = new LimitConcurrentRequestsMiddleware(
$this->getConcurrentRequestsLimit(\ini_get('memory_limit'), \ini_get('post_max_size'))
);
$middleware[] = new RequestBodyBufferMiddleware();
// Checking for an empty string because that is what a boolean
// false is returned as by ini_get depending on the PHP version.
Expand Down Expand Up @@ -162,17 +164,22 @@ public function listen(ServerInterface $server)
}

/**
* @param string $memory_limit
* @param string $post_max_size
* @return int
* @codeCoverageIgnore
*/
private function getConcurrentRequestsLimit()
private function getConcurrentRequestsLimit($memory_limit, $post_max_size)
{
if (\ini_get('memory_limit') == -1) {
if ($memory_limit == -1) {
return self::MAXIMUM_CONCURRENT_REQUESTS;
}

$availableMemory = IniUtil::iniSizeToBytes(\ini_get('memory_limit')) / 4;
$concurrentRequests = \ceil($availableMemory / IniUtil::iniSizeToBytes(\ini_get('post_max_size')));
if ($post_max_size == 0) {
return 1;
}

$availableMemory = IniUtil::iniSizeToBytes($memory_limit) / 4;
$concurrentRequests = (int) \ceil($availableMemory / IniUtil::iniSizeToBytes($post_max_size));

if ($concurrentRequests >= self::MAXIMUM_CONCURRENT_REQUESTS) {
return self::MAXIMUM_CONCURRENT_REQUESTS;
Expand Down
5 changes: 5 additions & 0 deletions tests/Io/IniUtilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ public function testIniSizeToBytes($input, $output)
$this->assertEquals($output, IniUtil::iniSizeToBytes($input));
}

public function testIniSizeToBytesWithInvalidSuffixReturnsNumberWithoutSuffix()
{
$this->assertEquals('2', IniUtil::iniSizeToBytes('2x'));
}

public function provideInvalidInputIniSizeToBytes()
{
return array(
Expand Down
44 changes: 44 additions & 0 deletions tests/ServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,48 @@ private function createPostFileUploadRequest()

return $data;
}

public function provideIniSettingsForConcurrency()
{
return array(
'default settings' => array(
'128M',
'8M',
4
),
'unlimited memory_limit limited to maximum concurrency' => array(
'-1',
'8M',
100
),
'unlimited post_max_size' => array(
'128M',
'0',
1
),
'small post_max_size limited to maximum concurrency' => array(
'128M',
'1k',
100
)
);
}

/**
* @param string $memory_limit
* @param string $post_max_size
* @param int $expectedConcurrency
* @dataProvider provideIniSettingsForConcurrency
*/
public function testServerConcurrency($memory_limit, $post_max_size, $expectedConcurrency)
{
$server = new Server(function () { });

$ref = new \ReflectionMethod($server, 'getConcurrentRequestsLimit');
$ref->setAccessible(true);

$value = $ref->invoke($server, $memory_limit, $post_max_size);

$this->assertEquals($expectedConcurrency, $value);
}
}