Allow setting of connection timeouts for HTTP requests using cURL
authorChad Horohoe <chadh@wikimedia.org>
Mon, 8 Apr 2013 23:45:05 +0000 (19:45 -0400)
committerGerrit Code Review <gerrit@wikimedia.org>
Tue, 4 Jun 2013 22:08:09 +0000 (22:08 +0000)
Bug: 47027
Change-Id: I6e64a8bfc58e899149463d305eac672c1e8ad2ba

includes/DefaultSettings.php
includes/HttpFunctions.php

index c93142a..58eb3d7 100644 (file)
@@ -6167,6 +6167,12 @@ $wgAsyncHTTPTimeout = 25;
  */
 $wgHTTPProxy = false;
 
+/**
+ * Timeout for connections done internally (in seconds)
+ * Only works for curl
+ */
+$wgHTTPConnectTimeout = 5e0;
+
 /** @} */ # End HTTP client }
 
 /************************************************************************//**
index 2da368c..1c9ad38 100644 (file)
@@ -40,6 +40,7 @@ class Http {
         * @param array $options options to pass to MWHttpRequest object.
         *      Possible keys for the array:
         *    - timeout             Timeout length in seconds
+        *    - connectTimeout      Timeout for connection, in seconds (curl only)
         *    - postData            An array of key-value pairs or a url-encoded form data
         *    - proxy               The proxy to use.
         *                          Otherwise it will use $wgHTTPProxy (if set)
@@ -65,6 +66,9 @@ class Http {
                if ( !isset( $options['timeout'] ) ) {
                        $options['timeout'] = 'default';
                }
+               if( !isset( $options['connectTimeout'] ) ) {
+                       $options['connectTimeout'] = 'default';
+               }
 
                $req = MWHttpRequest::factory( $url, $options );
                $status = $req->execute();
@@ -232,6 +236,11 @@ class MWHttpRequest {
                } else {
                        $this->timeout = $wgHTTPTimeout;
                }
+               if ( isset( $options['connectTimeout'] ) && $options['connectTimeout'] != 'default' ) {
+                       $this->connectTimeout = $options['connectTimeout'];
+               } else {
+                       $this->connectTimeout = $wgHTTPConnectTimeout;
+               }
                if ( isset( $options['userAgent'] ) ) {
                        $this->setUserAgent( $options['userAgent'] );
                }
@@ -721,6 +730,7 @@ class CurlHttpRequest extends MWHttpRequest {
 
                $this->curlOptions[CURLOPT_PROXY] = $this->proxy;
                $this->curlOptions[CURLOPT_TIMEOUT] = $this->timeout;
+               $this->curlOptions[CURLOPT_CONNECTTIMEOUT_MS] = $this->connectTimeout * 1000;
                $this->curlOptions[CURLOPT_HTTP_VERSION] = CURL_HTTP_VERSION_1_0;
                $this->curlOptions[CURLOPT_WRITEFUNCTION] = $this->callback;
                $this->curlOptions[CURLOPT_HEADERFUNCTION] = array( $this, "readHeader" );