Limit active DB connections spawned in waitForAll() to one
authorAaron Schulz <aschulz@wikimedia.org>
Fri, 19 Sep 2014 18:48:09 +0000 (11:48 -0700)
committerOri.livneh <ori@wikimedia.org>
Wed, 24 Sep 2014 22:40:30 +0000 (22:40 +0000)
* If new connections are created, they will be closed before
  making the next ones now.

Change-Id: I289d81ec00d3e1e313624e2a4c28a67bfb317feb

includes/db/LoadBalancer.php

index 8aa3bdb..0ccbd93 100644 (file)
@@ -394,7 +394,9 @@ class LoadBalancer {
         * @return bool
         */
        protected function doWait( $index, $open = false, $timeout = null ) {
-               # Find a connection to wait on
+               $close = false; // close the connection afterwards
+
+               # Find a connection to wait on, creating one if needed and allowed
                $conn = $this->getAnyOpenConnection( $index );
                if ( !$conn ) {
                        if ( !$open ) {
@@ -408,6 +410,9 @@ class LoadBalancer {
 
                                        return false;
                                }
+                               // Avoid connection spam in waitForAll() when connections
+                               // are made just for the sake of doing this lag check.
+                               $close = true;
                        }
                }
 
@@ -418,13 +423,17 @@ class LoadBalancer {
                if ( $result == -1 || is_null( $result ) ) {
                        # Timed out waiting for slave, use master instead
                        wfDebug( __METHOD__ . ": Timed out waiting for slave #$index pos {$this->mWaitForPos}\n" );
-
-                       return false;
+                       $ok = false;
                } else {
                        wfDebug( __METHOD__ . ": Done\n" );
+                       $ok = true;
+               }
 
-                       return true;
+               if ( $close ) {
+                       $this->closeConnection( $conn );
                }
+
+               return $ok;
        }
 
        /**