Skip to content

Commit

Permalink
Merge pull request #216 from rmccue/round-up-for-dns
Browse files Browse the repository at this point in the history
Round up to at least 1 second timeouts for cURL
  • Loading branch information
rmccue authored Jun 14, 2016
2 parents ea6b3bd + 9d956c7 commit 2781cdd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
2 changes: 2 additions & 0 deletions library/Requests.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,8 @@ public static function patch($url, $headers, $data = array(), $options = array()
* options:
*
* - `timeout`: How long should we wait for a response?
* Note: for cURL, a minimum of 1 second applies, as DNS resolution
* operates at second-resolution only.
* (float, seconds with a millisecond precision, default: 10, example: 0.01)
* - `connect_timeout`: How long should we wait while trying to connect?
* (float, seconds with a millisecond precision, default: 10, example: 0.01)
Expand Down
14 changes: 11 additions & 3 deletions library/Requests/Transport/cURL.php
Original file line number Diff line number Diff line change
Expand Up @@ -349,11 +349,19 @@ protected function setup_handle($url, $headers, $data, $options) {
break;
}

if (is_int($options['timeout']) || $this->version < self::CURL_7_16_2) {
curl_setopt($this->handle, CURLOPT_TIMEOUT, ceil($options['timeout']));
// cURL requires a minimum timeout of 1 second when using the system
// DNS resolver, as it uses `alarm()`, which is second resolution only.
// There's no way to detect which DNS resolver is being used from our
// end, so we need to round up regardless of the supplied timeout.
//
// https://github.com/curl/curl/blob/4f45240bc84a9aa648c8f7243be7b79e9f9323a5/lib/hostip.c#L606-L609
$timeout = max($options['timeout'], 1);

if (is_int($timeout) || $this->version < self::CURL_7_16_2) {
curl_setopt($this->handle, CURLOPT_TIMEOUT, ceil($timeout));
}
else {
curl_setopt($this->handle, CURLOPT_TIMEOUT_MS, round($options['timeout'] * 1000));
curl_setopt($this->handle, CURLOPT_TIMEOUT_MS, round($timeout * 1000));
}

if (is_int($options['connect_timeout']) || $this->version < self::CURL_7_16_2) {
Expand Down

0 comments on commit 2781cdd

Please sign in to comment.