open-im-server/internal/tools/cron/redis_locker.go
chao 10a577c310
feat: Supports launching multiple monolithic instances, load-balanced by nginx, requiring only MongoDB and Redis at a minimum (#3759)
* fix: performance issues with Kafka caused by encapsulating the MQ interface

* fix: admin token in standalone mode

* fix: full id version

* fix: resolve deadlock in cache eviction and improve GetBatch implementation

* refactor: replace LongConn with ClientConn interface and simplify message handling

* refactor: replace LongConn with ClientConn interface and simplify message handling

* fix: seq use $setOnInsert for min_seq in conversation update

* feat: add error code for handled friend requests and improve error handling in friend operations

* refactor(msg): update regex pattern for conversationID to include a trailing colon

* refactor: streamline cache initialization and enhance queue engine configuration

* refactor: streamline cache initialization and enhance queue engine configuration

* feat: add RegisterIP to API config and implement Redis server registration

* feat(redis): implement standalone gateway registration with Redis

* chore: update openimsdk/tools dependency to v0.0.50-alpha.121

* fix: change timer to ticker for improved periodic execution

* refactor: replace Kafka topic configuration with mqbuild constants for improved readability

* feat(redis): implement Redis-based locking mechanism for cron tasks
2026-07-03 10:28:06 +00:00

69 lines
1.6 KiB
Go

package cron
import (
"context"
"strings"
"time"
"github.com/google/uuid"
"github.com/redis/go-redis/v9"
"github.com/openimsdk/tools/log"
)
func NewRedisLocker(client redis.UniversalClient) *RedisLocker {
return &RedisLocker{
client: client,
script: redis.NewScript(strings.TrimSpace(`
if redis.call("get", KEYS[1]) == ARGV[1] then
return redis.call("del", KEYS[1])
else
return 0
end
`)),
}
}
type RedisLocker struct {
client redis.UniversalClient
script *redis.Script
}
func (e *RedisLocker) getKey(name string) string {
return "CRON_LOCKED:" + name
}
func (e *RedisLocker) lock(ctx context.Context, name string, owner string) (bool, error) {
ctx, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()
return e.client.SetNX(ctx, e.getKey(name), owner, lockLeaseTTL).Result()
}
func (e *RedisLocker) unlock(ctx context.Context, name string, owner string) error {
ctx, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()
return e.script.Run(ctx, e.client, []string{e.getKey(name)}, owner).Err()
}
func (e *RedisLocker) ExecuteWithLock(ctx context.Context, taskName string, task func()) {
owner := uuid.New().String()
ok, err := e.lock(ctx, taskName, owner)
if err != nil {
log.ZWarn(ctx, "cron lock get lock", err, "taskName", taskName)
return
}
log.ZDebug(ctx, "cron lock get lock", "taskName", taskName, "ok", ok, "owner", owner)
if !ok {
return
}
defer func() {
err := e.unlock(ctx, taskName, owner)
if err == nil {
log.ZDebug(ctx, "cron lock unlock", "taskName", taskName, "owner", owner)
} else {
log.ZWarn(ctx, "cron lock unlock", err, "taskName", taskName, "owner", owner)
}
}()
task()
}