rdbms: apply minimum sanity timeout for all cases of LoadBalancer::doWait()
[lhc/web/wiklou.git] / includes / libs / rdbms / loadbalancer / LoadBalancer.php
index 1ba1a97..f410882 100644 (file)
@@ -122,6 +122,8 @@ class LoadBalancer implements ILoadBalancer {
 
        /** @var int Default 'maxLag' when unspecified */
        const MAX_LAG_DEFAULT = 10;
+       /** @var int Default 'waitTimeout' when unspecified */
+       const MAX_WAIT_DEFAULT = 10;
        /** @var int Seconds to cache master server read-only status */
        const TTL_CACHE_READONLY = 5;
 
@@ -151,7 +153,9 @@ class LoadBalancer implements ILoadBalancer {
                        : DatabaseDomain::newUnspecified();
                $this->setLocalDomain( $localDomain );
 
-               $this->mWaitTimeout = isset( $params['waitTimeout'] ) ? $params['waitTimeout'] : 10;
+               $this->mWaitTimeout = isset( $params['waitTimeout'] )
+                       ? $params['waitTimeout']
+                       : self::MAX_WAIT_DEFAULT;
 
                $this->mReadIndex = -1;
                $this->mConns = [
@@ -522,7 +526,7 @@ class LoadBalancer implements ILoadBalancer {
                        for ( $i = 1; $i < $serverCount; $i++ ) {
                                if ( $this->mLoads[$i] > 0 ) {
                                        $start = microtime( true );
-                                       $ok = $this->doWait( $i, true, max( 1, (int)$timeout ) ) && $ok;
+                                       $ok = $this->doWait( $i, true, $timeout ) && $ok;
                                        $timeout -= ( microtime( true ) - $start );
                                        if ( $timeout <= 0 ) {
                                                break; // timeout reached
@@ -575,7 +579,7 @@ class LoadBalancer implements ILoadBalancer {
         * @return bool
         */
        protected function doWait( $index, $open = false, $timeout = null ) {
-               $close = false; // close the connection afterwards
+               $timeout = max( 1, $timeout ?: $this->mWaitTimeout );
 
                // Check if we already know that the DB has reached this point
                $server = $this->getServerName( $index );
@@ -595,6 +599,7 @@ class LoadBalancer implements ILoadBalancer {
                }
 
                // Find a connection to wait on, creating one if needed and allowed
+               $close = false; // close the connection afterwards
                $conn = $this->getAnyOpenConnection( $index );
                if ( !$conn ) {
                        if ( !$open ) {
@@ -626,7 +631,6 @@ class LoadBalancer implements ILoadBalancer {
                        [ 'dbserver' => $server ]
                );
 
-               $timeout = $timeout ?: $this->mWaitTimeout;
                $result = $conn->masterPosWait( $this->mWaitForPos, $timeout );
 
                if ( $result === null ) {