承接 roadrunner-php/lock 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

roadrunner-php/lock

Composer 安装命令:

composer require roadrunner-php/lock

包简介

This package provides a PHP integration package for the RoadRunner Lock plugin, which allows for easy management of distributed locks in PHP applications. The plugin provides a fast, lightweight, and reliable way to acquire, release, and manage locks in a distributed environment, making it ideal for

README 文档

README

RoadRunner locks

PHP Version Require Latest Stable Version phpunit psalm Codecov Total Downloads

This package provides a PHP integration package for the RoadRunner Lock plugin, which allows for easy management of distributed locks in PHP applications. The plugin provides a fast, lightweight, and reliable way to acquire, release, and manage locks in a distributed environment, making it ideal for use in high-traffic web applications and microservices.

Requirements

Make sure that your server is configured with following PHP version and extensions:

  • PHP 8.1+

Installation

You can install the package via composer:

composer require roadrunner-php/lock

Usage

use RoadRunner\Lock\Lock;
use Spiral\Goridge\RPC\RPC;

require __DIR__ . '/vendor/autoload.php';

$lock = new Lock(RPC::create('tcp://127.0.0.1:6001'));

Acquire lock

Locks a resource so that it can be accessed by one process at a time.

By default the call is non-blocking: if the resource is already locked, it returns false almost immediately (the RoadRunner server caps the default waitTTL window at 1ms). Pass a positive waitTTL to block until the lock is released — the call then returns the lock id as soon as the lock becomes free, or false when the waitTTL timeout elapses.

$id = $lock->lock('pdf:create');

// Acquire lock with ttl - 10 seconds
$id = $lock->lock('pdf:create', ttl: 10);
// or
$id = $lock->lock('pdf:create', ttl: new \DateInterval('PT10S'));

// Acquire lock and wait 5 seconds until lock will be released
$id = $lock->lock('pdf:create', waitTTL: 5);
// or
$id = $lock->lock('pdf:create', waitTTL: new \DateInterval('PT5S'));

// Acquire lock with id - 14e1b600-9e97-11d8-9f32-f2801f1b9fd1
$id = $lock->lock('pdf:create', id: '14e1b600-9e97-11d8-9f32-f2801f1b9fd1');

Acquire read lock

Locks a resource for shared access, allowing multiple processes to access the resource simultaneously. When a resource is locked for shared access, other processes that attempt to lock the resource for exclusive access will fail to do so while any shared lock is held.

As with lock(), the waitTTL parameter is non-blocking by default (false is returned almost immediately, within the server's 1ms window); pass a positive waitTTL to block for up to that duration for the lock to become available.

$id = $lock->lockRead('pdf:create', ttl: 10);
// or
$id = $lock->lockRead('pdf:create', ttl: new \DateInterval('PT10S'));

// Acquire lock and wait 5 seconds until lock will be released
$id = $lock->lockRead('pdf:create', waitTTL: 5);
// or
$id = $lock->lockRead('pdf:create', waitTTL: new \DateInterval('PT5S'));

// Acquire lock with id - 14e1b600-9e97-11d8-9f32-f2801f1b9fd1
$id = $lock->lockRead('pdf:create', id: '14e1b600-9e97-11d8-9f32-f2801f1b9fd1');

Lock parameters

Both lock() and lockRead() accept the same arguments:

Parameter Type Default Description
resource non-empty-string Name of the resource to lock.
id non-empty-string|null null Lock owner id. When omitted a random UUID is generated. Keep it — the same id must be passed to release().
ttl int|float|DateInterval 0 (forever) Lock lifetime, in seconds. When it elapses the lock is released automatically; 0 means it never expires on its own.
waitTTL int|float|DateInterval 0 (~1ms) How long to wait for the lock to become free, in seconds. 0 is effectively non-blocking — the server caps it at 1ms, so false is returned almost immediately when the resource is already locked. A positive value blocks for up to that duration, then returns false on timeout.

Both methods return the lock id (non-empty-string) when the lock is acquired, or false when it is not (the resource stayed busy until the waitTTL window elapsed).

ttl and waitTTL are expressed in seconds (int or float), or as a DateInterval.

Release lock

Releases an exclusive lock or read lock on a resource that was previously acquired by a call to lock() or lockRead().

// Release lock after task is done.
$lock->release('pdf:create', $id);

// Force release lock
$lock->forceRelease('pdf:create');

Check lock

Checks if a resource is currently locked and returns information about the lock.

$status = $lock->exists('pdf:create');
if($status) {
    // Lock exists
} else {
    // Lock not exists
}

Update TTL

Updates the time-to-live (TTL) for the locked resource.

// Add 10 seconds to lock ttl
$lock->updateTTL('pdf:create', $id, 10);
// or
$lock->updateTTL('pdf:create', $id, new \DateInterval('PT10S'));

Testing

composer test

Credits

License

The MIT License (MIT). Please see License File for more information.

roadrunner-php/lock 适用场景与选型建议

roadrunner-php/lock 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.96M 次下载、GitHub Stars 达 3, 最近一次更新时间为 2023 年 03 月 16 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「lock」 「spiral」 「roadrunner-php」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 roadrunner-php/lock 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 roadrunner-php/lock 我们能提供哪些服务?
定制开发 / 二次开发

基于 roadrunner-php/lock 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 1.96M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 35
  • 依赖项目数: 6
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 3
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-03-16