Issues (224)

src/Console/OpenFileDescriptorLimiter.php (1 issue)

Labels
Severity
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the box project.
7
 *
8
 * (c) Kevin Herrera <[email protected]>
9
 *     Théo Fidry <[email protected]>
10
 *
11
 * This source file is subject to the MIT license that is bundled
12
 * with this source code in the file LICENSE.
13
 */
14
15
namespace KevinGH\Box\Console;
16
17
use Closure;
18
use Fidry\Console\IO;
0 ignored issues
show
The type Fidry\Console\IO was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
use KevinGH\Box\Noop;
20
use KevinGH\Box\NotInstantiable;
21
use Symfony\Component\Console\Output\OutputInterface;
22
use function function_exists;
23
use function posix_getrlimit;
24
use function posix_setrlimit;
25
use function sprintf;
26
use const POSIX_RLIMIT_INFINITY;
27
use const POSIX_RLIMIT_NOFILE;
28
29
/**
30
 * @internal
31
 */
32
final class OpenFileDescriptorLimiter
33
{
34
    use NotInstantiable;
35
36
    private const LIMIT_MARGIN = 128;
37
38
    /**
39
     * Bumps the maximum number of open file descriptor if necessary.
40
     *
41
     * @return Closure Callable to call to restore the original maximum number of open files descriptors
42
     */
43
    public static function bumpLimit(int $count, IO $io): Closure
44
    {
45
        $count += self::LIMIT_MARGIN;  // Add a little extra for good measure
46
47
        if (false === function_exists('posix_getrlimit') || false === function_exists('posix_setrlimit')) {
48
            $io->writeln(
49
                '<info>[debug] Could not check the maximum number of open file descriptors: the functions "posix_getrlimit()" and '
50
                .'"posix_setrlimit" could not be found.</info>',
51
                OutputInterface::VERBOSITY_DEBUG,
52
            );
53
54
            return Noop::create();
55
        }
56
57
        $softLimit = posix_getrlimit()['soft openfiles'];
58
        $hardLimit = posix_getrlimit()['hard openfiles'];
59
60
        if ($softLimit >= $count) {
61
            return Noop::create();
62
        }
63
64
        $io->writeln(
65
            sprintf(
66
                '<info>[debug] Increased the maximum number of open file descriptors from ("%s", "%s") to ("%s", "%s")'
67
                .'</info>',
68
                $softLimit,
69
                $hardLimit,
70
                $count,
71
                'unlimited',
72
            ),
73
            OutputInterface::VERBOSITY_DEBUG,
74
        );
75
76
        posix_setrlimit(
77
            POSIX_RLIMIT_NOFILE,
78
            $count,
79
            'unlimited' === $hardLimit ? POSIX_RLIMIT_INFINITY : $hardLimit,
80
        );
81
82
        return static function () use ($io, $softLimit, $hardLimit): void {
83
            posix_setrlimit(
84
                POSIX_RLIMIT_NOFILE,
85
                $softLimit,
86
                'unlimited' === $hardLimit ? POSIX_RLIMIT_INFINITY : $hardLimit,
87
            );
88
89
            $io->writeln(
90
                '<info>[debug] Restored the maximum number of open file descriptors</info>',
91
                OutputInterface::VERBOSITY_DEBUG,
92
            );
93
        };
94
    }
95
}
96