xref: /webtrees/app/Services/TimeoutService.php (revision 67994fb087e1b24564a780e4ae8aeff801733e35)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2018 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16declare(strict_types=1);
17
18namespace Fisharebest\Webtrees\Services;
19
20/**
21 * Check for PHP timeouts.
22 */
23class TimeoutService
24{
25    /** @var float Long-running scripts run in small chunks */
26    const TIME_LIMIT = 1.5;
27
28    /** @var float Seconds until we run out of time */
29    const TIME_UP_THRESHOLD = 3.0;
30
31    /** @var float The start time of the request */
32    private $start_time;
33
34    /**
35     * TimeoutService constructor.
36     *
37     * @param float $start_time
38     */
39    public function __construct(float $start_time)
40    {
41        $this->start_time = $start_time;
42    }
43
44    /**
45     * Some long-running scripts need to know when to stop.
46     *
47     * @param float $threshold
48     *
49     * @return bool
50     */
51    public function isTimeNearlyUp(float $threshold = self::TIME_UP_THRESHOLD): bool
52    {
53        $max_execution_time = (float) ini_get('max_execution_time');
54
55        // If there's no time limit, then we can't run out of time.
56        if ($max_execution_time === 0.0) {
57            return false;
58        }
59
60        $now = microtime(true);
61
62        return $now + $threshold > $this->start_time + $max_execution_time;
63    }
64
65    /**
66     * Some long running scripts are broken down into small chunks.
67     *
68     * @param float $limit
69     *
70     * @return bool
71     */
72    public function isTimeLimitUp(float $limit = self::TIME_LIMIT): bool
73    {
74        $now = microtime(true);
75
76        return $now > $this->start_time + $limit;
77    }
78}
79