-
-
Notifications
You must be signed in to change notification settings - Fork 100
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
refactor: Move the bump of the file descriptor limit to a dedicated class #1189
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b654ae5
refactor: Move the bump of the file descriptor limit to a dedicated
theofidry 205f3da
Merge remote-tracking branch 'upstream/main' into refactor/file-limit
theofidry e087fc7
fix
theofidry c8419aa
Merge branch 'main' into refactor/file-limit
theofidry File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the box project. | ||
* | ||
* (c) Kevin Herrera <[email protected]> | ||
* Théo Fidry <[email protected]> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace KevinGH\Box\Console; | ||
|
||
use Closure; | ||
use Fidry\Console\IO; | ||
use KevinGH\Box\NotInstantiable; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use function function_exists; | ||
use function posix_getrlimit; | ||
use function posix_setrlimit; | ||
use function sprintf; | ||
use const POSIX_RLIMIT_INFINITY; | ||
use const POSIX_RLIMIT_NOFILE; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class OpenFileDescriptorLimiter | ||
{ | ||
use NotInstantiable; | ||
|
||
private const LIMIT_MARGIN = 128; | ||
|
||
/** | ||
* Bumps the maximum number of open file descriptor if necessary. | ||
* | ||
* @return Closure Callable to call to restore the original maximum number of open files descriptors | ||
*/ | ||
public static function bumpLimit(int $count, IO $io): Closure | ||
{ | ||
$count += self::LIMIT_MARGIN; // Add a little extra for good measure | ||
|
||
if (false === function_exists('posix_getrlimit') || false === function_exists('posix_setrlimit')) { | ||
$io->writeln( | ||
'<info>[debug] Could not check the maximum number of open file descriptors: the functions "posix_getrlimit()" and ' | ||
.'"posix_setrlimit" could not be found.</info>', | ||
OutputInterface::VERBOSITY_DEBUG, | ||
); | ||
|
||
// TODO: loverage noop | ||
return static function (): void {}; | ||
} | ||
|
||
$softLimit = posix_getrlimit()['soft openfiles']; | ||
$hardLimit = posix_getrlimit()['hard openfiles']; | ||
|
||
if ($softLimit >= $count) { | ||
return static function (): void {}; | ||
} | ||
|
||
$io->writeln( | ||
sprintf( | ||
'<info>[debug] Increased the maximum number of open file descriptors from ("%s", "%s") to ("%s", "%s")' | ||
.'</info>', | ||
$softLimit, | ||
$hardLimit, | ||
$count, | ||
'unlimited', | ||
), | ||
OutputInterface::VERBOSITY_DEBUG, | ||
); | ||
|
||
posix_setrlimit( | ||
POSIX_RLIMIT_NOFILE, | ||
$count, | ||
'unlimited' === $hardLimit ? POSIX_RLIMIT_INFINITY : $hardLimit, | ||
); | ||
|
||
return static function () use ($io, $softLimit, $hardLimit): void { | ||
// TODO: this check looks redundant | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TODO |
||
if (function_exists('posix_setrlimit') && isset($softLimit, $hardLimit)) { | ||
posix_setrlimit( | ||
POSIX_RLIMIT_NOFILE, | ||
$softLimit, | ||
'unlimited' === $hardLimit ? POSIX_RLIMIT_INFINITY : $hardLimit, | ||
); | ||
|
||
$io->writeln( | ||
'<info>[debug] Restored the maximum number of open file descriptors</info>', | ||
OutputInterface::VERBOSITY_DEBUG, | ||
); | ||
} | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TODO (there is more occurrences to check in the codebase)