PHP code example of gohany / circuitbreaker-symfony-bundle

1. Go to this page and download the library: Download gohany/circuitbreaker-symfony-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

gohany / circuitbreaker-symfony-bundle example snippets


// config/services.php

use Gohany\Circuitbreaker\Util\ExtRedisClient;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use function Symfony\Component\DependencyInjection\Loader\Configurator\service;

return static function (ContainerConfigurator $c): void {
    $services = $c->services();

    $services->set('gohany.circuitbreaker.redis_client', ExtRedisClient::class)
        ->args([service('app.redis')]); // `app.redis` is your \Redis service
};

// services.php
$services->set('gohany.circuitbreaker.redis_client', Gohany\Circuitbreaker\Util\ExtRedisClient::class)
    ->args([service('app.redis')]); // app.redis is your \Redis service

use Symfony\Component\Routing\Annotation\Route;

final class ChargesController
{
    /**
     * @Route("/courses/{courseEntity}/hydra/charges", methods={"GET"})
     * @Bulkhead(pool="db-main", lane="http.courses.charges")
     */
    public function __invoke(): Response
    {
        // ...
    }
}

/**
 * @Route("/courses/{courseEntity}/hydra/charges", name="api_courses_hydra_charges", methods={"GET"})
 * @Bulkhead(pool="db-main")
 */
public function __invoke(): Response
{
}

use Symfony\Component\Routing\Annotation\Route;

final class ChargesController
{
    /**
     * @Route("/courses/{courseEntity}/hydra/charges", methods={"GET"})
     *
     * // Parent share: counts this request against the hydra slice
     * @Bulkhead(pool="db-queries", lane="hydra")
     *
     * // Child weights: counts this request against the hydra route-priority pool
     * @Bulkhead(pool="db-hydra", lane="hydra.charges")
     */
    public function __invoke(): Response
    {
        // ...
    }
}
yaml
gohany_circuitbreaker:
  profiles:
    default:
      pools:
        # Fixed: explicit per-lane hard caps
        api-http:
          global_max: 100
          mode: fixed
          lanes:
            api_courses_hydra_charges: { max_concurrent: 10 }
            api_auth_login:            { max_concurrent: 20 }

        # Percent: lane caps are shares of global_max
        api-http-percent:
          global_max: 100
          mode: percent
          lanes:
            api_auth_login:  { percent: 0.40 }  # ~40 concurrent
            api_reporting:   { percent: 0.10 }  # ~10 concurrent

        # Weighted: relative priority under contention
        api-http-weighted:
          global_max: 100
          mode: weighted
          soft_borrow_utilization_threshold: 0.60
          lanes:
            api_auth_login:  { weight: 8 }
            api_reporting:   { weight: 1 }