An unresponsive service can be worse than a down one. It can tie up your entire system if not handled properly. All network requests should have a timeout.
Here’s how to add timeouts for popular PHP packages. All have been tested. The default is no timeout, unless otherwise specified. Enjoy!
Also available for Ruby, Python, Node, Go, and Rust
Standard library
Packages
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 1);
No exception is raised. Use curl_error($ch)
to check for a timeout.
new GuzzleHttp\Client(['timeout' => 1]);
Raises GuzzleHttp\Exception\ConnectException
$client = (new \OpenSearch\ClientBuilder()) ->setConnectionParams(['client' => ['curl' => [CURLOPT_CONNECTTIMEOUT => 1, CURLOPT_TIMEOUT => 1]]]) ->build();
Raises OpenSearch\Common\Exceptions\NoNodesAvailableException
new Predis\Client(['timeout' => 1, 'read_write_timeout' => 1]);
Default: 5s connect timeout
Raises Predis\Connection\ConnectionException
HttpClient::create(['timeout' => 1]);
Raises Symfony\Component\HttpClient\Exception\TimeoutException
Let us know. Even better, create a pull request for it.
git clone https://github.com/ankane/php-timeouts.git cd php-timeouts composer install php tests/server.php
To run all tests, use:
To run individual tests, use:
composer test -- --filter guzzle
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4