Curl is very powerful and takes a lot of arguments. Not only is it used on the command line, but there are extensions of curl in PHP, and the libcurl library is well supported.

Curl projects: github.com/curl/curl

Curl specifies parameters for time control and retries

curl --help

--connect-timeout SECONDS  Maximum time allowed for connection
-m, --max-time SECONDS  Maximum time allowed for the transfer

...

--retry NUM   Retry request NUM times if transient problems occur
--retry-delay SECONDS  Wait SECONDS between retries
--retry-max-time SECONDS  Retry only within this period
Copy the code

The above is a part of the parameters related to time that I sorted out, so let’s experiment in turn.

Connection timeout parameter connect-timeout

instructions

--connect-timeout SECONDS  Maximum time allowed for connection
Copy the code

The sample

# Here we set the timeout to 2s and request an address that cannot be resolved
curl --connect-timeout 2 --url http://xxx.com

curl: (28) Connection timed out after 2002 milliseconds
Copy the code

Display connection timeout, timeout time 2002 ms. Note that the duration of this warning may vary from count to count and is usually a little longer than our preset value.

# For a high return time requirement, you can set floating-point accuracy to millisecondsCurl --connect-timeout 0.3 -- URL http://xxx.com curl: (28) Connection timed out after 300 millisecondsCopy the code

Request timeout –max-time

instructions

-m, --max-time SECONDS  Maximum time allowed for the transfer
Copy the code

The sample

# Here we set the timeout to 2s, sleep 2 in the application
curl --max-time 2 --url http://www.shuai.com

curl: (28) Operation timed out after 2002 milliseconds with 0 bytes received
Copy the code

Connect-time and max-time are used together:

# Here we used an unresolvable address
curl --connect-time 3  --max-time 2 --url http://xxx.com
>  curl: (28) Connection timed out after 2001 milliseconds

curl --connect-time 3  --max-time 4 --url http://xxx.com
>  curl: (28) Operation timed out after 4002 milliseconds with 0 bytes received
Copy the code

Here we find that the return result is a connection timeout of 2001 milliseconds. When used together, the connection is the minimum time, and the return time is the max-time limit.

Request retry retry

instructions

--retry NUM   Retry request NUM times if transient problems occur
Copy the code

The sample

Again, let's ask for an address for Sleep 2Curl --max-time 0.1 --retry 3 -- URL http://www.shuai.com > Warning: Transient problem: timeout Will retryin 1 seconds. 3 retries left.
> Warning: Transient problem: timeout Will retry in 2 seconds. 2 retries left.
> Warning: Transient problem: timeout Will retry in 4 seconds. 1 retries left.
> curl: (28) Operation timed out after 100 milliseconds with 0 bytes received
Copy the code

We found that three retries were made, but not immediately after failure, but 1 s after the first retry, 2 s after the second retry, 4 s after the third retry, and so on (each retry is limited by max-time).

Retry timeout retry-max-time

We find that our max-time only restricts the time of a single request, thereby affecting the total retry time, but what if we want to complete the retry in unit time? Curl also provides a retry-max-time timeout for retries

Curl --retry 3 --retry-max-time 2 --max-time 0.1 -- URL http://www.shuai.com > Warning: Transient problem: timeout Will retryin 1 seconds. 3 retries left.
> Warning: Transient problem: timeout Will retry in 2 seconds. 2 retries left.
> curl: (28) Operation timed out after 101 milliseconds with 0 bytes received

Copy the code

We configured three retries with a total timeout of 2 seconds, but the timeout ended after only two retries.

Retry delay Retry-delay

As we discussed in request retries, instead of retries immediately after failure, the default retry time is incremental, and we can use retry-delay to control the retry interval.

# Here we set the retry time to 5s and retry 3 timesCurl --retry 3 --retry-delay 5 --max-time 0.1 -- URL http://xxx.com > Warning: Transient problem: timeout Will retryin 5 seconds. 3 retries left.
> Warning: Transient problem: timeout Will retry in 5 seconds. 2 retries left.
> Warning: Transient problem: timeout Will retry in 5 seconds. 1 retries left.
> curl: (28) Connection timed out after 101 milliseconds
Copy the code

We find that Will retry in becomes 5 s per time

PHP uses the Guzzle package

The Guzzle is a PHP HTTP client for sending requests easily and integrating them into our WEB service.

Fast installation

{
  "require": {
    "guzzlehttp/guzzle": "6.0 ~ 5.3 | ~"
  },
  "repositories": {
    "packagist": {
      "type": "composer"."url": "https://mirrors.aliyun.com/composer/"}}}Copy the code

Perform composer

composer install
Copy the code

Sample code (timeout mechanism)

require "./vendor/autoload.php";


$client = new GuzzleHttp\Client();

$res = $client->request('GET'.'http://xxx.com'['connect_timeout'= > 3,'timeout'= > 2,]);echo $res->getBody() . PHP_EOL;
Copy the code

The output:

PHP Fatal error:  Uncaught GuzzleHttp\Exception\ConnectException: 
cURL error 28: Connection timed out after 2002 milliseconds
(see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
....
Copy the code

We have configured a connect_TIMEOUT timeout period of 3 s and a timeout period of 2 s

Guzzle retry mechanism

The retry mechanism is a bit cumbersome and needs to be implemented using Middleware, but it’s easy to understand

require "./vendor/autoload.php";

$handlerStack= GuzzleHttp\HandlerStack::create(); // Bind middleware and retry three times$restryCount = 3;
$handlerStack->push(\GuzzleHttp\Middleware::retry(function () use (&$restryCount) {
    if (--$restryCount< = 0) {return false;
    }
    return true;
}, function () use ($restryCount) {
    return1000; / / ms}));$client = new GuzzleHttp\Client(['handler'= >$handlerStack]);

$res = $client->request('GET'.'http://xxx.xxx.com'['connect_timeout'= > 3,'timeout'= > 2,]);echo $res->getBody() . PHP_EOL;
Copy the code

When defining the retry time, you need to implement policies such as whether to continue retry and when to retry, providing great retry flexibility.

Note that curl retries in seconds, whereas this is set to milliseconds.