Use `lock(,,0)` instead of `lockIsFree && lock(,,1)` for non-blocking locks
authorTimo Tijhof <krinklemail@gmail.com>
Thu, 15 Feb 2018 22:30:26 +0000 (14:30 -0800)
committerTimo Tijhof <krinklemail@gmail.com>
Thu, 15 Feb 2018 22:30:26 +0000 (14:30 -0800)
This pattern is already used elsewhere and seems like a more efficient
way to acquire locks in a non-blocking way.

Change-Id: Idb369e7cb03b793d5f8295e956fecd8d1f849e17

includes/deferred/SiteStatsUpdate.php
includes/jobqueue/jobs/RecentChangesUpdateJob.php

index ad1f172..2f882b8 100644 (file)
@@ -101,9 +101,7 @@ class SiteStatsUpdate implements DeferrableUpdate, MergeableUpdate {
                $pd = [];
                if ( $config->get( 'SiteStatsAsyncFactor' ) ) {
                        // Lock the table so we don't have double DB/memcached updates
-                       if ( !$dbw->lockIsFree( $lockKey, __METHOD__ )
-                               || !$dbw->lock( $lockKey, __METHOD__, 1 ) // 1 sec timeout
-                       ) {
+                       if ( !$dbw->lock( $lockKey, __METHOD__, 0 ) ) {
                                $this->doUpdatePendingDeltas();
 
                                return;
index a92ae96..d97e4f9 100644 (file)
@@ -76,10 +76,9 @@ class RecentChangesUpdateJob extends Job {
                $lockKey = wfWikiID() . ':recentchanges-prune';
 
                $dbw = wfGetDB( DB_MASTER );
-               if ( !$dbw->lockIsFree( $lockKey, __METHOD__ )
-                       || !$dbw->lock( $lockKey, __METHOD__, 1 )
-               ) {
-                       return; // already in progress
+               if ( !$dbw->lock( $lockKey, __METHOD__, 0 ) ) {
+                       // already in progress
+                       return;
                }
 
                $factory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
@@ -138,7 +137,7 @@ class RecentChangesUpdateJob extends Job {
                                $dbw->setSessionOptions( [ 'connTimeout' => 900 ] );
 
                                $lockKey = wfWikiID() . '-activeusers';
-                               if ( !$dbw->lockIsFree( $lockKey, __METHOD__ ) || !$dbw->lock( $lockKey, __METHOD__, 1 ) ) {
+                               if ( !$dbw->lock( $lockKey, __METHOD__, 0 ) ) {
                                        // Exclusive update (avoids duplicate entries)… it's usually fine to just drop out here,
                                        // if the Job is already running.
                                        return;