PHP code example of thesis / etcd

1. Go to this page and download the library: Download thesis/etcd 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 / etcd example snippets


use Thesis\Etcd;
use Thesis\Grpc\Client;

$etcd = new Etcd\Client(
    new Client\Builder()
        ->withHost('127.0.0.1:2379')
        ->build(),
);

$etcd->put('/config/payments/timeout', '30');
$etcd->put('/config/payments/currency', 'EUR');

$kv = $etcd->get('/config/payments/timeout'); // ?KeyValue, null if absent
echo $kv?->value;                             // '30'

// Load a whole namespace at once with prefix scan.
$config = $etcd->getPrefix('/config/payments/');
foreach ($config->kvs as $kv) {
    echo "{$kv->key} = {$kv->value}\n";
}

foreach ($etcd->streamPrefix('/events/') as $kv) {
    // handle one key at a time; nothing is held in memory but the current one
}

use Thesis\Etcd\WatchEvent;
use Thesis\Etcd\WatchEventType;

$watch = $etcd->watchPrefix('/config/payments/', static function (WatchEvent $event): void {
    $verb = $event->type === WatchEventType::Delete ? 'deleted' : 'set';
    printf("%s %s\n", $event->kv->key, $verb);
});

// ... later, when you no longer care ...
$watch->close();

use Thesis\Etcd\WatchOptions;

$snapshot = $etcd->getPrefix('/config/payments/');
// ... apply $snapshot->kvs ...

$watch = $etcd->watchPrefix(
    '/config/payments/',
    static function (WatchEvent $event): void { /* apply each later change */ },
    new WatchOptions(startRevision: $snapshot->revision + 1),
);

use Thesis\Etcd\Txn;

$current = $etcd->get('/counter');

$result = $etcd->txn(
    Txn::compare(Txn\Compare::modRevision('/counter')->equals($current?->modRevision ?? 0))
        ->then(Txn\Op::put('/counter', (string) (((int) $current?->value) + 1)))
        ->otherwise(Txn\Op::get('/counter')), // someone beat us, read the fresh value
);

if (!$result->succeeded) {
    // retry with $result->responses[0], the current state
}

use Thesis\Etcd\PutOptions;
use Thesis\Time\TimeSpan;

$lease = $etcd->grantLease(TimeSpan::fromSeconds(15));

$etcd->put('/workers/worker-7', '10.0.0.5', new PutOptions(leaseId: $lease->id));

$keepAlive = $etcd->keepAlive($lease->id, $lease->ttl);

// ... worker runs, the registration stays alive as long as this process does ...

$keepAlive->close(); // stop renewing, the key expires on schedule
// or: $lease->release() to drop the lease and its keys right now

use Thesis\Etcd\Lock;
use Thesis\Time\TimeSpan;

$report = $etcd->withLock('/locks/report', ttl: TimeSpan::fromSeconds(10), fn: static function (Lock $lock): string {
    // Only one instance is ever inside this block at a time.
    return generateReport();
});

$lock = $etcd->lock('/locks/report'); // self-leased, blocks until held
try {
    // critical section
} finally {
    $lock->unlock(); // releases the lock and the lease
}

$lease = $etcd->grantLease(TimeSpan::fromSeconds(10));
$lock = $etcd->lock('/locks/report', $lease->id);
try {
    // critical section
} finally {
    $lock->unlock();
    $lease->release();
}

use function Amp\async;

$leadership = async(static fn() => $etcd->lock('/election/my-service'));

$lock = $leadership->await(); // now we are the leader
try {
    // ... lead until we stop ...
} finally {
    $lock->unlock();
}

$lock = $etcd->tryLock('/election/my-service');
if ($lock === null) {
    // Someone else leads this round, carry on as a follower.
    return;
}

try {
    // We are the leader.
} finally {
    $lock->unlock();
}