• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

src/H03-May-2022-717322

CODE_OF_CONDUCT.mdH A D15-Dec-2021136 31

COPYINGH A D15-Dec-20211 KiB2016

README.mdH A D15-Dec-20211.8 KiB6247

README.md

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