该包已被弃用且不再维护。未建议替代包。

为 Guzzle 的重试行为

1.0.0 2017-07-07 07:43 UTC

This package is not auto-updated.

Last update: 2021-06-26 12:04:09 UTC


README

Guzzle 的重试行为

安装

添加依赖 mikeevstropov/guzzle

$ composer require mikeevstropov/guzzle

新选项

  • requests_limit

    接受

    • integer - 正整数

    默认值

    • 1 - 只尝试一次

    接收响应的最大尝试次数。

  • repeat_on

    接受

    • array - 数值索引数组

    默认值

    • array(0, 5) - 重复失败的请求或如果响应码是 5xx

    重试请求的错误码列表

    • array(5) - 在 GuzzleHttp\Exception\ServerException (5xx 代码) 时
    • array(4) - 在 GuzzleHttp\Exception\ClientException (4xx 代码) 时
    • array(0) - 其他 TransferExceptionGuzzleHttp\Exception\ConnectException

    可以组合使用,如 array(4, 5).

使用

<?php

$client = new \GuzzleHttp\Client();

// Let's try to request "http://httpstat.us/503" that
// page will always return "503 Service Unavailable"
$response = $client->get('http://httpstat.us/503', [
    'requests_limit' => 3,
]); // will thrown GuzzleHttp\Exception\ServerException after 3 attempts

// We can pass option "repeat_on" to prevent retrying
// if response has code 5xx (by default [0, 5])
$response = $client->get('http://httpstat.us/503', [
    'requests_limit' => 3,
    'repeat_on' => array(0, 4)
]); // will thrown GuzzleHttp\Exception\ServerException after first request

// But same options with request to the page that return 4xx
// will have 3 attempts, because we pass "4" as array item in
// option "repeat_on"
$response = $client->get('http://httpstat.us/402', [
    'requests_limit' => 3,
    'repeat_on' => array(0, 4)
]); // will thrown GuzzleHttp\Exception\ServerException after 3 attempts

开发

克隆

$ git clone https://github.com/mikeevstropov/guzzle.git

进入项目

$ cd guzzle

安装依赖

$ composer install

增加 composer 超时。因为 composer 默认设置为 300 秒。

$ composer config --global process-timeout 600

运行测试

$ composer test