mirror of
https://github.com/openimsdk/open-im-server.git
synced 2025-11-05 11:52:10 +08:00
feat: Release cron lock
Signed-off-by: imalasong <imalasong@qq.com>
This commit is contained in:
parent
42332cdd26
commit
9bc6a07cbf
@ -103,6 +103,15 @@ func netlock(rdb redis.UniversalClient, key string, ttl time.Duration) bool {
|
||||
return ok
|
||||
}
|
||||
|
||||
// netUnlock release redis lock.
|
||||
func netUnlock(rdb redis.UniversalClient, key string) error {
|
||||
_, err := rdb.Del(context.Background(), key).Result()
|
||||
if err != nil {
|
||||
return errs.Wrap(err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func cronWrapFunc(config *CronTaskConfig, rdb redis.UniversalClient, key string, fn func()) func() {
|
||||
enableCronLocker := config.CronTask.EnableCronLocker
|
||||
return func() {
|
||||
@ -114,6 +123,9 @@ func cronWrapFunc(config *CronTaskConfig, rdb redis.UniversalClient, key string,
|
||||
|
||||
// when acquire redis lock, call fn().
|
||||
if netlock(rdb, key, 5*time.Second) {
|
||||
defer func() {
|
||||
_ = netUnlock(rdb, key)
|
||||
}()
|
||||
fn()
|
||||
}
|
||||
}
|
||||
|
||||
@ -38,6 +38,12 @@ func TestDisLock(t *testing.T) {
|
||||
|
||||
// set different key
|
||||
assert.Equal(t, true, netlock(rdb, "cron-2", 2*time.Second))
|
||||
|
||||
// set key and release
|
||||
assert.Equal(t, true, netlock(rdb, "cron-3", 5*time.Second))
|
||||
assert.Equal(t, nil, netUnlock(rdb, "cron-3"))
|
||||
//retrieve set key
|
||||
assert.Equal(t, true, netlock(rdb, "cron-3", 5*time.Second))
|
||||
}
|
||||
|
||||
//func TestCronWrapFunc(t *testing.T) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user