定制 nevsnode/backoff 二次开发

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

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

nevsnode/backoff

最新稳定版本:v1.3.0

Composer 安装命令:

composer require nevsnode/backoff

包简介

Simple PHP Backoff Library

README 文档

README

Very simple Backoff PHP library. It provides an easy solution to wait after failures with an increasing delay.

Install

composer require nevsnode/backoff

Example Usage

<?php
use nevsnode\Backoff;

$backoff = new Backoff();

$resource = new ExampleResource();
$result = $backoff->retryOnException(5, function () use ($resource) {
    $return = $resource->fetchSomething();
    if (!$return) {
        throw new Exception('Failed to fetch something');
    }
    return $return;
});

By default, when exceeding the number of retries, the last exception will just be thrown again. Optionally a final closure/value can be defined which is executed or returned instead:

<?php
$backoff = new nevsnode\Backoff();

// $result will now become the string "Some error"
$result = $backoff->retryOnException(3, function () {
    throw new \Exception('Some error');
}, function ($e) {
    return $e->getMessage();
});

// $result will now become FALSE
$result = $backoff->retryOnException(2, function () {
    throw new \Exception('Some error');
}, false);

Settings

Setting Type Default Description
min Integer 1000 Minimum delay (in ms)
max Integer 30000 Maximum delay (in ms)
factor Float 2.0 Multiplicator of delay on additional delays
jitter Boolean true Allow jitter
jitterMax Integer 2000 Maximum jitter (in ms)
exceptions List of Strings every Throwable/Exception Specific list of exception-classes which will be retried

The settings can be passed as an associative array to the constructor and returned or adjusted after instantiation:

<?php

// pass settings to constructor
$backoff = new Backoff([
    'min' => 2000,
    'max' => 10000,
    'factor' => M_E,
]);

// define setting through setter
$backoff->setJitter(true);
$backoff->setJitterMax(6000);
$backoff->setMin(3000);

// return setting through getter
$max = $backoff->getMax();

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-05-22