Download the PHP package happysir/distributed-lock without Composer
On this page you can find all versions of the php package happysir/distributed-lock. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Table of contents
Download happysir/distributed-lock
More information about happysir/distributed-lock
Files in happysir/distributed-lock
Download happysir/distributed-lock
More information about happysir/distributed-lock
Files in happysir/distributed-lock
Vendor happysir
Package distributed-lock
Short Description A distributed lock based on PHP and redis(swoft)
License Apache-2.0
Package distributed-lock
Short Description A distributed lock based on PHP and redis(swoft)
License Apache-2.0
Please rate this library. Is it a good library?
Informations about the package distributed-lock
[TOC]
锁
加锁是访问临界资源时对资源的保护机制,加锁的目的是对并发访问和并发请求进行保护,未获取到锁的请求将无权操作资源。
临界区域指的是一块对公共资源进行访问的代码,并非一种机制或是算法。一个程序、进程、线程可以拥有多个临界区域
安装
配置
详细参数:
pool
Redis 连接池
使用
注解方式
当前组件提供了注解的方式使用分布式锁,使用方式如下: `
@DistributedLock
注解说明
key
锁资源唯一标识key,同一时间片只能由一个线程持有type
锁类型DistributedLock::NON_BLOCKING
类型,在获取锁失败时会直接抛出DistributedLockException
异常DistributedLock::RETRY_TO_GET
类型,在获取锁失败时,会重新尝试获取锁(每0.5s重试一次,最多重试retries次),超过最大重试次数后会抛出DistributedLockException
异常
ttl
锁的有效时间(ttls后锁会自动过期)errcode
尝试持有锁时抛出的异常对应的codeerrmsg
尝试持有锁时抛出的异常对应的msgretries
重试次数(需要注意的是,重试次数过多,可能会造成系统负载上升,因此硬性限制最大重试次数为10次)
key 这里支持 symfony/expression-language 表达式,可以实现很多复杂的功能,详细文档。key 表达式内置 CLASS(类名) 和 METHOD(方法名称) 两个变量,方便开发者使用。详细使用参考Swoft服务限流章节
代码方式(显式使用)
DistributedLock::NON_BLOCKING
DistributedLock::RETRY_TO_GET
看门狗机制
存在这样一种场景,业务代码较为复杂,执行时长会超过我们申请锁的时间,这时候就可能有第二个线程成功申请锁,未达到我们想要的保护目的。
针对这样的场景,我们在组件中支持了锁自动续约机制-看门狗任务。
当我们成功获取锁后,程序会为我们创建一个协程,此协程的作用是续约我们的锁的过期时间。在锁被释放或者当前请求结束后,此协程会自动退出。
All versions of distributed-lock with dependencies
PHP Build Version
Package Version
The package happysir/distributed-lock contains the following files
Loading the files please wait ....