承接 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.

统计信息

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

GitHub 信息

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

其他信息

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

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固