PHP code example of wal3fo / laravel-sentinel

1. Go to this page and download the library: Download wal3fo/laravel-sentinel 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/ */

    

wal3fo / laravel-sentinel example snippets




return [
    'defaults' => [
        'threshold' => 50.0,
        'min_requests' => 10,
        'cooldown_seconds' => 60,
        'release_delay_seconds' => 30,
        'half_open_probe_limit' => 1,
    ],

    'services' => [
        'mail' => [
            'threshold' => 40.0,
            'min_requests' => 20,
            'cooldown_seconds' => 120,
            'release_delay_seconds' => 20,
            'half_open_probe_limit' => 1,
        ],

        'crm' => [
            'threshold' => 30.0,
            'min_requests' => 8,
            'cooldown_seconds' => 45,
            'release_delay_seconds' => 15,
        ],
    ],
];



namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Wal3fo\LaravelSentinel\Middleware\CircuitBreakerMiddleware;

final class SendEmailJob implements ShouldQueue
{
    use Dispatchable;
    use InteractsWithQueue;
    use Queueable;
    use SerializesModels;

    public function middleware(): array
    {
        return [
            new CircuitBreakerMiddleware('mail'),
        ];
    }

    public function handle(): void
    {
        // Call external provider here.
    }
}

public function middleware(): array
{
    return [
        new CircuitBreakerMiddleware(['mail', 'crm']),
    ];
}



namespace App\Jobs;

use Illuminate\Contracts\Queue\ShouldQueue;
use Wal3fo\LaravelSentinel\Attributes\UseCircuitBreaker;
use Wal3fo\LaravelSentinel\Middleware\CircuitBreakerMiddleware;

#[UseCircuitBreaker(['mail'])]
final class SendNotificationJob implements ShouldQueue
{
    public function middleware(): array
    {
        return [new CircuitBreakerMiddleware()];
    }

    public function handle(): void
    {
        // Perform work.
    }
}



namespace App\Support;

use Throwable;
use Wal3fo\LaravelSentinel\Contracts\FailureClassifier;

final class ApiFailureClassifier implements FailureClassifier
{
    public function shouldCount(Throwable $throwable, string $service, array $serviceConfig = []): bool
    {
        return ! $throwable instanceof \DomainException;
    }
}

'failure_classifier' => [
    'class' => App\Support\ApiFailureClassifier::class,
],
bash
php artisan vendor:publish --tag=sentinel-config