定制 tagadvance/elephant-retrying 二次开发

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

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

tagadvance/elephant-retrying

Composer 安装命令:

composer require tagadvance/elephant-retrying

包简介

This is a port of the guava-retrying module to allow for the creation of configurable retrying strategies for an arbitrary function call, such as something that talks to a remote service with flaky uptime.

README 文档

README

Build Status Coverage Status License

What is this?

The elephant-retrying module provides a general purpose method for retrying arbitrary PHP code with specific stop, retry, and exception handling capabilities.

This is a fork of the excellent guava-retrying code posted here by Ray Holder (rholder).

Port Accommodations

  • Generics have been removed because PHP does not support generics.
  • All time units have been converted to seconds of type float.
  • Time limiter has been removed as such functionality would be difficult to implement in PHP.

Composer

composer require tagadvance/elephant-retrying

Quickstart

A minimal sample of some of the functionality would look like:

$retryer = RetryerBuilder::newBuilder()
	->retryIfResult('is_null')
	->retryIfExceptionOfType(\RuntimeException::class)
	->withStopStrategy(StopStrategies . stopAfterAttempt(3))
	->build();
try {
	$retryer->call(fn() => true); // do something useful here
} catch (RetryException $e) {
	print $e->getTraceAsString();
} catch (ExecutionException $e) {
	print $e->getTraceAsString();
}

This will retry whenever the result of the Callable is null, if an IOException is thrown, or if any other RuntimeException is thrown from the call() method. It will stop after attempting to retry 3 times and throw a RetryException that contains information about the last failed attempt. If any other Exception pops out of the call() method it's wrapped and rethrown in an ExecutionException.

Exponential Backoff

Create a Retryer that retries forever, waiting after every failed retry in increasing exponential backoff intervals until at most 5 minutes. After 5 minutes, retry from then on in 5 minute intervals.

$maximumWaitTimeSeconds = 300; // 5 minutes
$retryer = RetryerBuilder::newBuilder()
        ->retryIfExceptionOfType(\RuntimeException::class)
        ->withWaitStrategy(WaitStrategies::exponentialWait(1, $maximumWaitTimeSeconds))
        ->withStopStrategy(StopStrategies::neverStop())
        ->build();

You can read more about exponential backoff and the historic role it played in the development of TCP/IP in Congestion Avoidance and Control.

Fibonacci Backoff

Create a Retryer that retries forever, waiting after every failed retry in increasing Fibonacci backoff intervals until at most 2 minutes. After 2 minutes, retry from then on in 2 minute intervals.

$maximumWaitTimeSeconds = 120; // 2 minutes
$retryer = RetryerBuilder::newBuilder()
	->retryIfExceptionOfType(\RuntimeException::class)
	->withWaitStrategy(WaitStrategies::fibonacciWait(1, $maximumWaitTimeSeconds))
	->withStopStrategy(StopStrategies::neverStop())
	->build();

Similar to the ExponentialWaitStrategy, the FibonacciWaitStrategy follows a pattern of waiting an increasing amount of time after each failed attempt.

Instead of an exponential function it's (obviously) using a Fibonacci sequence to calculate the wait time.

Depending on the problem at hand, the FibonacciWaitStrategy might perform better and lead to better throughput than the ExponentialWaitStrategy - at least according to A Performance Comparison of Different Backoff Algorithms under Different Rebroadcast Probabilities for MANETs.

The implementation of FibonacciWaitStrategy is using an iterative version of the Fibonacci because a (naive) recursive version will lead to a StackOverflowError at a certain point (although very unlikely with useful parameters for retrying).

Inspiration for this implementation came from Efficient retry/backoff mechanisms.

Source

git clone git@github.com:tagadvance/elephant-retrying.git

Clean, Install, and Test

./make

License

The guava-retrying module is released under version 2.0 of the Apache License.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: Apache-2.0
  • 更新时间: 2020-02-16

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固