Passed
Pull Request — master (#390)
by Théo
02:53
created

bumpOpenFileDescriptorLimit()   B

Complexity

Conditions 8
Paths 3

Size

Total Lines 50
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 33
dl 0
loc 50
rs 8.1475
c 0
b 0
f 0
cc 8
nc 3
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace KevinGH\Box\Console;
6
7
use KevinGH\Box\Box;
8
use Symfony\Component\Console\Output\OutputInterface;
9
use Symfony\Component\Console\Style\SymfonyStyle;
10
11
/**
12
 * @private
13
 */
14
final class FileDescriptorManipulator
15
{
16
    /**
17
     * Bumps the maximum number of open file descriptor if necessary.
18
     *
19
     * @return callable callable to call to restore the original maximum number of open files descriptors
20
     */
21
    public static function bumpOpenFileDescriptorLimit(int $filesCount, SymfonyStyle $io): callable
22
    {
23
        $filesCount += 128;  // Add a little extra for good measure
24
25
        if (false === function_exists('posix_getrlimit') || false === function_exists('posix_setrlimit')) {
26
            $io->writeln(
27
                '<info>[debug] Could not check the maximum number of open file descriptors: the functions "posix_getrlimit()" and '
28
                .'"posix_setrlimit" could not be found.</info>',
29
                OutputInterface::VERBOSITY_DEBUG
30
            );
31
32
            return static function (): void {};
33
        }
34
35
        $softLimit = posix_getrlimit()['soft openfiles'];
36
        $hardLimit = posix_getrlimit()['hard openfiles'];
37
38
        if ($softLimit >= $filesCount) {
39
            return static function (): void {};
40
        }
41
42
        $io->writeln(
43
            sprintf(
44
                '<info>[debug] Increased the maximum number of open file descriptors from ("%s", "%s") to ("%s", "%s")'
45
                .'</info>',
46
                $softLimit,
47
                $hardLimit,
48
                $filesCount,
49
                'unlimited'
50
            ),
51
            OutputInterface::VERBOSITY_DEBUG
52
        );
53
54
        posix_setrlimit(
55
            POSIX_RLIMIT_NOFILE,
56
            $filesCount,
57
            'unlimited' === $hardLimit ? POSIX_RLIMIT_INFINITY : $hardLimit
58
        );
59
60
        return static function () use ($io, $softLimit, $hardLimit): void {
61
            if (function_exists('posix_setrlimit') && isset($softLimit, $hardLimit)) {
62
                posix_setrlimit(
63
                    POSIX_RLIMIT_NOFILE,
64
                    $softLimit,
65
                    'unlimited' === $hardLimit ? POSIX_RLIMIT_INFINITY : $hardLimit
66
                );
67
68
                $io->writeln(
69
                    '<info>[debug] Restored the maximum number of open file descriptors</info>',
70
                    OutputInterface::VERBOSITY_DEBUG
71
                );
72
            }
73
        };
74
    }
75
76
    private function __construct()
77
    {
78
    }
79
}