From 19f3068ce5d654957f0bc727af8b0d0c7a6b7ba6 Mon Sep 17 00:00:00 2001 From: Aaron Schulz Date: Thu, 20 Nov 2014 00:26:44 -0800 Subject: [PATCH] Made $timeout in BagOStuff::lock() actually work * Also added a separate $expiry option for things that take a long time Change-Id: Ie5f81dea031f3c3f3ca8d61ad4cb322a5b876f61 --- includes/objectcache/BagOStuff.php | 11 ++++++----- includes/objectcache/MultiWriteBagOStuff.php | 5 +++-- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/includes/objectcache/BagOStuff.php b/includes/objectcache/BagOStuff.php index 1978c3ead3..0a2344688b 100644 --- a/includes/objectcache/BagOStuff.php +++ b/includes/objectcache/BagOStuff.php @@ -173,13 +173,14 @@ abstract class BagOStuff { /** * @param string $key - * @param int $timeout [optional] + * @param int $timeout Lock wait timeout [optional] + * @param int $expiry Lock expiry [optional] * @return bool Success */ - public function lock( $key, $timeout = 6 ) { + public function lock( $key, $timeout = 6, $expiry = 6 ) { $this->clearLastError(); $timestamp = microtime( true ); // starting UNIX timestamp - if ( $this->add( "{$key}:lock", 1, $timeout ) ) { + if ( $this->add( "{$key}:lock", 1, $expiry ) ) { return true; } elseif ( $this->getLastError() ) { return false; @@ -198,11 +199,11 @@ abstract class BagOStuff { } usleep( $sleep ); // back off $this->clearLastError(); - $locked = $this->add( "{$key}:lock", 1, $timeout ); + $locked = $this->add( "{$key}:lock", 1, $expiry ); if ( $this->getLastError() ) { return false; } - } while ( !$locked ); + } while ( !$locked && ( microtime( true ) - $timestamp ) < $timeout ); return $locked; } diff --git a/includes/objectcache/MultiWriteBagOStuff.php b/includes/objectcache/MultiWriteBagOStuff.php index 6a691379a0..c2a4a2783a 100644 --- a/includes/objectcache/MultiWriteBagOStuff.php +++ b/includes/objectcache/MultiWriteBagOStuff.php @@ -136,12 +136,13 @@ class MultiWriteBagOStuff extends BagOStuff { /** * @param string $key * @param int $timeout + * @param int $expiry * @return bool */ - public function lock( $key, $timeout = 0 ) { + public function lock( $key, $timeout = 6, $expiry = 6 ) { // Lock only the first cache, to avoid deadlocks if ( isset( $this->caches[0] ) ) { - return $this->caches[0]->lock( $key, $timeout ); + return $this->caches[0]->lock( $key, $timeout, $expiry ); } else { return true; } -- 2.20.1