6use Psr\Log\LoggerInterface;
8use Wikimedia\WaitConditionLoop;
53 self::LOCK_SH => self::LOCK_SH,
54 self::LOCK_UW => self::LOCK_EX,
55 self::LOCK_EX => self::LOCK_EX
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 );
223 $status = StatusValue::newGood();
225 foreach ( $pathsByType as
$type => $paths ) {
227 if ( $status->isOK() ) {
228 $lockedByType[
$type] = $paths;
231 foreach ( $lockedByType as $lType => $lPaths ) {
232 $status->merge( $this->
doUnlock( $lPaths, $lType ) );
257 $status = StatusValue::newGood();
258 foreach ( $pathsByType as
$type => $paths ) {
Class for handling resource locking.
doUnlockByType(array $pathsByType)
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...
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.