Made Block::insert handle expired rows
authorAaron Schulz <aschulz@wikimedia.org>
Mon, 6 Apr 2015 22:13:07 +0000 (15:13 -0700)
committerAaron Schulz <aschulz@wikimedia.org>
Mon, 6 Apr 2015 22:13:07 +0000 (15:13 -0700)
Bug: T90780
Change-Id: Ife9bcc0ec329156dac0bd033de35b25430234764

includes/Block.php

index 4698f45..873a26d 100644 (file)
@@ -442,19 +442,33 @@ class Block {
                        $dbw = wfGetDB( DB_MASTER );
                }
 
-               # Don't collide with expired blocks
-               Block::purgeExpired();
+               # Periodic purge via commit hooks
+               if ( mt_rand( 0, 9 ) == 0 ) {
+                       Block::purgeExpired();
+               }
 
                $row = $this->getDatabaseArray();
                $row['ipb_id'] = $dbw->nextSequenceValue( "ipblocks_ipb_id_seq" );
 
-               $dbw->insert(
-                       'ipblocks',
-                       $row,
-                       __METHOD__,
-                       array( 'IGNORE' )
-               );
+               $dbw->insert( 'ipblocks', $row, __METHOD__, array( 'IGNORE' ) );
                $affected = $dbw->affectedRows();
+
+               # Don't collide with expired blocks.
+               # Do this after trying to insert to avoid pointless gap locks.
+               if ( !$affected ) {
+                       $dbw->delete( 'ipblocks',
+                               array(
+                                       'ipb_address' => $row['ipb_address'],
+                                       'ipb_user' => $row['ipb_user'],
+                                       'ipb_expiry < ' . $dbw->addQuotes( $dbw->timestamp() )
+                               ),
+                               __METHOD__
+                       );
+
+                       $dbw->insert( 'ipblocks', $row, __METHOD__, array( 'IGNORE' ) );
+                       $affected = $dbw->affectedRows();
+               }
+
                $this->mId = $dbw->insertId();
 
                if ( $affected ) {