Debian Codegraph @

vendor/wikimedia/request-timeout/README.md

raw
1 [Excimer](https://www.mediawiki.org/wiki/Excimer) is an extension for PHP which
2 provides flexible userspace timers.
3
4 This library provides a global request timeout concept, ideally using Excimer,
5 but with a fallback to set_time_limit() if Excimer is not available.
6
7 Typical usage:
8
9 ```
10 use Wikimedia\RequestTimeout\RequestTimeout;
11
12 RequestTimeout::singleton()->setWallTimeLimit( 20 );
13 ```
14
15 This sets a timer for 20 seconds of wall clock time. When it expires, a
16 TimeoutException will be thrown.
17
18 It is possible to query the amount of time remaining:
19
20 ```
21 if ( RequestTimeout::singleton()->getWallTimeRemaining() > 5 ) {
22 do_slow_thing();
23 } else {
24 do_fast_thing();
25 }
26 ```
27
28 This works in the fallback mode.
29
30 The library provides a critical section concept. If a critical section is
31 active, timeouts will be queued, rather than immediately thrown. The timeout
32 exception will be thrown once no critical section is open.
33
34 ```
35 $csp = RequestTimeout::singleton()->createCriticalSectionProvider( 5 );
36 $csp->enter( __METHOD__ );
37 try {
38 do_something();
39 } finally {
40 $csp->exit( __METHOD__ );
41 }
42 ```
43
44 It is important to always exit a critical section. If the code in the critical
45 section can throw an error, try/finally can be used to ensure that the critical
46 section is exited. Alternatively we provide a scope variable model:
47
48 ```
49 function foo() {
50 $scope = $csp->scopedEnter( __METHOD__ );
51 do_something();
52 }
53 ```
54
55 The critical section exits when the scope object is destroyed. However, with
56 this method, it is important to not terminate the request during the critical
57 section, for example by calling `exit()`, or by keeping the scope object in
58 global variable after the function returns. The library may throw an exception
59 from a destructor during request shutdown, which causes a PHP fatal error.
60
61 Critical sections are not functional in the fallback mode.