s-patompong/php-retrier

PHP Retrier 允许您轻松重试您的逻辑。

1.0.0 2021-09-23 10:21 UTC

This package is auto-updated.

Last update: 2024-09-23 17:02:14 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Retrier 可以帮助您轻松重试逻辑。

<?php

// Your own API class
use App\Api\ApiConnector;
use SPatompong\Retrier\Retrier;

$api = new ApiConnector();

// By default, Retrier use RetryThrowableStrategy which will retry if the result is an instance of \Throwable
$result = (new Retrier())
    ->setLogic(function() use($api) {
        return $api->get();
    })
    ->execute();

安装

您可以通过 composer 安装此包

composer require s-patompong/php-retrier

使用

字段及其默认值

最小配置示例

<?php

// Your own API class
use App\Api\ApiConnector;
use SPatompong\Retrier\Retrier;

$api = new ApiConnector();

$retrier = (new Retrier())
    ->setLogic(function() {
        return 0;
    });

// After 3 retries, it's possible that the code still gets the \Throwable
// Thus, we still need to put it in a try/catch block
try {
    $value = $retrier->execute();
} catch(\Throwable $t) {
    echo "Still gets throwable after 3 retries.\n";
}

完整配置示例

<?php

// Your own API class
use App\Api\ApiConnector;
use SPatompong\Retrier\Retrier;
use SPatompong\Retrier\Presets\Strategies\RetryNullStrategy;

$api = new ApiConnector();

// Keep track of retry count, useful for logging or echoing to the terminal
$retryCount = 0;

$value = (new Retrier())
    // Change the stragegy to RetryNullStrategy to keep retrying if the Logic returns null
    ->setRetryStrategy(new RetryNullStrategy())
    
    // Set the wait time for each retry to 10 seconds
    ->setDelay(10)
    
    // Let the code retry 5 times
    ->setRetryTimes(5)
    
    // Set the onRetryListener to print out some useful log
    ->setOnRetryListener(function ($currentTryCount, $value, $throwable) use (&$retryCount) {
        $retryCount++;
        echo "Failed to get API data, retry count: $retryCount\n";
    })
    
    // Set the logic
    ->setLogic(fn () => $api->get())
    
    // Execute it
    ->execute();
    
// At this point, value could still be null if after 5 times the code still couldn't get the API data
echo $value;

设置逻辑或重试监听器时,也可以使用可调用的数组语法

<?php

use SPatompong\Retrier\Retrier;
use SPatompong\Retrier\Tests\Helpers\FakeClass;

$fakeClass = new FakeClass();

$publicMethodResult = (new Retrier())
    ->setLogic([$fakeClass, 'fakePublicMethod'])
    ->execute();
    
$staticMethodResult = (new Retrier())
    ->setLogic([FakeClass::class, 'fakeStaticMethod'])
    ->execute();

重试策略

RetryStrategy 是一个实现 RetryStrategy 接口的类。Retrier 类使用它来确定是否应该重试(给定逻辑的返回值)。

<?php

namespace SPatompong\Retrier\Contracts;

interface RetryStrategy
{
    /**
     * Add a logic to check if the retrier should retry
     *
     * @param mixed $value
     * @return bool
     */
    public function shouldRetry(mixed $value): bool;
}

此库提供了两种预设策略

  1. RetryThrowableStrategy - 这是一个默认策略,将重试任何 \Throwable 响应。
  2. RetryNullStrategy - 此策略将在响应为 NULL 时持续重试。

如果您想有一个自定义的 shouldRetry() 逻辑,您可以创建自己的 RetryStrategy 类并实现此 RetryStrategy 接口。

<?php

namespace App\RetryStrategies;

use SPatompong\Retrier\Contracts\RetryStrategy;
use GuzzleHttp\Exception\ClientException;

class RetryGuzzleClientExceptionStrategy implements RetryStrategy
{
    public function shouldRetry(mixed $value): bool
    {
        return $value instanceof ClientException;
    }
}

然后,将其设置为 Retrier 的重试策略

<?php

use SPatompong\Retrier\Retrier;
use App\RetryStrategies\RetryGuzzleClientExceptionStrategy;

$retrier = (new Retrier())
    ->setRetryStrategy(new RetryGuzzleClientExceptionStrategy())
    
try {
    $retrier->execute();
} catch(\Throwable $t) {
    // Still gets ClientException after retry or other type of Throwable
}

测试

composer test

变更日志

有关最近更改的更多信息,请参阅变更日志

贡献

有关详细信息,请参阅贡献指南

安全漏洞

有关如何报告安全漏洞的详细信息,请参阅我们的安全策略

致谢

许可

MIT 许可证 (MIT)。有关更多信息,请参阅许可文件