Go to the documentation of this file.
6 use Psr\Log\LoggerInterface;
7 use Psr\Log\NullLogger;
8 use Wikimedia\WaitConditionLoop;
73 const MAX_LOCK_TTL = 7200;
84 $this->domain = $config[
'domain'] ??
'global';
85 if ( isset( $config[
'lockTTL'] ) ) {
86 $this->lockTTL = max( 5, $config[
'lockTTL'] );
87 } elseif ( PHP_SAPI ===
'cli' || PHP_SAPI ===
'phpdbg' ) {
88 $this->lockTTL = 3600;
90 $met = ini_get(
'max_execution_time' );
91 $this->lockTTL = max( 5 * 60, 2 * (
int)$met );
97 $this->lockTTL = min( $this->lockTTL, self::MAX_LOCK_TTL );
100 for ( $i = 1; $i <= 5; ++$i ) {
101 $random[] = mt_rand( 0, 0xFFFFFFF );
103 $this->session = md5( implode(
'-', $random ) );
105 $this->logger = $config[
'logger'] ??
new NullLogger();
116 final public function lock( array $paths,
$type = self::LOCK_EX, $timeout = 0 ) {
128 final public function lockByType( array $pathsByType, $timeout = 0 ) {
132 $loop =
new WaitConditionLoop(
133 function () use ( &
$status, $pathsByType ) {
136 return $status->isOK() ?: WaitConditionLoop::CONDITION_CONTINUE;
152 final public function unlock( array $paths,
$type = self::LOCK_EX ) {
179 return Wikimedia\base_convert( sha1(
"{$this->domain}:{$path}" ), 16, 36, 31 );
191 return sha1(
"{$this->domain}:{$path}" );
204 foreach ( $pathsByType as
$type => $paths ) {
205 foreach ( $paths as
$path ) {
206 if ( (
string)
$path ===
'' ) {
207 throw new InvalidArgumentException( __METHOD__ .
": got empty path." );
210 $res[$this->lockTypeMap[
$type]] = array_unique( $paths );
225 foreach ( $pathsByType as
$type => $paths ) {
228 $lockedByType[
$type] = $paths;
231 foreach ( $lockedByType as $lType => $lPaths ) {
248 abstract protected function doLock( array $paths,
$type );
258 foreach ( $pathsByType as
$type => $paths ) {
Class for handling resource locking.
array $locksHeld
Map of (resource path => lock type => count)
const LOCK_SH
Lock types; stronger locks have higher values.
sha1Base16Absolute( $path)
Get the base 16 SHA-1 of a string, padded to 31 digits.
array $lockTypeMap
Mapping of lock types to the type actually used.
normalizePathsByType(array $pathsByType)
Normalize the $paths array by converting LOCK_UW locks into the appropriate type and removing any dup...
sha1Base36Absolute( $path)
Get the base 36 SHA-1 of a string, padded to 31 digits.
doUnlockByType(array $pathsByType)
unlock(array $paths, $type=self::LOCK_EX)
Unlock the resources at the given abstract paths.
lock(array $paths, $type=self::LOCK_EX, $timeout=0)
Lock the resources at the given abstract paths.
unlockByType(array $pathsByType)
Unlock the resources at the given abstract paths.
static newGood( $value=null)
Factory function for good results.
string $session
Random 32-char hex number.
doUnlock(array $paths, $type)
Unlock resources with the given keys and lock type.
__construct(array $config)
Construct a new instance from configuration.
doLock(array $paths, $type)
Lock resources with the given keys and lock type.
doLockByType(array $pathsByType)
lockByType(array $pathsByType, $timeout=0)
Lock the resources at the given abstract paths.