定制 sparkcentral/backoff 二次开发

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

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

sparkcentral/backoff

Composer 安装命令:

composer require sparkcentral/backoff

包简介

Utility trait with retry (back off) functionality

README 文档

README

Simple utility trait which provides backoff / retry functionality.

Build Status

Features

  • Two different strategies: backoffOnException, backoffOnCondition
  • You can provide list of exception classes so that retry will happen only when one of those from the list is thrown.
  • You can pass custom function to backoffOnCondition which defines whether to retry operation or not.
  • Retries happen with delays which grow linearly (y=x*2), you can pass custom starting delay as well.

For details please refer to documentation for backoffOnException(), backoffOnCondition() methods.

Dependencies

  • PHP >= 5.6 (variadics)

Basic example

<?php

use Sparkcentral\Backoff\Backoff;

class ExternalServiceWrapper
{
    use Backoff;

    private $externalApiClient;

    public function __construct(ExternalApiClient $client)
    {
        $this->externalApiClient = $client;
    }

    public function getById($id)
    {
        $result = $this->backoffOnException(
            [$this->externalApiClient, 'get'], // call 'get' method on externalApiClient
            [$id], // pass this argument to 'get'
            5, // try up to 5 times
            [ConnectException::class] // only retry on ConnectExceptions, will re-throw everything else
        );

        return $result->getObject();
    }
}

Similarly you can use backoffOnCondition() in case code you're trying to execute does not throw any exceptions, but (for instance) returns null in case of failure.

<?php

use Sparkcentral\Backoff\Backoff;

class ExternalServiceWrapper
{
    use Backoff;

    private $externalApiClient;

    public function __construct(ExternalApiClient $client)
    {
        $this->externalApiClient = $client;
    }

    public function getById($id)
    {
        $result = $this->backoffOnCondition(
            [$this->externalApiClient, 'get'],
            [$id],
            5,
            function ($result) {
                return !is_null($result);
            }
        );

        return $result->getObject();
    }
}

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-07-26

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固