mirror of
https://github.com/openimsdk/open-im-server.git
synced 2026-11-21 00:23:41 +08:00
* 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 * feat: Support Streaming Messages in the Open-Source Server * chore: update openimsdk/tools dependency to v0.0.50-alpha.122 * chore: update openimsdk/protocol dependency to v0.0.73-alpha.20
61 lines
1.5 KiB
Go
61 lines
1.5 KiB
Go
package redis
|
|
|
|
import (
|
|
"context"
|
|
"encoding/hex"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/cache"
|
|
"github.com/openimsdk/tools/errs"
|
|
"github.com/openimsdk/tools/log"
|
|
"github.com/redis/go-redis/v9"
|
|
)
|
|
|
|
const lockPrefix = "LOCK:"
|
|
|
|
func NewLock(rdb redis.UniversalClient) cache.Lock {
|
|
return &redisLock{rdb: rdb}
|
|
}
|
|
|
|
type redisLock struct {
|
|
rdb redis.UniversalClient
|
|
}
|
|
|
|
func (x *redisLock) Lock(ctx context.Context, key string, timeout time.Duration) (string, error) {
|
|
uid, err := uuid.NewUUID()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if timeout < time.Second {
|
|
timeout = time.Minute * 2
|
|
}
|
|
value := hex.EncodeToString(uid[:])
|
|
key = lockPrefix + key
|
|
for {
|
|
ok, err := x.rdb.SetNX(ctx, key, value, timeout).Result()
|
|
if err != nil {
|
|
return "", errs.WrapMsg(err, "get redis lock", "key", key)
|
|
}
|
|
if ok {
|
|
return value, nil
|
|
}
|
|
timer := time.NewTimer(50 * time.Millisecond)
|
|
select {
|
|
case <-ctx.Done():
|
|
timer.Stop()
|
|
return "", context.Cause(ctx)
|
|
case <-timer.C:
|
|
}
|
|
}
|
|
}
|
|
|
|
func (x *redisLock) Unlock(ctx context.Context, key, value string) {
|
|
ctx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 10*time.Second)
|
|
defer cancel()
|
|
script := "\nlocal value = redis.call(\"GET\", KEYS[1])\nif value == ARGV[1] then\n return redis.call(\"DEL\", KEYS[1])\nend\nreturn 0"
|
|
if err := x.rdb.Eval(ctx, script, []string{lockPrefix + key}, value).Err(); err != nil {
|
|
log.ZWarn(ctx, "unlock redis lock", err, "key", key)
|
|
}
|
|
}
|