定制 s-patompong/php-retrier 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

s-patompong/php-retrier

Composer 安装命令:

composer require s-patompong/php-retrier

包简介

PHP Retrier lets you retry your logic easily.

README 文档

README

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

Retrier can help you retry your logic easily.

<?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();

Installation

You can install the package via composer:

composer require s-patompong/php-retrier

Usage

Fields and their default values:

Field Description Setter Default
delay Wait time between each retry setDelay(int $delay): Retrier 3
retryTimes Number of retry setRetryTimes(int $retryTimes): Retrier 3
onRetryListener A closure that will be called on retry setOnRetryListener(function(int $currentTryTimes, ?mixed $returnedValue, ?\Throwable $throwable): void {}): Retrier null
retryStrategy A class that implement RetryStrategy interface setRetryStrategy(RetryStrategy $retryStrategy): Retrier null

Minimal configuration example:

<?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";
}

Full configuration example:

<?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;

It's also possible to use callable array syntax when set the logic or retryListener:

<?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();

Retry Strategy

RetryStrategy is a class that implement RetryStrategy interface. The Retrier class uses it to determine if it should retry or not (given the return value from the logic).

<?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;
}

This library provides two presets strategy:

  1. RetryThrowableStrategy - This is a default strategy that will retry any \Throwable response.
  2. RetryNullStrategy - This strategy will keep retry if the response is NULL.

If you want to have a custom shouldRetry() logic, you can create your own RetryStrategy class and implement this RetryStrategy interface.

<?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;
    }
}

Then, set it as a retry strategy of the 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
}

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

统计信息

  • 总下载量: 4
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-09-23

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固