承接 gousto/replay 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

gousto/replay

Composer 安装命令:

composer require gousto/replay

包简介

Replay comes in allowing you to plan re-tries and fallbacks strategies for your functions.

README 文档

README

CircleCI GitHub tag Scrutinizer

Re-play functions that error out!

Some times things not always works at the first attempt. Replay comes in allowing you to plan re-tries and fallbacks strategies for your functions.

Install

composer require gousto/replay

Features

  • Re-try functions within exception scope
  • Increase delay for every attempt
  • Create advanced re-try fallback strategies

Usage

If you want to replay a function when it throws an Exception here is an example:

<?php

use Gousto\Replay\Replay;

try {

    $result = Replay::retry(3, function() {
       return Http::get('example.com'); 
    }, 50);
    
    Log::info("All good");
} catch(\Gousto\Replay\RetryLogicException $e) {
    \Log::error("After 3 times the function still error out!");
}

This code will return you right away the result of Http::get(), if no Exception is occured.

In case of Exception it will retry again up to 3 times with a 50ms interval.

Increase delay at every attempt:

Let's say we want to have a re-try strategy that want to increase the interval at every attempt i.e: 1st attempt 10ms, 2nd attempts 20ms 3rd attempt 40ms

<?php

use Gousto\Replay\Replay;

$replay = Replay::times(3, function() {

    throw new LogicException();
  
}, 10);

$replay->onRetry(function(Exception $e, Replay $replay) {
   $replay->setDelay($replay->getDelay() * 2); 
});

// play the strategy!
$replay->play();

Custom Exceptions

Replay allow you to trigger the retry logic only for particular Exceptions, pass an array of Exceptions class names as 4th paramter:

<?php

use Gousto\Replay\Replay;

// Will throw LogicException and not retry
Replay::retry(3, function() {

    throw new LogicException();
  
}, 0, [RuntimeException::class]);

This will throw a LogicException when the function is invoked. We are going to retry only when the RuntimeException is thrown.

API

- retry($attempts = 1, Closure $user_function, $delay = 0, $exception_targets = [Exception::class])

Return the result of the Closure if no exception is occured. Alternatevely it retries invoking the function until the number of attempts specified. Throw Gousto\Replay\RetryLogicException::class if the number of attempts remained is 0.

  • $attempts: int, number of attempts the function might be replayed
  • $user_function: Closure, function which will be replayed if errored
  • $delay: int, delay is measured in milliseconds
  • $exception_targets: array, list of exceptions which will be considered as errors
use Gousto\Replay\Replay;

Replay::retry(1, function() {

    throw new Exception();
  
}, 0, [Exception::class]);

- times($attempts = 1, Closure $user_function, $delay = 0, $exception_targets = [])

Helper function for instantiate Replay, facilitate chaining

  • $attempts: int, number of attempts the function might be replayed
  • $user_function: Closure, function which will be replayed if errored
  • $delay: int, delay is measured in milliseconds
  • $exception_targets: array, list of exceptions which will be considered as errors
use Gousto\Replay\Replay;

$replay = Replay::times(1, function() {

    throw new Exception();
  
}, 0, [Exception::class]);

$replay->play();

play($silent = false):

Tell replay to invoke the function and use the retry logic if needed.

  • $silent: bool, if true will not throw RetryLogicException but will return it.
use Gousto\Replay\Replay;

$replay = Replay::times(2, function() {

    throw new Exception();
  
}, 0, [Exception::class]);

$result = $replay->play(true); // $result instanceof RetryLogicException

$replay->play(); // throw RetryLogicException

onRetry(Closure $handler):

Hook a function that will be trigger for every attempt, this is useful for logging or any other action.

use Gousto\Replay\Replay;

$counter = 0;
$replay = Replay::times(3, function() use (&$counter) {
    $counter++;
    if ($counter === 3) {
        return $counter;
    }
    throw new Exception("Error"); 
});

// WILL LOG:
// Attempt 1: Failed
// Attempt 2: Failed
$replay->onRetry(function(Exception $exception, Replay $replay) {
   Log::error("Attempt {$replay->attempts()}: Failed"); 
});

try {
    $replay->play();
} catch(\Gousto\Replay\RetryLogicException $e) {
    
}

next():

The next function allow you to manually control the retry cycle

use Gousto\Replay\Replay;

$replay = Replay::times(3, function() {

    throw new Exception();
  
}, 0, [Exception::class]);

$replay->next(); // 1 attempt
$replay->next(); // 2 attempts
$replay->next(); // Throw RetryLogicException

attempts():

Return the number of retry attempts

use Gousto\Replay\Replay;

$replay = Replay::times(3, function() {

    throw new Exception();
  
}, 0, [Exception::class]);

$replay->next();
$replay->attempts(); // 1

$replay->next();
$replay->attempts(); // 2

isErrored():

Return the state of the function invokation

$replay = Replay::times(4, function() {

    return 10;
  
}, 0, [Exception::class]);

$result = $replay->play(); // 10

$replay->isErrored(); // False

Credits

www.gousto.co.uk

gousto/replay 适用场景与选型建议

gousto/replay 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 37.98k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2017 年 07 月 29 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 gousto/replay 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 gousto/replay 我们能提供哪些服务?
定制开发 / 二次开发

基于 gousto/replay 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-07-29