leocavalcante/swoole-mutex

Mutual exclusion abstractions for PHP's Swoole concurrency run-time.

dev-master 2020-04-11 20:05 UTC

This package is auto-updated.

Last update: 2024-11-13 00:16:05 UTC


README

Actions Status

🚦 Mutual exclusion abstractions for PHP's Swoole concurrency run-time.

With great power comes great responsibility — Uncle Ben (I guess)

Swoole awesomeness enables concurrency across multiple processes and cores, but sharing state/memory between them isn't a straightforward achievement. Swoole\Table comes to help, but maybe we could have an even better API for that task.

This package is a Facade for Swoole\Table providing common APIs for mutual exclusion patterns.

We all hail Edsger W. Dijkstra (and Swoole team for their amazing work).

Install

composer require leocavalcante/swoole-mutex

Example

Counting HTTP requests across >1 workers.

use Mutex\Atomic\Integer;
use Swoole\Http\{Request, Response, Server};

$counter = new Integer();
$server = new Server('127.0.0.1', 8000);

$server->on('request', function (Request $req, Response $res) use ($counter): void {
    if ($req->server['request_uri'] !== '/favicon.ico') {
        $counter->inc();
    }

    $res->end('You are number: ' . $counter->value());
});

$server->set(['worker_num' => 4]);
$server->start();

© 2k2O