6use Psr\Log\LoggerInterface;
8use Wikimedia\RequestTimeout\RequestTimeout;
9use Wikimedia\WaitConditionLoop;
55 self::LOCK_SH => self::LOCK_SH,
56 self::LOCK_UW => self::LOCK_EX,
57 self::LOCK_EX => self::LOCK_EX
96 $this->domain = $config[
'domain'] ??
'global';
97 if ( isset( $config[
'lockTTL'] ) ) {
98 $this->lockTTL = max( self::MIN_LOCK_TTL, $config[
'lockTTL'] );
99 } elseif ( PHP_SAPI ===
'cli' || PHP_SAPI ===
'phpdbg' ) {
100 $this->lockTTL = self::CLI_LOCK_TTL;
102 $ttl = 2 * ceil( RequestTimeout::singleton()->getWallTimeLimit() );
103 $this->lockTTL = ( $ttl === INF || $ttl < self::MIN_GUESSED_LOCK_TTL )
104 ? self::MIN_GUESSED_LOCK_TTL : $ttl;
110 $this->lockTTL = min( $this->lockTTL, self::MAX_LOCK_TTL );
113 for ( $i = 1; $i <= 5; ++$i ) {
114 $random[] = mt_rand( 0, 0xFFFFFFF );
116 $this->session = md5( implode(
'-', $random ) );
118 $this->logger = $config[
'logger'] ??
new NullLogger();
129 final public function lock( array $paths,
$type = self::LOCK_EX, $timeout = 0 ) {
141 final public function lockByType( array $pathsByType, $timeout = 0 ) {
145 $loop =
new WaitConditionLoop(
146 function () use ( &$status, $pathsByType ) {
149 return $status->isOK() ?: WaitConditionLoop::CONDITION_CONTINUE;
166 final public function unlock( array $paths,
$type = self::LOCK_EX ) {
193 return Wikimedia\base_convert( sha1(
"{$this->domain}:{$path}" ), 16, 36, 31 );
205 return sha1(
"{$this->domain}:{$path}" );
218 foreach ( $pathsByType as
$type => $paths ) {
219 foreach ( $paths as
$path ) {
220 if ( (
string)
$path ===
'' ) {
221 throw new InvalidArgumentException( __METHOD__ .
": got empty path." );
224 $res[$this->lockTypeMap[
$type]] = array_unique( $paths );
238 $status = StatusValue::newGood();
240 foreach ( $pathsByType as
$type => $paths ) {
242 if ( $status->isOK() ) {
243 $lockedByType[
$type] = $paths;
246 foreach ( $lockedByType as $lType => $lPaths ) {
247 $status->merge( $this->
doUnlock( $lPaths, $lType ) );
273 $status = StatusValue::newGood();
274 foreach ( $pathsByType as
$type => $paths ) {
Class for handling resource locking.
const CLI_LOCK_TTL
Default lock TTL in CLI mode.
doUnlockByType(array $pathsByType)
const MIN_LOCK_TTL
Minimum lock TTL.
array $lockTypeMap
Mapping of lock types to the type actually used.
lock(array $paths, $type=self::LOCK_EX, $timeout=0)
Lock the resources at the given abstract paths.
const MIN_GUESSED_LOCK_TTL
The minimum lock TTL if it is guessed from max_execution_time rather than configured.
doUnlock(array $paths, $type)
Unlock resources with the given keys and lock type.
doLock(array $paths, $type)
Lock resources with the given keys and lock type.
string $session
Random 32-char hex number.
lockByType(array $pathsByType, $timeout=0)
Lock the resources at the given abstract paths.
const LOCK_SH
Lock types; stronger locks have higher values.
doLockByType(array $pathsByType)
normalizePathsByType(array $pathsByType)
Normalize the $paths array by converting LOCK_UW locks into the appropriate type and removing any dup...
const MAX_LOCK_TTL
Max expected lock expiry in any context.
sha1Base36Absolute( $path)
Get the base 36 SHA-1 of a string, padded to 31 digits.
unlock(array $paths, $type=self::LOCK_EX)
Unlock the resources at the given abstract paths.
array $locksHeld
Map of (resource path => lock type => count)
sha1Base16Absolute( $path)
Get the base 16 SHA-1 of a string, padded to 31 digits.
unlockByType(array $pathsByType)
Unlock the resources at the given abstract paths.
__construct(array $config)
Construct a new instance from configuration.