-
Notifications
You must be signed in to change notification settings - Fork 498
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 ! |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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(); | ||
|
||
|
@@ -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; | ||
stream_set_timeout( $fp, $timeout_sec, $timeout_msec ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This shouldn't have spaces around the parameters. :) There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
||
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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...
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
There was a problem hiding this comment.
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)There was a problem hiding this comment.
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 ?There was a problem hiding this comment.
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. :)