xref: /webtrees/app/Services/TimeoutService.php (revision e0dcab18f07e7cdd57669bc5b941048aac1ce91c)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2019 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
20use Psr\Http\Message\ServerRequestInterface;
21use function app;
22
23/**
24 * Check for PHP timeouts.
25 */
26class TimeoutService
27{
28    /** @var float Long-running scripts run in small chunks */
29    private const TIME_LIMIT = 1.5;
30
31    /** @var float Seconds until we run out of time */
32    private const TIME_UP_THRESHOLD = 3.0;
33
34    /** @var float|null The start time of the request */
35    private $start_time;
36
37    /**
38     * TimeoutService constructor.
39     *
40     * @param float $start_time
41     */
42    public function __construct(float $start_time = null)
43    {
44        $this->start_time = $start_time ?? microtime(true);
45    }
46
47    /**
48     * Some long-running scripts need to know when to stop.
49     *
50     * @param float $threshold
51     *
52     * @return bool
53     */
54    public function isTimeNearlyUp(float $threshold = self::TIME_UP_THRESHOLD): bool
55    {
56        $max_execution_time = (int) ini_get('max_execution_time');
57
58        // If there's no time limit, then we can't run out of time.
59        if ($max_execution_time === 0) {
60            return false;
61        }
62
63        $now = microtime(true);
64
65        return $now + $threshold > $this->start_time + (float) $max_execution_time;
66    }
67
68    /**
69     * Some long running scripts are broken down into small chunks.
70     *
71     * @param float $limit
72     *
73     * @return bool
74     */
75    public function isTimeLimitUp(float $limit = self::TIME_LIMIT): bool
76    {
77        $now = microtime(true);
78
79        return $now > $this->start_time + $limit;
80    }
81
82    /**
83     * @return float
84     */
85    protected function startTime(): float
86    {
87        if ($this->start_time === null) {
88            $request = app(ServerRequestInterface::class);
89
90            $this->start_time = (float) ($request->getServerParams()['REQUEST_TIME_FLOAT'] ?? microtime(true));
91        }
92
93        return $this->start_time;
94    }
95}
96