Made $timeout in BagOStuff::lock() actually work
authorAaron Schulz <aschulz@wikimedia.org>
Thu, 20 Nov 2014 08:26:44 +0000 (00:26 -0800)
committerAaron Schulz <aschulz@wikimedia.org>
Thu, 20 Nov 2014 08:26:44 +0000 (00:26 -0800)
* Also added a separate $expiry option for things that take a long time

Change-Id: Ie5f81dea031f3c3f3ca8d61ad4cb322a5b876f61

includes/objectcache/BagOStuff.php
includes/objectcache/MultiWriteBagOStuff.php

index 1978c3e..0a23446 100644 (file)
@@ -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;
        }
index 6a69137..c2a4a27 100644 (file)
@@ -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;
                }