GithubHelp home page GithubHelp logo

flix-tech / guzzle-circuit-breaker-middleware Goto Github PK

View Code? Open in Web Editor NEW
9.0 6.0 0.0 9 KB

A Guzzle 6 middleware to integrate the Circuit Breaker implementation by https://github.com/ejsmont-artur/php-circuit-breaker

License: MIT License

PHP 100.00%
guzzle guzzlehttp guzzle-middleware circuit-breaker php php7 flix-tech flixbus

guzzle-circuit-breaker-middleware's Introduction

Guzzle 6 Circuit-Breaker Middleware

Build Status

This is a Guzzle 6 middleware that integrates https://github.com/ejsmont-artur/php-circuit-breaker.

Contents

Requirements

Dependencies

Dependency Version Reason
php ~7.0 Anything lower has (almost) reached EOL
guzzlephp/promises ~6.2 Middlewares pass Promises around
psr/http-message ~1.0 Standardization, doh
ejsmont-artur/php-circuit-breaker ~0.1 Simple and robust Circuit Breaker implementation for PHP

Installation

This library is installed via composer.

composer require "flix-tech/guzzle-circuit-breaker-middleware=~1.0"

Usage

NOTE It is recommended that this middleware is on top of the stack so that if a service is unavailable, it can instantly reject without going further down the chain.

With the default configuration, this middleware will inspect the PSR-7 requests for either a transport option key "circuit_breaker.requested_service_name" exposed via Middleware::CB_TRANSFER_OPTION_KEY in the request options array, or a request header "X-CB-Service-Name" exposed via Middleware::CB_SERVICE_NAME_HEADER. You can pass an own service name extractor callable that takes the form f(RequestInterface $request, array $requestOptions): string.

The service name will be used to look up the availability in the circuit breaker. If it is not available, it will be instantly rejected with a CircuitBreakerIsClosedException.

If the request was successful, the middleware will report a success to the circuit breaker for the given service. Otherwise it will report a failure and pass the rejection further down the chain.

You can pass a custom exception map into the middleware with which you can control which exception types and values should actually trigger a failure report. I.e. a 404 might be a failed configuration issue and might not trigger failure reports. The exception map callback takes the form f($rejectedValue): bool.

Example

<?php

use Ejsmont\CircuitBreaker\Factory;
use FlixTech\CircuitBreakerMiddleware\Middleware;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Handler\CurlHandler;
use Psr\Http\Message\RequestInterface;

$circuitBreaker = Factory::getSingleApcInstance();

$serviceNameExtractor = function (RequestInterface $request, array $options) {
    if (\array_key_exists('my_custom_option_key', $options)) {
        return $options['my_custom_option_key'];
    }
    
    return null;
};

$exceptionMap = function ($rejectedValue) {
    if ($rejectedValue instanceof RequestException && $rejectedValue->getResponse()) {
        return 404 !== $rejectedValue->getResponse()->getStatusCode();
    }
    
    return true;
};

$middleware = new Middleware(
    $circuitBreaker,
    $serviceNameExtractor,
    $exceptionMap
);

$handlerStack = HandlerStack::create(new CurlHandler());
$handlerStack->push($middleware);

$client = new Client(['handler' => $handlerStack]);

Testing

To run the tests, just run ./vendor/bin/phpunit from the root of the project after installing the dev requirements.

Contributing

In order to contribute to this library, follow this workflow:

  • Fork the repository
  • Create a feature branch
  • Work on the feature
  • Run tests to verify that the tests are passing
  • Open a PR to the upstream master branch
  • See any Travis CI messages popping up on your PR
  • Be happy about contributing to open source!

guzzle-circuit-breaker-middleware's People

Contributors

tpl0ch avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.