6use Psr\Log\LoggerInterface;
8use Wikimedia\WaitConditionLoop;
54 self::LOCK_SH => self::LOCK_SH,
55 self::LOCK_UW => self::LOCK_EX,
56 self::LOCK_EX => self::LOCK_EX
74 protected const MAX_LOCK_TTL = 7200;
86 $this->domain = $config[
'domain'] ??
'global';
87 if ( isset( $config[
'lockTTL'] ) ) {
88 $this->lockTTL = max( 5, $config[
'lockTTL'] );
89 } elseif ( PHP_SAPI ===
'cli' || PHP_SAPI ===
'phpdbg' ) {
90 $this->lockTTL = 3600;
92 $met = ini_get(
'max_execution_time' );
93 $this->lockTTL = max( 5 * 60, 2 * (
int)$met );
99 $this->lockTTL = min( $this->lockTTL, self::MAX_LOCK_TTL );
102 for ( $i = 1; $i <= 5; ++$i ) {
103 $random[] = mt_rand( 0, 0xFFFFFFF );
105 $this->session = md5( implode(
'-', $random ) );
107 $this->logger = $config[
'logger'] ??
new NullLogger();
118 final public function lock( array $paths,
$type = self::LOCK_EX, $timeout = 0 ) {
130 final public function lockByType( array $pathsByType, $timeout = 0 ) {
134 $loop =
new WaitConditionLoop(
135 function () use ( &$status, $pathsByType ) {
138 return $status->isOK() ?: WaitConditionLoop::CONDITION_CONTINUE;
154 final public function unlock( array $paths,
$type = self::LOCK_EX ) {
181 return Wikimedia\base_convert( sha1(
"{$this->domain}:{$path}" ), 16, 36, 31 );
193 return sha1(
"{$this->domain}:{$path}" );
206 foreach ( $pathsByType as
$type => $paths ) {
207 foreach ( $paths as
$path ) {
208 if ( (
string)
$path ===
'' ) {
209 throw new InvalidArgumentException( __METHOD__ .
": got empty path." );
212 $res[$this->lockTypeMap[
$type]] = array_unique( $paths );
226 $status = StatusValue::newGood();
228 foreach ( $pathsByType as
$type => $paths ) {
230 if ( $status->isOK() ) {
231 $lockedByType[
$type] = $paths;
234 foreach ( $lockedByType as $lType => $lPaths ) {
235 $status->merge( $this->
doUnlock( $lPaths, $lType ) );
261 $status = StatusValue::newGood();
262 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 Stable to call.