open-im-server/pkg/common/storage/cache/redis/standalone_gateway.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

55 lines
1.6 KiB
Go

package redis
import (
"context"
"strconv"
"time"
"github.com/redis/go-redis/v9"
"github.com/openimsdk/tools/errs"
)
const standaloneGatewayHashKey = "STANDALONE_GATEWAY_REGISTRY"
type StandaloneGatewayRedis struct {
rdb redis.UniversalClient
validTime time.Duration
}
func NewStandaloneGatewayRedis(rdb redis.UniversalClient, validTime time.Duration) *StandaloneGatewayRedis {
return &StandaloneGatewayRedis{rdb: rdb, validTime: validTime}
}
func (s *StandaloneGatewayRedis) RegisterGateway(ctx context.Context, addr string) error {
pipe := s.rdb.Pipeline()
pipe.HSet(ctx, standaloneGatewayHashKey, addr, strconv.FormatInt(time.Now().UnixMilli(), 10))
pipe.Expire(ctx, standaloneGatewayHashKey, s.validTime*2)
_, err := pipe.Exec(ctx)
return errs.Wrap(err)
}
func (s *StandaloneGatewayRedis) UnregisterGateway(ctx context.Context, addr string) error {
return errs.Wrap(s.rdb.HDel(ctx, standaloneGatewayHashKey, addr).Err())
}
func (s *StandaloneGatewayRedis) GetGatewayAddrs(ctx context.Context) ([]string, error) {
gateways, err := s.rdb.HGetAll(ctx, standaloneGatewayHashKey).Result()
if err != nil {
return nil, errs.Wrap(err)
}
now := time.Now()
addrs := make([]string, 0, len(gateways))
for addr, registeredAt := range gateways {
registeredAtMs, err := strconv.ParseInt(registeredAt, 10, 64)
if err != nil {
return nil, errs.WrapMsg(err, "redis gateway register time is not int64", "addr", addr, "value", registeredAt)
}
if now.Sub(time.UnixMilli(registeredAtMs)) <= s.validTime {
addrs = append(addrs, addr)
}
}
return addrs, nil
}