From 167d507c4fb668eba99e388630a309c8cfc4d60d Mon Sep 17 00:00:00 2001 From: wuqian30624 Date: Wed, 28 Oct 2020 14:50:52 +0800 Subject: [PATCH] Avoid redisson fair lock blocking if there are lots of registered threads in queue but not used. Signed-off-by: wuqian30624 --- .../main/java/org/redisson/RedissonFairLock.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/redisson/src/main/java/org/redisson/RedissonFairLock.java b/redisson/src/main/java/org/redisson/RedissonFairLock.java index fa8077746..b3a0f2520 100644 --- a/redisson/src/main/java/org/redisson/RedissonFairLock.java +++ b/redisson/src/main/java/org/redisson/RedissonFairLock.java @@ -201,6 +201,22 @@ public class RedissonFairLock extends RedissonLock implements RLock { "return nil;" + "end;" + + + // check if the lock is not held, and other queues are not used + "while true do " + + "local firstThreadId = redis.call('lindex', KEYS[2], 0);" + + "if (firstThreadId == false) or (firstThreadId == ARGV[2]) then " + + "break;"+ + "end;" + + "local timeout = tonumber(redis.call('zscore', KEYS[3], firstThreadId));" + + "if (timeout > tonumber(ARGV[4])) and (redis.call('exists', KEYS[1]) == 0) then " + + "redis.call('lpop', KEYS[2]);" + + "redis.call('zrem', KEYS[3], firstThreadId);" + + "else" + + "break;" + + "end;" + + "end;" + + // the lock cannot be acquired // check if the thread is already in the queue "local timeout = redis.call('zscore', KEYS[3], ARGV[2]);" +