承接 enl/retry-loop 相关项目开发

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

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

enl/retry-loop

Composer 安装命令:

composer require enl/retry-loop

包简介

Retry Loop is tiny class for widely used concept of retry on failure code

README 文档

README

Retry Loop is widely used concept which can be easily explained with "retry on failure and give up after several retries".

Installation

composer require enl/retry-loop

Usage

For example, we need to push some data into remote service with the following pseudocode:

$response = $serviceClient->push($uri, $data);

But what if this remove service is temporarily down or network connection is unstable in this particular moment?

The common solution is to use try-catch. But what about several retries?

This code looks much better and does the trick:

$loop = new RetryLoop($retries = 5);
$response = $loop->run(function() use ($serviceClient, $uri, $data) {
    return $serviceClient->push($uri, $data);
});

Give up on some exceptions

Sometimes, you need to give up on several exceptions, for example, if your client throws 'BadRequestException', you should give up trying to push the data. In order to achieve this, you can specify second parameter for RetryLoop constructor:

$loop = new RetryLoop($retries = 5, $giveUpAt = [BadRequestException::class]);
$response = $loop->run(function() use ($serviceClient, $uri, $data) {
    return $serviceClient->push($uri, $data);
});

In case of BadRequestException, RetryLoop will throw LoopFailed exception with actual exception as previous.

Before Retry hook

If you need to perform some action before retry (logging, reconnection, whatever you need), just use $beforeRetry parameter:

$loop = new RetryLoop($retries = 5, $giveUpAt = [BadRequestException::class], function($e) {
    $this->log('info', 'Exception caught by retry loop, retrying: '.$e->getMessage());
});
$response = $loop->run(function() use ($serviceClient, $uri, $data) {
    return $serviceClient->push($uri, $data);
});

RetryLoop is immutable!

RetryLoop class itself is immutable and does not store any internal state except given constructor parameters, so that you can easily reuse single loop instance for several retries, if it is necessary

Builder

Moreover, there is a LoopBuilder that provides fluent interface for loop building:

$loop = RetryLoop::builder
    ->retries(5)
    ->giveUpAt(BadRequestException::class)
    ->giveUpAt(SomeOtherException::class)
    ->giveUpAt([YetAnotherException::class])
    ->beforeRetry(function() { sleep(5); })
    ->get(); // or just `run` and get result if builder is not needed after that.

enl/retry-loop 适用场景与选型建议

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

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

围绕 enl/retry-loop 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

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