refactor(locker params): modify the locker lease time unit

1.modify the locker lease time unit
    2.modify internalLockLeaseTime params unit
    3.modify refreshTime params unit
    4.modify lua script
This commit is contained in:
douxu 2025-04-18 15:17:51 +08:00
parent 310f4c043c
commit 23110cbba9
3 changed files with 26 additions and 26 deletions

View File

@ -14,8 +14,8 @@ local lockKey = KEYS[2] .. ':' .. ARGV[2];
if (mode == false) then
redis.call('hset', KEYS[1], 'mode', 'read');
redis.call('hset', KEYS[1], lockKey, '1');
redis.call('hexpire', KEYS[1], ARGV[1], 'fields', '1', lockKey);
redis.call('expire', KEYS[1], ARGV[1]);
redis.call('hpexpire', KEYS[1], ARGV[1], 'fields', '1', lockKey);
redis.call('pexpire', KEYS[1], ARGV[1]);
return 1;
end;
@ -29,11 +29,11 @@ end;
if (mode == 'read') then
if (redis.call('exists', KEYS[1], ARGV[2]) == 1) then
redis.call('hincrby', KEYS[1], lockKey, '1');
local remainTime = redis.call('httl', KEYS[1], 'fields', '1', lockKey);
redis.call('hexpire', KEYS[1], math.max(tonumber(remainTime[1]), ARGV[1]), 'fields', '1', lockKey);
local remainTime = redis.call('hpttl', KEYS[1], 'fields', '1', lockKey);
redis.call('hpexpire', KEYS[1], math.max(tonumber(remainTime[1]), ARGV[1]), 'fields', '1', lockKey);
else
redis.call('hset', KEYS[1], lockKey, '1');
redis.call('hexpire', KEYS[1], ARGV[1], 'fields', '1', lockKey);
redis.call('hpexpire', KEYS[1], ARGV[1], 'fields', '1', lockKey);
end;
local cursor = 0;
local maxRemainTime = tonumber(ARGV[1]);
@ -45,13 +45,13 @@ if (mode == 'read') then
for i = 1, #fields,2 do
local field = fields[i];
local remainTime = redis.call('httl', KEYS[1], 'fields', '1', field);
local remainTime = redis.call('hpttl', KEYS[1], 'fields', '1', field);
maxRemainTime = math.max(tonumber(remainTime[1]), maxRemainTime);
end;
until cursor == 0;
local remainTime = redis.call('ttl', KEYS[1]);
redis.call('expire', KEYS[1], math.max(tonumber(remainTime),maxRemainTime));
local remainTime = redis.call('pttl', KEYS[1]);
redis.call('pexpire', KEYS[1], math.max(tonumber(remainTime),maxRemainTime));
return 1;
end;
`
@ -86,7 +86,7 @@ if ((mode == 'read') and (lockExists == 0)) then
end;
local counter = redis.call('hincrby', KEYS[1], lockKey, -1);
local delTTLs = redis.call('httl', KEYS[1], 'fields', '1', lockKey);
local delTTLs = redis.call('hpttl', KEYS[1], 'fields', '1', lockKey);
local delTTL = tonumber(delTTLs[1]);
if (counter == 0) then
redis.call('hdel', KEYS[1], lockKey);
@ -103,17 +103,17 @@ if (redis.call('hlen', KEYS[1]) > 1) then
for i = 1, #fields,2 do
local field = fields[i];
local remainTime = redis.call('httl', KEYS[1], 'fields', '1', field);
local remainTime = redis.call('hpttl', KEYS[1], 'fields', '1', field);
maxRemainTime = math.max(tonumber(remainTime[1]), maxRemainTime);
end;
until cursor == 0;
if (maxRemainTime > 0) then
if (delTTL > maxRemainTime) then
redis.call('expire', KEYS[1], maxRemainTime);
redis.call('pexpire', KEYS[1], maxRemainTime);
else
local remainTime = redis.call('ttl', KEYS[1]);
redis.call('expire', KEYS[1], math.max(tonumber(remainTime),maxRemainTime));
local remainTime = redis.call('pttl', KEYS[1]);
redis.call('pexpire', KEYS[1], math.max(tonumber(remainTime),maxRemainTime));
end;
end;
else
@ -149,8 +149,8 @@ if (mode == false) then
end;
redis.call('hset', KEYS[1], 'mode', 'write');
redis.call('hset', KEYS[1], lockKey, 1);
redis.call('hexpire', KEYS[1], ARGV[1], 'fields', '1', lockKey);
redis.call('expire', KEYS[1], ARGV[1]);
redis.call('hpexpire', KEYS[1], ARGV[1], 'fields', '1', lockKey);
redis.call('pexpire', KEYS[1], ARGV[1]);
redis.call('lpop', waitKey, '1');
return 1;
elseif (mode == 'read') then
@ -163,8 +163,8 @@ else
local lockExists = redis.call('hexists', KEYS[1], lockKey);
if (lockExists == 1) then
redis.call('hincrby', KEYS[1], lockKey, 1);
redis.call('hexpire', KEYS[1], ARGV[1], 'fields', '1', lockKey);
redis.call('expire', KEYS[1], ARGV[1]);
redis.call('hpexpire', KEYS[1], ARGV[1], 'fields', '1', lockKey);
redis.call('pexpire', KEYS[1], ARGV[1]);
return 1;
end;
-- 放到 list 中等待写锁释放后再次尝试加锁并且订阅写锁释放的消息
@ -233,7 +233,7 @@ local lockExists = redis.call('hexists', KEYS[1], lockKey);
local mode = redis.call('hget', KEYS[1], 'mode');
local maxRemainTime = tonumber(ARGV[1]);
if (lockExists == 1) then
redis.call('hexpire', KEYS[1], ARGV[1], 'fields', '1', lockKey);
redis.call('hpexpire', KEYS[1], ARGV[1], 'fields', '1', lockKey);
if (mode == 'read') then
local cursor = 0;
local pattern = KEYS[2] .. ':*';
@ -244,19 +244,19 @@ if (lockExists == 1) then
for i = 1, #fields,2 do
local field = fields[i];
local remainTime = redis.call('httl', KEYS[1], 'fields', '1', field);
local remainTime = redis.call('hpttl', KEYS[1], 'fields', '1', field);
maxRemainTime = math.max(tonumber(remainTime[1]), maxRemainTime);
end;
until cursor == 0;
if (maxRemainTime > 0) then
local remainTime = redis.call('ttl', KEYS[1]);
redis.call('expire', KEYS[1], math.max(tonumber(remainTime),maxRemainTime));
local remainTime = redis.call('pttl', KEYS[1]);
redis.call('pexpire', KEYS[1], math.max(tonumber(remainTime),maxRemainTime));
end;
elseif (mode == 'write') then
redis.call('expire', KEYS[1], ARGV[1]);
redis.call('pexpire', KEYS[1], ARGV[1]);
end;
-- return redis.call('ttl',KEYS[1]);
-- return redis.call('pttl',KEYS[1]);
return 1;
end;
return -8;

View File

@ -18,7 +18,7 @@ import (
)
const (
internalLockLeaseTime = uint64(30)
internalLockLeaseTime = uint64(30 * 1000)
unlockMessage = 0
)
@ -128,7 +128,7 @@ ARGV[2]:当前客户端的唯一标识token,用于区分不同的客户端
func (rl *redissionLocker) refreshLockTimeout(ctx context.Context) {
rl.Logger.Info("lock refresh by key and token", zap.String("token", rl.Token), zap.String("key", rl.Key))
lockTime := time.Duration(rl.lockLeaseTime/3) * time.Second
lockTime := time.Duration(rl.lockLeaseTime/3) * time.Millisecond
timer := time.NewTimer(lockTime)
defer timer.Stop()

View File

@ -112,7 +112,7 @@ func (rl *RedissionRWLocker) tryRLock(ctx context.Context) error {
func (rl *RedissionRWLocker) refreshLockTimeout(ctx context.Context) {
rl.Logger.Info("lock refresh by key and token", zap.String("token", rl.Token), zap.String("key", rl.Key))
lockTime := time.Duration(rl.lockLeaseTime/3) * time.Second
lockTime := time.Duration(rl.lockLeaseTime/3) * time.Millisecond
timer := time.NewTimer(lockTime)
defer timer.Stop()