avtonom/delay-exponential-backoff-bundle

用于PHP的Symfony2扩展包的指数退避延迟

v1.1.0 2017-02-14 16:03 UTC

This package is not auto-updated.

Last update: 2024-09-23 15:00:04 UTC


README

指数退避是一种算法,它使用反馈来乘性减少某些过程的速率,以逐渐找到一个可接受的速率。

页面扩展包: https://github.com/Avtonom/delay-exponential-backoff-bundle

    $ php bin/console exponential-backoff
    $ php bin/console exponential-backoff halfDelay -l 20
    

安装方法

在项目根目录中运行以下命令,假设您已经为项目设置了composer

composer.phar require avtonom/exponential-backoff-bundle ~1.1

~1.1切换为最新的标签。

将扩展包添加到app/AppKernel.php

$bundles(
    ...
            new Avtonom\ExponentialBackoffBundle\AvtonomExponentialBackoffBundle(),
    ...
);

配置选项(parameters.yaml)

parameters:
    avtonom_exponential_backoff.cap: 1000000 # [OPTIONAL] - Max duration allowed (in microseconds). If backoff duration is greater than cap, cap is returned
    avtonom_exponential_backoff.max_attempts: 0 # [OPTIONAL] - Number of attemps before thrown an Exception
    

API

getDefaultOptions()

此方法为静态方法,返回一个包含默认选项的数组

  • cap:允许的最大持续时间(以微秒为单位)。如果退避持续时间大于cap,则返回cap,默认为1000000微秒。
  • maxAttempts:在抛出Yriveiro\Backoff\BackoffException之前尝试的次数。默认为0,无限制。

halfDelay($attempt)

delay($attempt)

exponential($attempt)

此方法使用指数函数E(attempt) = (2**attempt - 1)来计算退避时间。

参数

  • attempt:表示当前重试次数的增量值。

equalJitter($attempt);

指数退避有一个缺点。在高并发情况下,由于时间高度依赖于当前尝试,我们可以有多个具有相同退避时间的调用。

为了解决这个问题,我们可以添加一个抖动值以允许一些随机化。

equalJitter使用函数:E(attempt) = min(((2**attempt - 1) / 2), random(0, ((2**attempt - 1) / 2)))

参数

  • attempt:表示当前重试次数的增量值。

fullJitter($attempt);

全抖动行为类似于equalJitter方法,它们之间的主要区别在于抖动值的计算方式。

fullJitter使用函数:E(attempt) = min(random(0, (2**attempt - 1) / 2))

参数

  • attempt:表示当前重试次数的增量值。

用法

    $attempt++;
    usleep($this->('avtonom_exponential_backoff')->equalJitter($attempt));
    

阅读信息 https://habrahabr.ru/post/227225/

使用 https://github.com/yriveiro/php-backoff