当前位置:网站首页>Redismission source code analysis
Redismission source code analysis
2022-07-08 01:51:00 【Know what you know】
Redisson There are two ways to lock ,tryLock and lock, The difference in use is tryLock You can set the expiration time of the lock leaseTime And waiting time waitTime, The logic of core processing is almost the same
public boolean tryLock(long waitTime, long leaseTime, TimeUnit unit) throws InterruptedException {
long time = unit.toMillis(waitTime);
long current = System.currentTimeMillis();
long threadId = Thread.currentThread().getId();
// Attempt to acquire lock , If you don't get the lock , The remaining timeout of the lock is returned
Long ttl = tryAcquire(waitTime, leaseTime, unit, threadId);
// lock acquired
// ttl by null, It means we can get the lock , return true
if (ttl == null) {
return true;
}
// If waitTime It's over time , Just go back to false, Failed to apply for lock
time -= System.currentTimeMillis() - current;
if (time <= 0) {
acquireFailed(waitTime, unit, threadId);
return false;
}
current = System.currentTimeMillis();
/**
Subscribe to lock release events , And pass await Method blocks waiting for the lock to be released , It effectively solves the problem of waste of resources in invalid lock application :
Based on the amount of information , When the lock is occupied by other resources , The current thread passes Redis Of channel Release event of subscription lock
Once the lock is released, a message will be sent to inform the waiting thread to compete .
*/
RFuture<RedissonLockEntry> subscribeFuture = subscribe(threadId);
if (!subscribeFuture.await(time, TimeUnit.MILLISECONDS)) {
if (!subscribeFuture.cancel(false)) {
subscribeFuture.onComplete((res, e) -> {
if (e == null) {
unsubscribe(subscribeFuture, threadId);
}
});
}
acquireFailed(waitTime, unit, threadId);
return false;
}
try {
// If the time taken to acquire the lock exceeds the maximum waiting time , Locking failed
time -= System.currentTimeMillis() - current;
if (time <= 0) {
acquireFailed(waitTime, unit, threadId);
return false;
}
// Cycle to acquire the lock within the maximum waiting time
while (true) {
long currentTime = System.currentTimeMillis();
ttl = tryAcquire(waitTime, leaseTime, unit, threadId);
// lock acquired
if (ttl == null) {
return true;
}
time -= System.currentTimeMillis() - currentTime;
if (time <= 0) {
acquireFailed(waitTime, unit, threadId);
return false;
}
// waiting for message
currentTime = System.currentTimeMillis();
// waiting for message, Wait for unlock message
if (ttl >= 0 && ttl < time) {
subscribeFuture.getNow().getLatch().tryAcquire(ttl, TimeUnit.MILLISECONDS);
} else {
subscribeFuture.getNow().getLatch().tryAcquire(time, TimeUnit.MILLISECONDS);
}
time -= System.currentTimeMillis() - currentTime;
if (time <= 0) {
acquireFailed(waitTime, unit, threadId);
return false;
}
}
} finally {
// Unsubscribe from messages
unsubscribe(subscribeFuture, threadId);
}
// return get(tryLockAsync(waitTime, leaseTime, unit));
}
Here's the thing to watch out for ,RedissonLock It's also not solved When the node hangs up , There is a risk of losing locks . And the reality is that there are some scenes that can't be tolerated , therefore Redisson Provides the implementation of redlock Algorithm RedissonRedLock,RedissonRedLock It really solves the problem of single point failure , The price is extra for RedissonRedLock build Redis Environmental Science .
therefore , If the business scenario can tolerate this small probability of error , It is recommended to use RedissonLock, If you can't stand , It is recommended to use RedissonRedLock.
边栏推荐
- 软件测试笔试题你会吗?
- The body has a mysterious margin of 8px
- I don't know. The real interest rate of Huabai installment is so high
- 腾讯游戏客户端开发面试 (Unity + Cocos) 双重轰炸 社招6轮面试
- Codeforces Round #643 (Div. 2)——B. Young Explorers
- Capability contribution three solutions of gbase were selected into the "financial information innovation ecological laboratory - financial information innovation solutions (the first batch)"
- How to make enterprise recruitment QR code?
- How to make the conductive slip ring signal better
- SQLite3 data storage location created by Android
- 快手小程序担保支付php源码封装
猜你喜欢
快速熟知XML解析
Qt - - Packaging Programs - - Don't install Qt - can run directly
保姆级教程:Azkaban执行jar包(带测试样例及结果)
Leetcode exercise - Sword finger offer 36 Binary search tree and bidirectional linked list
Why does the updated DNS record not take effect?
进程和线程的退出
Apache multiple component vulnerability disclosure (cve-2022-32533/cve-2022-33980/cve-2021-37839)
ClickHouse原理解析与应用实践》读书笔记(8)
从cmath文件看名字是怎样被添加到命名空间std中的
C language - modularization -clion (static library, dynamic library) use
随机推荐
adb工具介绍
How to make enterprise recruitment QR code?
ArrayList源码深度剖析,从最基本的扩容原理,到魔幻的迭代器和fast-fail机制,你想要的这都有!!!
common commands
cv2-drawline
COMSOL - Construction of micro resistance beam model - final temperature distribution and deformation - establishment of geometric model
Capability contribution three solutions of gbase were selected into the "financial information innovation ecological laboratory - financial information innovation solutions (the first batch)"
日志特征选择汇总(基于天池比赛)
用户之声 | 对于GBase 8a数据库学习的感悟
滑环在直驱电机转子的应用领域
从cmath文件看名字是怎样被添加到命名空间std中的
Codeforces Round #643 (Div. 2)——B. Young Explorers
nacos-微服务网关Gateway组件 +Swagger2接口生成
Chapter 7 behavior level modeling
软件测试笔试题你会吗?
如何让导电滑环信号更好
Usage of xcolor color in latex
Redux使用
发现值守设备被攻击后分析思路
碳刷滑环在发电机中的作用