定制 petrknap/critical-section 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

petrknap/critical-section

最新稳定版本:v2.1.1

Composer 安装命令:

composer require petrknap/critical-section

包简介

Critical section based on `symfony/lock`

README 文档

README

The CriticalSection is a simple object that handles the critical section overhead for you and lets you focus on the actual code.

use PetrKnap\CriticalSection\CriticalSection;
use Symfony\Component\Lock\NoLock;

$lock = new NoLock();

$criticalOutput = CriticalSection::withLock($lock)(fn () => 'This was critical.');

var_dump($criticalOutput);

You can wrap critical sections one inside the other thanks to the WrappingCriticalSection. This makes it easy to combine multiple locks, for example.

use PetrKnap\CriticalSection\CriticalSection;
use Symfony\Component\Lock\NoLock;

$lockA = new NoLock();
$lockB = new NoLock();

$criticalOutput = CriticalSection::withLock($lockA)->withLock($lockB)(fn () => 'This was critical.');

var_dump($criticalOutput);

You can also pass locks as array and leave the composition to the critical section.

use PetrKnap\CriticalSection\CriticalSection;
use Symfony\Component\Lock\NoLock;

$lockA = new NoLock();
$lockB = new NoLock();

$criticalOutput = CriticalSection::withLocks([$lockA, $lockB])(fn () => 'This was critical.');

var_dump($criticalOutput);

Do you need to accept only locked resources?

Use the LockedResource if you need to be sure that you are not processing resource outside it's critical section.

namespace PetrKnap\CriticalSection;

use Symfony\Component\Lock\NoLock;

/** @param Locked<Some\Resource> $resource */
function f(LockedResource $resource) {
    echo $resource->value;
}

$lock = new NoLock();
$resource = LockableResource::of(new Some\Resource('data'), $lock);
CriticalSection::withLock($lock)(fn () => f($resource));

Does your critical section work with database?

Use the doctrine/dbal and its transactional method.

/** @var PetrKnap\CriticalSection\CriticalSectionInterface $criticalSection */
/** @var Doctrine\DBAL\Connection $connection */
$criticalSection(
    fn () => $connection->transactional(
        fn () => 'This was critical on DB server.'
    )
);

Always use transactional inside critical section to prevent starvation.

Run composer require petrknap/critical-section to install it. You can support this project via donation. The project is licensed under the terms of the LGPL-3.0-or-later.

统计信息

  • 总下载量: 69
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: LGPL-3.0-or-later
  • 更新时间: 2023-11-11