2017 © Pedro Peláez
 

library backoff

Utility trait with retry (back off) functionality

image

sparkcentral/backoff

Utility trait with retry (back off) functionality

  • Thursday, November 10, 2016
  • by pulyaevskiy
  • Repository
  • 3 Watchers
  • 0 Stars
  • 17,471 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 3 Versions
  • 4 % Grown

The README.md

Backoff

Simple utility trait which provides backoff / retry functionality., (*1)

Build Status, (*2)

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., (*3)

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., (*4)

<?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();
    }
}

The Versions

10/11 2016

dev-master

9999999-dev

Utility trait with retry (back off) functionality

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

by Sparkcentral Engineering

10/11 2016

v1.1.0

1.1.0.0

Utility trait with retry (back off) functionality

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

by Sparkcentral Engineering

26/07 2015

v1.0.0

1.0.0.0

Utility trait with retry (back off) functionality

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

by Sparkcentral Engineering