THRIFT-5336 Add possibility to setup connection timeout in TCurlClient
Patch: Vladimir Panivko
Client: php

This closes #2306
diff --git a/lib/php/lib/Transport/TCurlClient.php b/lib/php/lib/Transport/TCurlClient.php
index f781da9..2087433 100644
--- a/lib/php/lib/Transport/TCurlClient.php
+++ b/lib/php/lib/Transport/TCurlClient.php
@@ -84,6 +84,13 @@
     protected $timeout_;
 
     /**
+     * Connection timeout
+     *
+     * @var float
+     */
+    protected $connectionTimeout_;
+
+    /**
      * http headers
      *
      * @var array
@@ -109,6 +116,7 @@
         $this->request_ = '';
         $this->response_ = null;
         $this->timeout_ = null;
+        $this->connectionTimeout_ = null;
         $this->headers_ = array();
     }
 
@@ -123,6 +131,16 @@
     }
 
     /**
+     * Set connection timeout
+     *
+     * @param float $connectionTimeout
+     */
+    public function setConnectionTimeoutSecs($connectionTimeout)
+    {
+        $this->connectionTimeout_ = $connectionTimeout;
+    }
+
+    /**
      * Whether this transport is open.
      *
      * @return boolean true if open
@@ -237,6 +255,14 @@
                 curl_setopt(self::$curlHandle, CURLOPT_TIMEOUT, $this->timeout_);
             }
         }
+        if ($this->connectionTimeout_ > 0) {
+            if ($this->connectionTimeout_ < 1.0) {
+                // Timestamps smaller than 1 second are ignored when CURLOPT_CONNECTTIMEOUT is used
+                curl_setopt(self::$curlHandle, CURLOPT_CONNECTTIMEOUT_MS, 1000 * $this->connectionTimeout_);
+            } else {
+                curl_setopt(self::$curlHandle, CURLOPT_CONNECTTIMEOUT, $this->connectionTimeout_);
+            }
+        }
         curl_setopt(self::$curlHandle, CURLOPT_POSTFIELDS, $this->request_);
         $this->request_ = '';