| 1 |
<?php |
| 2 |
|
| 3 |
namespace Wikimedia\RequestTimeout; |
| 4 |
|
| 5 |
use Wikimedia\RequestTimeout\Detail\BasicRequestTimeout; |
| 6 |
use Wikimedia\RequestTimeout\Detail\ExcimerRequestTimeout; |
| 7 |
|
| 8 |
abstract class RequestTimeout { |
| 9 |
/** @var RequestTimeout|null The singleton instance */ |
| 10 |
private static $instance; |
| 11 |
|
| 12 |
/** |
| 13 |
* Get a singleton instance of RequestTimeout. If the Excimer extension is |
| 14 |
* loaded, this will return a fully functional implementation. If it is |
| 15 |
* not loaded, a fallback implementation will be returned. |
| 16 |
* |
| 17 |
* @return RequestTimeout |
| 18 |
*/ |
| 19 |
public static function singleton() { |
| 20 |
if ( !self::$instance ) { |
| 21 |
self::$instance = self::factory(); |
| 22 |
} |
| 23 |
return self::$instance; |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Set the instance to be returned by singleton(), or null to clear the |
| 28 |
* instance so that it will be recreated. |
| 29 |
* |
| 30 |
* @param RequestTimeout|null $instance |
| 31 |
*/ |
| 32 |
public static function setInstance( ?RequestTimeout $instance ) { |
| 33 |
self::$instance = $instance; |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Create a new instance of RequestTimeout. If the Excimer extension is |
| 38 |
* loaded, this will return a fully functional implementation. If it is |
| 39 |
* not loaded, a fallback implementation will be returned. |
| 40 |
* |
| 41 |
* @return RequestTimeout |
| 42 |
*/ |
| 43 |
public static function factory() { |
| 44 |
if ( extension_loaded( 'excimer' ) ) { |
| 45 |
return new ExcimerRequestTimeout; |
| 46 |
} else { |
| 47 |
return new BasicRequestTimeout; |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Create a CriticalSectionProvider with the specified configuration. |
| 53 |
* |
| 54 |
* @param float $emergencyLimit The emergency timeout in seconds |
| 55 |
* @param callable|null $emergencyCallback A callback to call when the |
| 56 |
* emergency timeout expires. If null, an exception will be thrown. |
| 57 |
* @param callable|null $implicitExitCallback A callback to call if a |
| 58 |
* critical section scope is exited implicitly, rather than by calling |
| 59 |
* exit(). |
| 60 |
* @return CriticalSectionProvider |
| 61 |
*/ |
| 62 |
public function createCriticalSectionProvider( |
| 63 |
$emergencyLimit, $emergencyCallback = null, $implicitExitCallback = null |
| 64 |
) { |
| 65 |
return new CriticalSectionProvider( $this, $emergencyLimit, |
| 66 |
$emergencyCallback, $implicitExitCallback ); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* @internal For use by CriticalSectionProvider |
| 71 |
* |
| 72 |
* @param string $name |
| 73 |
* @param float $emergencyLimit |
| 74 |
* @param callable|null $emergencyCallback |
| 75 |
* @return int |
| 76 |
*/ |
| 77 |
abstract public function enterCriticalSection( $name, $emergencyLimit, $emergencyCallback ); |
| 78 |
|
| 79 |
/** |
| 80 |
* @internal For use by CriticalSectionProvider |
| 81 |
* |
| 82 |
* @throws TimeoutException |
| 83 |
* @param int $id |
| 84 |
*/ |
| 85 |
abstract public function exitCriticalSection( $id ); |
| 86 |
|
| 87 |
/** |
| 88 |
* Set the wall time limit. If excimer is available, an exception will be |
| 89 |
* thrown after the specified number of seconds. |
| 90 |
* |
| 91 |
* If excimer is not available, this falls back to set_time_limit(), which |
| 92 |
* causes a fatal error after the CPU time (not wall clock time) exceeds |
| 93 |
* the given number of seconds, which is rounded to an integer. |
| 94 |
* |
| 95 |
* A time limit of INF or 0 is interpreted as no limit. |
| 96 |
* |
| 97 |
* @param float $limit The limit in seconds |
| 98 |
*/ |
| 99 |
abstract public function setWallTimeLimit( $limit ); |
| 100 |
|
| 101 |
/** |
| 102 |
* Get the amount of time remaining of the limit. If there is no limit, INF |
| 103 |
* will be returned. |
| 104 |
* |
| 105 |
* @return float |
| 106 |
*/ |
| 107 |
abstract public function getWallTimeRemaining(); |
| 108 |
|
| 109 |
/** |
| 110 |
* Get the current wall time limit, or INF if there is no limit |
| 111 |
* |
| 112 |
* @return float |
| 113 |
*/ |
| 114 |
abstract public function getWallTimeLimit(); |
| 115 |
} |