~/docs
Resonate
Resonate
13.x 13.43.0.3, current
Fledge
Toggle sidebar
Fledge
webpatser/fledge-framework 13.43.0.3

Fledge 13.x

Examples

Copyable examples for concurrent tasks, concurrent middleware, parallel database queries and URI normalization.

Each example uses classes and methods that exist in v13.43.0.3.

Concurrent tasks

The fiber driver of the Concurrency facade runs tasks in one process. Tasks that use non-blocking I/O overlap while they wait.

use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Concurrency;
 
// All 3 reads overlap on socket I/O.
[$one, $two, $three] = Concurrency::driver('fiber')->run([
fn () => Cache::get('user:1'),
fn () => Cache::get('user:2'),
fn () => Cache::get('user:3'),
]);

run() returns the results in the order of the tasks and rethrows the first failure in task order. On a timeout it throws Fledge\Async\CancelledException. A task stops on a timeout only when it passes Fledge\Concurrency\TaskCancellation::current() to its awaits or calls throwIfRequested() in a loop.

Parallel database queries

With a fledge-* connection (see Installation), FiberDB::concurrent() from fledge-fiber runs queries in parallel. Set DB_CONNECTION=fledge-mysql in .env first.

use Fledge\Fiber\Database\FiberDB;
 
[$users, $posts, $count] = FiberDB::concurrent(
fn() => User::where('active', true)->get(),
fn() => Post::latest()->limit(10)->get(),
fn() => Comment::where('approved', false)->count(),
);

Concurrent middleware

Middleware that implements Illuminate\Contracts\Http\ConcurrentMiddleware can be grouped and run in parallel. It suits independent I/O such as Redis lookups, API key validation or rate limit checks. Each member implements two methods:

namespace Illuminate\Contracts\Http;
 
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
 
interface ConcurrentMiddleware
{
public function before(Request $request): Request|Response;
 
public function after(Request $request, Response $response): Response;
}

A Laravel 13 app has no HTTP Kernel by default. Either define $concurrentMiddleware on a custom kernel that extends Illuminate\Foundation\Http\Kernel, or call Route::concurrentMiddleware('io-checks', [...]) in a service provider. Then reference the group as concurrent:<group> in a middleware group:

// In a custom Kernel
protected $concurrentMiddleware = [
'io-checks' => [
ValidateApiKey::class, // Redis lookup
CheckRateLimit::class, // Redis lookup
LoadSubscriptionTier::class // Redis lookup
],
];
 
protected $middlewareGroups = [
'api' => [
'concurrent:io-checks',
SubstituteBindings::class,
],
];

All before() methods run concurrently, each with its own copy of the request. The first Response in registration order short-circuits the pipeline. Otherwise the request changes are merged back (later registration wins per key), the pipeline continues, and all after() methods run concurrently.

Warning: Do not put authentication or authorization middleware in a concurrent group. Every member's before() runs before a short-circuit response is chosen, so side effects such as session writes would run even for rejected requests. The framework rejects its own auth middleware in a group at registration time.

URI normalization

use Illuminate\Support\Uri;
 
$uri = Uri::of('https://bébé.be');
 
$uri->host();

The host is returned as punycode, xn--bb-bjab.be. See Native URI for the full rules.

Switch the Redis client

REDIS_CLIENT=phpredis

Use phpredis when you only make single sequential calls against a local Redis and per-call overhead matters more than non-blocking I/O.

Reproduce the benchmarks

php artisan fledge:bench --scenario=uri --iterations=10000
php artisan fledge:bench --scenario=polyfills
php artisan fledge:bench --scenario=redis