8use InvalidArgumentException;
9use Psr\Log\LoggerInterface;
10use Psr\Log\NullLogger;
12use Wikimedia\RequestTimeout\RequestTimeout;
13use Wikimedia\WaitConditionLoop;
83 $this->domain = $config[
'domain'] ??
'global';
84 if ( isset( $config[
'lockTTL'] ) ) {
85 $this->lockTTL = max( self::MIN_LOCK_TTL, $config[
'lockTTL'] );
86 } elseif ( PHP_SAPI ===
'cli' || PHP_SAPI ===
'phpdbg' ) {
87 $this->lockTTL = 3600;
89 $ttl = 2 * ceil( RequestTimeout::singleton()->getWallTimeLimit() );
91 $this->lockTTL = ( $ttl === INF || $ttl < $minMaxTTL ) ? $minMaxTTL : $ttl;
94 $this->lockTTL = min( $this->lockTTL, self::MAX_LOCK_TTL );
97 for ( $i = 1; $i <= 5; ++$i ) {
98 $random[] = mt_rand( 0, 0xFFFFFFF );
100 $this->session = md5( implode(
'-', $random ) );
102 $this->logger = $config[
'logger'] ??
new NullLogger();
113 final public function lock( array $paths, $type = self::LOCK_EX, $timeout = 0 ) {
114 return $this->
lockByType( [ $type => $paths ], $timeout );
125 final public function lockByType( array $pathsByType, $timeout = 0 ) {
129 $loop =
new WaitConditionLoop(
130 function () use ( &$status, $pathsByType ) {
133 return $status->isOK() ?: WaitConditionLoop::CONDITION_CONTINUE;
150 final public function unlock( array $paths, $type = self::LOCK_EX ) {
177 return \Wikimedia\base_convert( sha1(
"{$this->domain}:{$path}" ), 16, 36, 31 );
190 foreach ( $pathsByType as $type => $paths ) {
191 foreach ( $paths as
$path ) {
192 if ( (
string)
$path ===
'' ) {
193 throw new InvalidArgumentException( __METHOD__ .
": got empty path." );
196 $res[$this->lockTypeMap[$type]] = array_unique( $paths );
210 $status = StatusValue::newGood();
212 foreach ( $pathsByType as $type => $paths ) {
213 $status->merge( $this->
doLock( $paths, $type ) );
214 if ( $status->isOK() ) {
215 $lockedByType[$type] = $paths;
218 foreach ( $lockedByType as $lType => $lPaths ) {
219 $status->merge( $this->
doUnlock( $lPaths, $lType ) );
235 abstract protected function doLock( array $paths, $type );
245 $status = StatusValue::newGood();
246 foreach ( $pathsByType as $type => $paths ) {
247 $status->merge( $this->
doUnlock( $paths, $type ) );
260 abstract protected function doUnlock( array $paths, $type );
263class_alias( LockManager::class,
'LockManager' );
Generic operation result class Has warning/error list, boolean status and arbitrary value.