Skip to content

Commit a79d5c1

Browse files
committed
[docs update]Redisson 看门狗自动续期图解
1 parent 1d35053 commit a79d5c1

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

docs/distributed-system/distributed-lock.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ else
6767
end
6868
```
6969

70-
![Redis 实现简易分布式锁](images/distributed-lock/distributed-lock-setnx.png)
70+
![Redis 实现简易分布式锁](./images/distributed-lock/distributed-lock-setnx.png)
7171

7272
这是一种最简易的 Redis 分布式锁实现,实现方式比较简单,性能也很高效。不过,这种方式实现分布式锁存在一些问题。就比如应用程序遇到一些问题比如释放锁的逻辑突然挂掉,可能会导致锁无法被释放,进而造成共享资源无法再被其他线程/进程访问。
7373

@@ -101,6 +101,8 @@ Redisson 是一个开源的 Java 语言 Redis 客户端,提供了很多开箱
101101

102102
Redisson 中的分布式锁自带自动续期机制,使用起来非常简单,原理也比较简单,其提供了一个专门用来监控和续期锁的 **Watch Dog( 看门狗)**,如果操作共享资源的线程还未执行完成的话,Watch Dog 会不断地延长锁的过期时间,进而保证锁不会因为超时而被释放。
103103

104+
![Redisson 看门狗自动续期](./images/distributed-lock/distributed-lock-redisson-renew-expiration.png)
105+
104106
看门狗名字的由来于 `getLockWatchdogTimeou()` 方法,这个方法返回的是看门狗给锁续期的过期时间,默认为 30 秒([redisson-3.17.6](https://github.com/redisson/redisson/releases/tag/redisson-3.17.6))。
105107

106108
```java
@@ -158,7 +160,7 @@ Watch Dog 通过调用 `renewExpirationAsync()` 方法实现锁的异步续期
158160
```java
159161
protected CompletionStage<Boolean> renewExpirationAsync(long threadId) {
160162
return evalWriteAsync(getRawName(), LongCodec.INSTANCE, RedisCommands.EVAL_BOOLEAN,
161-
// 如果指定的锁存在就续期,将其过期时间设置为 30s(默认)
163+
// 判断是否为持锁线程,如果是就执行续期操作,就锁的过期时间设置为 30s(默认)
162164
"if (redis.call('hexists', KEYS[1], ARGV[2]) == 1) then " +
163165
"redis.call('pexpire', KEYS[1], ARGV[1]); " +
164166
"return 1; " +
27.6 KB
Loading

0 commit comments

Comments
 (0)