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

Add HTTP timeout option #114

Merged
merged 2 commits into from
Oct 20, 2018
Merged

Add HTTP timeout option #114

merged 2 commits into from
Oct 20, 2018

Conversation

clue
Copy link
Owner

@clue clue commented Oct 20, 2018

This now respects PHP's default_socket_timeout setting (default 60s) as a timeout for sending the outgoing HTTP request and waiting for a successful response and will otherwise cancel the pending request and reject its value with an Exception. You can now use the timeout option to pass a custom timeout value in seconds like this:

$browser = $browser->withOptions(array(
    'timeout' => 10.0
));

$browser->get($uri)->then(function (ResponseInterface $response) {
    // response received within 10 seconds maximum
    var_dump($response->getHeaders());
});

Similarly, you can use a negative timeout value to not apply a timeout at all or use a null value to restore the default handling.

Supersedes / closes #90, thank you @Rakdar!
Resolves / closes #1

Originally by @Rakdar ( clue#90 ),
rebased and squashed by @clue.
@clue clue added this to the v2.5.0 milestone Oct 20, 2018
@clue clue merged commit 0b316c9 into clue:master Oct 20, 2018
@clue clue deleted the timeouts branch October 20, 2018 14:03
@holtkamp
Copy link
Contributor

@clue very interesting feature, but what I am missing in the documentation, how to deal when a timeout occurs? Is/should that handled by the otherwise part of the Promise?

An example might clarify stuff a lot 🤓

@clue
Copy link
Owner Author

clue commented Oct 27, 2018

@holtkamp Fair point :-)

A timeout will reject the pending promise with an Exception just like any other error condition, such as failed DNS lookup or connection attempt. You can take a look at the exception message like this:

$browser = $browser->withOptions(array(
    'timeout' => 10.0
));

$browser->get($uri)->then(function (ResponseInterface $response) {
    // response received within 10 seconds maximum
    var_dump($response->getHeaders());
}, function (Exception $e) {
    echo 'Error: ' . $e->getMessage() . PHP_EOL;
});

Does this answer your question? 👍

@holtkamp
Copy link
Contributor

holtkamp commented Oct 27, 2018

Thanks! Yeah, that clarifies it.

Personally I prefer the otherwise fallback of the Promise:

$browser = $browser->withOptions(array(
    'timeout' => 10.0
));

$browser->get($uri)
->then(function (ResponseInterface $response): void {
    // response received within 10 seconds maximum
    var_dump($response->getHeaders());
})
->otherwise(function (Exception $e): void {
    echo 'Error: ' . $e->getMessage() . PHP_EOL;
});

or is that not the intention?

@clue
Copy link
Owner Author

clue commented Oct 27, 2018

@holtkamp Sure, both work fine in this case, it's mostly a matter of personal preference 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add timeout option to Browser
3 participants