feat: 领导者服务改为同步锁

This commit is contained in:
tbphp
2025-07-08 21:39:11 +08:00
parent fe3fd9c14a
commit a159debd77
4 changed files with 87 additions and 103 deletions

View File

@@ -10,19 +10,15 @@ import (
)
// NewStore creates a new store based on the application configuration.
// It prioritizes Redis if a DSN is provided, otherwise it falls back to an in-memory store.
func NewStore(cfg types.ConfigManager) (Store, error) {
redisDSN := cfg.GetRedisDSN()
// Prioritize Redis if configured
if redisDSN != "" {
logrus.Info("Redis DSN found, initializing Redis store...")
opts, err := redis.ParseURL(redisDSN)
if err != nil {
return nil, fmt.Errorf("failed to parse redis DSN: %w", err)
}
client := redis.NewClient(opts)
// Ping the server to ensure a connection is established.
if err := client.Ping(context.Background()).Err(); err != nil {
return nil, fmt.Errorf("failed to connect to redis: %w", err)
}
@@ -31,7 +27,6 @@ func NewStore(cfg types.ConfigManager) (Store, error) {
return NewRedisStore(client), nil
}
// Fallback to in-memory store
logrus.Info("Redis DSN not configured, falling back to in-memory store.")
return NewMemoryStore(), nil
}