yiisoft/mutex-file

Yii Mutex Library - File Driver

1.1.1 2022-09-04 19:25 UTC

README

68747470733a2f2f796969736f66742e6769746875622e696f2f646f63732f696d616765732f7969695f6c6f676f2e737667

Yii Mutex Library - File Driver


Latest Stable Version Total Downloads Build status Scrutinizer Code Quality Code Coverage Mutation testing badge static analysis type-coverage

This library provides a File mutex implementation for yiisoft/mutex.

Requirements

  • PHP 7.4 or higher.

Installation

The package could be installed with composer:

composer require yiisoft/mutex-file --prefer-dist

General usage

The package provides two classes implementing MutexInterface and MutexFactoryInterface from the yiisoft/mutex package:

$mutex = new \Yiisoft\Mutex\File\FileMutex(
    'mutex-name',
    '/path/to/directory/for/storing/mutex/files',
    0775, // Optional. The permission to be set for newly created mutex directory. Default is `0775`.
    0777, // Optional. The permission to be set for newly created mutex files. Default is `null`.
);

$mutexFactory = new \Yiisoft\Mutex\File\FileMutexFactory(
    '/path/to/directory/for/storing/mutex/files',
    0775, // Optional. The permission to be set for newly created mutex directory. Default is `0775`.
    0777, // Optional. The permission to be set for newly created mutex files. Default is `null`.
);

There are multiple ways you can use the package. You can execute a callback in a synchronized mode i.e. only a single instance of the callback is executed at the same time:

$synchronizer = new \Yiisoft\Mutex\Synchronizer($mutexFactory);

$newCount = $synchronizer->execute('critical', function () {
    return $counter->increase();
}, 10);

Another way is to manually open and close mutex:

$simpleMutex = new \Yiisoft\Mutex\SimpleMutex($mutexFactory);

if (!$simpleMutex->acquire('critical', 10)) {
    throw new \Yiisoft\Mutex\Exception\MutexLockedException('Unable to acquire the "critical" mutex.');
}

$newCount = $counter->increase();
$simpleMutex->release('critical');

It could be done on lower level:

$mutex = $mutexFactory->createAndAcquire('critical', 10);
$newCount = $counter->increase();
$mutex->release();

And if you want even more control, you can acquire mutex manually:

$mutex = $mutexFactory->create('critical');

if (!$mutex->acquire(10)) {
    throw new \Yiisoft\Mutex\Exception\MutexLockedException('Unable to acquire the "critical" mutex.');
}

$newCount = $counter->increase();
$mutex->release();

The FileMutex supports the "wait for a lock for a certain time" functionality. Using the withRetryDelay() method, you can override the number of milliseconds between each try until specified timeout times out:

$mutex = $mutex->withRetryDelay(100);

By default, it is 50 milliseconds - it means that we may try to acquire lock up to 20 times per second.

Testing

Unit testing

The package is tested with PHPUnit. To run tests:

./vendor/bin/phpunit

Mutation testing

The package tests are checked with Infection mutation framework with Infection Static Analysis Plugin. To run it:

./vendor/bin/roave-infection-static-analysis-plugin

Static analysis

The code is statically analyzed with Psalm. To run static analysis:

./vendor/bin/psalm

License

The Yii File mutex is free software. It is released under the terms of the BSD License. Please see LICENSE for more information.

Maintained by Yii Software.

Support the project

Open Collective

Follow updates

Official website Twitter Telegram Facebook Slack