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