Download the PHP package ejsmont-artur/php-circuit-breaker-bundle without Composer

On this page you can find all versions of the php package ejsmont-artur/php-circuit-breaker-bundle. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package php-circuit-breaker-bundle

What is php-circuit-breaker-bundle

Build Status

knpbundles.com

php-circuit-breaker-bundle is a Symfony 2 bundle, providing easy integration of php-circuit-breaker component.

php-circuit-breaker is the core package providing a generic PHP implementation of circuit breaker pattern. This bundle wraps it up and makes easier to use with Symfony 2 framework. Bundle uses service.xml to configure default services. It also integrates with Doctrine/Cache to allow you to use any cache backend (in case you were already using Doctrine/Cache).

Motivation & Benefits

Installation

Since Symfony 2 uses Composer, all you have to do is add a require dependency to your composer.json

"require": {
    "ejsmont-artur/php-circuit-breaker-bundle": "0.1.*"
},

Then you can override defaults of threshold and timeout in your application services.yaml

parameters:
    # Allowed amount of failures before marking service as unavailable
    ejsmont_circuit_breaker.threshold: 3
    # how many seconds should we wait before allowing a single request
    ejsmont_circuit_breaker.retry_timeout: 5

After that you should update composer dependencies and you are good to go.

Examples

Below you can see a few ways of obtaining instances of circuit breaker component. You can also see how to use it once you get an instance. For more documentation please see php-circuit-breaker page.

Example 1 - default APC storage

This is the simplest example as you use defaults for all settings and default APC storage. Circuit breaker status information will be serialised into APC cache.

$circuitBreaker = $this->get('apcCircuitBreaker');

Yes, that is it. You can use predefined service called apcCircuitBreaker and you do not need any settings. It will use static factory and keep returning the same instance during script run.

Example 2 - configuration via dependency injection

Beside of "apcCircuitBreaker" service that uses APC you can use "circuitBreaker" service which is configurable and allows you to inject any doctrine cache instance.

In the example below i use "circuitBreakerCacheBackend" service to override the default behaviour of "circuitBreaker" service. Here i am using memcached but it could be any doctrine cache instance. This service affects behaviour of "circuitBreaker" only, "apcCircuitBreaker" service uses its own APC cache instance.

Configure as needed in service.yaml of your app:

services:
    circuitBreakerCacheBackend:
        class: Doctrine\Common\Cache\MemcachedCache
        calls:
          -   [setMemcached, ["@memcachedInstance"]]
    memcachedInstance:
        class: Memcached
        calls:
            - [addServer, ['127.0.0.1', 11211, 1]]

Then in your code you can use the configurable doctrine cache instance like below:

 $circuitBreaker = $this->get('circuitBreaker');

Example 3 - manual composition

If you wanted to do it for some reason you can also create instance of circuit breaker by hand. In this example we use Doctrine\Cache adapter so all you need to provide is the cache instance. In this case, just to mix it up, we have decided to use file cache.

$fileCache = new \Doctrine\Common\Cache\FilesystemCache('/tmp/cache/', '.cache');
$circuitBreaker = Factory::getDoctrineCacheInstance($fileCache);

Example of how to use an instance

See more details of Circuit Breaker pattern on php-circuit-breaker github page and my blog posts on circuit breaker

In short, once you get instance of circuit breaker, you can ask it if particular service is available or not. Circuit breaker will check it's status metrics and give you response based on its previous records. After a successful connection to the service you should tell Circuit Breaker that it went ok. In case of service failure or timeout, you should report failure to circuit breaker.

This way Circuit breaker "learns" what is the current status of each service (names are arbitrary strings). You can define threshold and retry timeout to allow single request from time to time in case service got fixed.

if ($circuitBreaker->isAvailable("UserProfileService1")) {
    try{
        // do something useful with the service
        $circuitBreaker->reportSuccess('UserProfileService1');
    }catch(ServiceCallFailedServiceDown $e){
        // if service is down report it back to circuit breaker
        $circuitBreaker->reportFailure('UserProfileService1');
        // handle as temporarily unavailable (or remove some features)
    }
}else{
    // handle as temporarily unavailable (or remove some features)
}   

Running tests

If you dont have composer, get it and download dependencies (creates /vendor folder)

curl -s http://getcomposer.org/installer | php
php composer.phar update

You can run all tests using ant:

ant phpunit

You can run tests, generate coverage and docs:

ant ci

You can run selected test case by running:

cd tests
phpunit Unit/Ejsmont/CircuitBreakerBundle/Storage/DoctrineCacheAdapterTest.php

Author


All versions of php-circuit-breaker-bundle with dependencies

PHP Build Version
Package Version
Requires php Version >=5.3.0
symfony/symfony Version 2.1.*
doctrine/common Version >=2.0.0
ejsmont-artur/php-circuit-breaker Version >=0.1.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package ejsmont-artur/php-circuit-breaker-bundle contains the following files

Loading the files please wait ....