Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transfer & connect timeouts, in seconds & milliseconds #97

Merged
merged 3 commits into from
Jan 24, 2014
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions examples/timeout.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

// First, include Requests
include('../library/Requests.php');

// Next, make sure Requests can load internal classes
Requests::register_autoloader();

// Define a timeout of 2.5 seconds
$options = array(
'timeout' => 2.5,
);

// Now let's make a request to a page that will delay its response by 3 seconds
$request = Requests::get('http://httpbin.org/delay/3', array(), $options);

// An exception will be thrown, stating a timeout of the request !
5 changes: 4 additions & 1 deletion library/Requests.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,9 @@ public static function patch($url, $headers, $data = array(), $options = array()
* options:
*
* - `timeout`: How long should we wait for a response?
* (integer, seconds, default: 10)
* (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)
* - `useragent`: Useragent to send to the server
* (string, default: php-requests/$version)
* - `follow_redirects`: Should we follow 3xx redirects?
Expand Down Expand Up @@ -443,6 +445,7 @@ public static function request_multiple($requests, $options = array()) {
protected static function get_default_options($multirequest = false) {
$defaults = array(
'timeout' => 10,
'connect_timeout' => 10,
'useragent' => 'php-requests/' . self::VERSION,
'redirected' => 0,
'redirects' => 10,
Expand Down
12 changes: 10 additions & 2 deletions library/Requests/Transport/cURL.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,17 @@ protected function setup_handle($url, $headers, $data, $options) {
break;
}

if( is_int($options['timeout']) or version_compare($this->version, '7.16.2', '<') ) {
curl_setopt($this->fp, CURLOPT_TIMEOUT, ceil($options['timeout']));
} else {
curl_setopt($this->fp, CURLOPT_TIMEOUT_MS, round($options['timeout'] * 1000) );
}
if( is_int($options['connect_timeout']) or version_compare($this->version, '7.16.2', '<') ) {
curl_setopt($this->fp, CURLOPT_CONNECTTIMEOUT, ceil($options['connect_timeout']));
} else {
curl_setopt($this->fp, CURLOPT_CONNECTTIMEOUT_MS, round($options['connect_timeout'] * 1000));
}
curl_setopt($this->fp, CURLOPT_URL, $url);
curl_setopt($this->fp, CURLOPT_TIMEOUT, $options['timeout']);
curl_setopt($this->fp, CURLOPT_CONNECTTIMEOUT, $options['timeout']);
curl_setopt($this->fp, CURLOPT_REFERER, $url);
curl_setopt($this->fp, CURLOPT_USERAGENT, $options['useragent']);
curl_setopt($this->fp, CURLOPT_HTTPHEADER, $headers);
Expand Down
7 changes: 5 additions & 2 deletions library/Requests/Transport/fsockopen.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public function request($url, $headers = array(), $data = array(), $options = ar

$options['hooks']->dispatch('fsockopen.remote_socket', array(&$remote_socket));

$fp = stream_socket_client($remote_socket, $errno, $errstr, $options['timeout'], STREAM_CLIENT_CONNECT, $context);
$fp = stream_socket_client($remote_socket, $errno, $errstr, ceil($options['connect_timeout']), STREAM_CLIENT_CONNECT, $context);

restore_error_handler();

Expand Down Expand Up @@ -198,7 +198,10 @@ public function request($url, $headers = array(), $data = array(), $options = ar
$options['hooks']->dispatch('fsockopen.after_request', array(&$fake_headers));
return '';
}
stream_set_timeout($fp, $options['timeout']);

$timeout_sec = (int) floor( $options['timeout'] );
$timeout_msec = $timeout_sec == $options['timeout'] ? 0 : 1000000 * $options['timeout'] % 1000000;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this number represent? We should move it to a class constant instead.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's just a million to compute the microsecond part of the timeout...

$options['timeout']  = 2.15;
$timeout_sec = (int) floor( $options['timeout'] );
$timeout_msec = $timeout_sec == $options['timeout'] ? 0 : 1000000 * $options['timeout'] % 1000000;
var_dump( $timeout_sec, $timeout_msec ); // "int 2" and "int 150000"

Not sure there's a point in using a class const, I think a comment would be better then :) I'm always reluctant to add comments in Request, as there is very few of them in here

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the most part I try and make it self-explanatory, but the issue here is that it's easy to miss a 0. A class constant ensures that there's no error usually. (Something like SECOND_IN_MICROSECONDS would work well here, IMO)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay -- const in Requests.php or, since it's only used in fsockopen, in Transport/fsockopen.php ?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Class constant for Requests_Transport_fsockopen, since it's only used there. :)

stream_set_timeout( $fp, $timeout_sec, $timeout_msec );
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't have spaces around the parameters. :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bleh. I'm too used to WP coding standards whit more whitespace to make things breathe and get more readable.


$this->info = stream_get_meta_data($fp);

Expand Down
8 changes: 8 additions & 0 deletions tests/Requests.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,12 @@ public function test30xWithoutLocation() {
$this->assertEquals(302, $response->status_code);
$this->assertEquals(0, $response->redirects);
}

/**
* @expectedException Requests_Exception
*/
public function testTimeoutException() {
$options = array('timeout' => 0.5);
$response = Requests::get('http://httpbin.org/delay/3', array(), $options);
}
}