PHP code example of thesis / grpc-retry

1. Go to this page and download the library: Download thesis/grpc-retry 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/ */

    

thesis / grpc-retry example snippets


use Thesis\Grpc\Client;
use Thesis\Grpc\Retry;

$retry = new Retry\Interceptor(new Retry\Config(maxAttempts: 5));

$client = new Client\Builder()
    ->withUnaryInterceptors($retry)
    ->withStreamInterceptors($retry)
    ->build();

use Google\Rpc\Code;
use Thesis\Grpc\Retry\Backoff;
use Thesis\Grpc\Retry\Config;

new Config(
    maxAttempts: 3,                        // total attempts, including the initial call
    retryableCodes: [Code::UNAVAILABLE],   // statuses that make a failed call eligible for a retry
    backoff: new Backoff\Exponential(),    // pause between attempts
);

interface Backoff
{
    /**
     * @param positive-int $attempt
     * @return float
     */
    public function delay(int $attempt): float;
}

use Thesis\Grpc\Retry\Backoff\Exponential;

new Exponential(
    base: 0.1,      // delay before the first retry, in seconds
    factor: 2.0,    // multiplier per attempt: 0.1 → 0.2 → 0.4 → ...
    max: 30.0,      // upper bound for a single delay
    jitter: 0.2,    // ±20% random deviation
);

new Exponential(randomizer: new Random\Randomizer(new Random\Engine\Mt19937(seed: 42)));

use Thesis\Grpc\Retry\Backoff\Fixed;

new Fixed(0.25); // always wait 250ms