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

Fledge Fiber 13.x

Examples

Copyable examples for concurrent fibers, Guzzle on the Fledge handler, driver configuration and integration flags.

Concurrent closures

async() starts a closure in its own fiber and disperse() runs several at once, returning results in input order. Both are defined in Fledge\Async (src/Fledge/Async/functions.php).

use function Fledge\Async\async;
use function Fledge\Async\delay;
use function Fledge\Async\disperse;
 
$future = async(function (): string {
delay(0.1);
 
return 'done';
});
 
echo $future->await();
 
$results = disperse([
'a' => function (): int {
delay(0.05);
 
return 1;
},
'b' => function (): int {
delay(0.05);
 
return 2;
},
]);
// ['a' => 1, 'b' => 2]

Sequential and concurrent, live

Five 100 ms delays, first one after the other, then together with disperse(). The server runs both inside this request and reports the elapsed time of each.

Running...
use Fledge\Async\TimeoutCancellation;
use function Fledge\Async\delay;
use function Fledge\Async\disperse;

// Sequential: five 100 ms delays, one after the other
for ($i = 0; $i < 5; $i++) {
    delay(0.1);
}

// Concurrent: the same five delays, each in its own fiber
disperse(array_fill(0, 5, function (): int {
    delay(0.1);

    return 1;
}), new TimeoutCancellation(1.0));

A Guzzle client on the Fledge handler

FledgeGuzzle hands out Guzzle pieces that run on the Fledge handler instead of curl.

use Fledge\Fiber\Http\FledgeGuzzle;
 
$stack = FledgeGuzzle::stack(); // HandlerStack: redirects, cookies, http_errors, prepare-body on FledgeHandler
$client = FledgeGuzzle::client(['base_uri' => 'https://api.example.com']);
$limited = FledgeGuzzle::client([], 8); // at most 8 open connections per host
$factory = FledgeGuzzle::factory(8); // the shared AsyncClientFactory for that limit
FledgeGuzzle::flush(); // drop the shared pools (tests)

stack(?int $perHost = null) returns a fresh HandlerStack on each call. client(array $options = [], ?int $perHost = null) returns a GuzzleHttp\Client on it, and a handler in $options wins. Connection pools live in one AsyncClientFactory per per-host limit, so keep-alive connections are reused across integrations.

Choose the drivers

DB_CONNECTION=fledge-mysql
REDIS_CLIENT=fledge

fledge-mysql and fledge-mariadb request TLS when at least one of Pdo\Mysql::ATTR_SSL_CA, ATTR_SSL_CAPATH, ATTR_SSL_CERT, ATTR_SSL_KEY or ATTR_SSL_CIPHER is present in options. To avoid fetching the server's RSA key over a connection without TLS, pin it or refuse the fetch:

'options' => [Pdo\Mysql::ATTR_SERVER_PUBLIC_KEY => '/path/to/server-public.pem'],
// or
'secure_key_retrieval' => true,

For MySQL 5.7, set the version in the connection config to get the NO_AUTO_CREATE_USER strict mode string:

'version' => '5.7',

Turn integrations off

Mail, Pusher/Reverb broadcasting, S3 and Elasticsearch use the Fledge HTTP client by default. Set a flag to false to return one integration to its previous client.

FLEDGE_HTTP_MAIL=false
FLEDGE_HTTP_BROADCASTING=false
FLEDGE_HTTP_S3=false
FLEDGE_HTTP_ELASTICSEARCH=false

The configuration lives in config/fledge-http.php, merged by the provider. It holds scalars only, so it is safe with config:cache; run php artisan config:cache again after changing an env var. All integrations are inactive while PHPUnit runs.

Note: Restart long-lived workers after changing a flag so they pick it up.

Per-host connection limits are set under fledge-http.pool_per_host.* (null means unlimited): mail null, broadcasting 8, s3 null, elasticsearch 8.

Select the event loop driver

FLEDGE_EVENT_LOOP=poll

Accepted values are auto, poll and select. select keeps the stream_select() driver.