PHP code example of largerio / laravel-concurrent-limiter
1. Go to this page and download the library: Download largerio/laravel-concurrent-limiter 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/ */
largerio / laravel-concurrent-limiter example snippets
use Largerio\LaravelConcurrentLimiter\LaravelConcurrentLimiter;
Route::middleware(LaravelConcurrentLimiter::with(10, 30, 'api'))->group(function () {
// ...
});
use Largerio\LaravelConcurrentLimiter\JobConcurrentLimiter;
class ProcessPayment implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable;
public function middleware(): array
{
return [
new JobConcurrentLimiter(
maxParallel: 5, // Max 5 concurrent jobs
key: 'stripe-api', // Shared key for all Stripe jobs
releaseAfter: 30, // Retry after 30 seconds if limited
shouldRelease: true // Auto-release job back to queue
),
];
}
public function handle(): void
{
// Process payment with Stripe...
}
}
// config/concurrent-limiter.php
'adaptive' => [
'enabled' => true,
'algorithm' => 'vegas', // 'vegas' or 'gradient2'
'min_limit' => 1, // Never go below this
'max_limit' => 100, // Never exceed this
'ewma_alpha' => 0.3, // EWMA smoothing (Vegas)
'sample_window' => 60, // Metrics TTL in seconds
'min_rtt_reset_samples' => 1000, // Reset minRTT after N samples (Vegas)
'rtt_tolerance' => 2.0, // Acceptable latency multiplier (Gradient2)
],
// Route configuration
Route::middleware('concurrent.limit:10,30,api')->group(...);
// ↑
// maxParallel = 10 (hard cap)
// Adaptive can only REDUCE the limit, never exceed maxParallel
// Effective limit = min(maxParallel, adaptiveLimit)