[LockManager] Added timeout to the lock() function.
authorAaron Schulz <aschulz@wikimedia.org>
Wed, 13 Mar 2013 08:07:41 +0000 (01:07 -0700)
committerGerrit Code Review <gerrit@wikimedia.org>
Tue, 2 Apr 2013 22:28:15 +0000 (22:28 +0000)
Change-Id: I32f97b813fc6036b2774d67c14c574420e7a1437

includes/filebackend/lockmanager/LockManager.php

index 0512a01..f534eff 100644 (file)
@@ -88,11 +88,22 @@ abstract class LockManager {
         *
         * @param array $paths List of resource names
         * @param $type integer LockManager::LOCK_* constant
+        * @param integer $timeout Timeout in seconds (0 means non-blocking) (since 1.21)
         * @return Status
         */
-       final public function lock( array $paths, $type = self::LOCK_EX ) {
+       final public function lock( array $paths, $type = self::LOCK_EX, $timeout = 0 ) {
                wfProfileIn( __METHOD__ );
-               $status = $this->doLock( array_unique( $paths ), $this->lockTypeMap[$type] );
+               $msleep = array( 0, 50, 100, 300, 500 ); // retry backoff times
+               $start = microtime( true );
+               do {
+                       $status = $this->doLock( array_unique( $paths ), $this->lockTypeMap[$type] );
+                       $elapsed = microtime( true ) - $start;
+                       if ( $status->isOK() || $elapsed >= $timeout || $elapsed < 0 ) {
+                               break; // success, timeout, or clock set back
+                       }
+                       usleep( 1e3 * ( next( $msleep ) ?: 1000 ) ); // use 1 sec after enough times
+                       $elapsed = microtime( true ) - $start;
+               } while ( $elapsed < $timeout && $elapsed >= 0 );
                wfProfileOut( __METHOD__ );
                return $status;
        }