7use Psr\Log\LoggerInterface;
9use Wikimedia\RequestTimeout\RequestTimeout;
10use Wikimedia\WaitConditionLoop;
39 self::LOCK_SH => self::LOCK_SH,
40 self::LOCK_UW => self::LOCK_EX,
41 self::LOCK_EX => self::LOCK_EX
80 $this->domain = $config[
'domain'] ??
'global';
81 if ( isset( $config[
'lockTTL'] ) ) {
82 $this->lockTTL = max( self::MIN_LOCK_TTL, $config[
'lockTTL'] );
83 } elseif ( PHP_SAPI ===
'cli' || PHP_SAPI ===
'phpdbg' ) {
84 $this->lockTTL = 3600;
86 $ttl = 2 * ceil( RequestTimeout::singleton()->getWallTimeLimit() );
88 $this->lockTTL = ( $ttl === INF || $ttl < $minMaxTTL ) ? $minMaxTTL : $ttl;
91 $this->lockTTL = min( $this->lockTTL, self::MAX_LOCK_TTL );
94 for ( $i = 1; $i <= 5; ++$i ) {
95 $random[] = mt_rand( 0, 0xFFFFFFF );
97 $this->session = md5( implode(
'-', $random ) );
99 $this->logger = $config[
'logger'] ??
new NullLogger();
110 final public function lock( array $paths, $type = self::LOCK_EX, $timeout = 0 ) {
111 return $this->
lockByType( [ $type => $paths ], $timeout );
122 final public function lockByType( array $pathsByType, $timeout = 0 ) {
126 $loop =
new WaitConditionLoop(
127 function () use ( &$status, $pathsByType ) {
130 return $status->isOK() ?: WaitConditionLoop::CONDITION_CONTINUE;
147 final public function unlock( array $paths, $type = self::LOCK_EX ) {
174 return Wikimedia\base_convert( sha1(
"{$this->domain}:{$path}" ), 16, 36, 31 );
187 foreach ( $pathsByType as $type => $paths ) {
188 foreach ( $paths as
$path ) {
189 if ( (
string)
$path ===
'' ) {
190 throw new InvalidArgumentException( __METHOD__ .
": got empty path." );
193 $res[$this->lockTypeMap[$type]] = array_unique( $paths );
207 $status = StatusValue::newGood();
209 foreach ( $pathsByType as $type => $paths ) {
210 $status->merge( $this->
doLock( $paths, $type ) );
211 if ( $status->isOK() ) {
212 $lockedByType[$type] = $paths;
215 foreach ( $lockedByType as $lType => $lPaths ) {
216 $status->merge( $this->
doUnlock( $lPaths, $lType ) );
232 abstract protected function doLock( array $paths, $type );
242 $status = StatusValue::newGood();
243 foreach ( $pathsByType as $type => $paths ) {
244 $status->merge( $this->
doUnlock( $paths, $type ) );
257 abstract protected function doUnlock( array $paths, $type );
Resource locking handling.
doUnlockByType(array $pathsByType)
string $domain
domain (usually wiki ID)
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.
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)
unlockByType(array $pathsByType)
Unlock the resources at the given abstract paths.
int $lockTTL
maximum time locks can be held
__construct(array $config)
Construct a new instance from configuration.