MSP-1388 [+] добавлена настройка для sentinel

This commit is contained in:
Leonid Nikolaev committed 2023-01-30 10:47:04 +03:00
1 parent 265dbc6250
commit 7a03f4e49b
3 files changed
+13 -2

No files matched your search

-1
View File
@@ -60,7 +60,6 @@ func (a *Assembly) ReceiveConfig(ctx context.Context, remoteConfig []byte) error
a.logger.SetLevel(newCfg.LogLevel)
// TODO: добавить сентиль
a.redisCli, err = rc.NewRC(newCfg, a.logger)
if err != nil {
a.logger.Fatal(ctx, errors.WithMessage(err, "error on connect to redis"))
+1
View File
@@ -26,6 +26,7 @@ type Remote struct {
LogLevel log.Level `schemaGen:"logLevel" schema:"Уровень логирования"`
Redis struct {
Server string `schemaGen:"server" schema:"Адрес сервера redis, обязателен, если sentinel не указан"`
UserName string `schemaGen:"userName" schema:"Имя пользователя в redis"`
Password string `schemaGen:"password" schema:"Пароль для redis"`
DB int `schemaGen:"db" schema:"номер БД в redis"`
Prefix string `schemaGen:"prefix" schema:"Префикс ключей для модуля"`
+12 -1
View File
@@ -3,6 +3,7 @@ package rc
import (
"context"
"errors"
"fmt"
"time"
"isp-lock-service/conf"
@@ -43,6 +44,7 @@ func NewRC(cfg conf.Remote, l *log.Adapter) (*RC, error) {
cli := goredislib.NewClient(&goredislib.Options{
Addr: cfg.Redis.Server,
Username: cfg.Redis.UserName,
Password: cfg.Redis.Password,
DB: cfg.Redis.DB,
})
@@ -54,6 +56,7 @@ func NewRC(cfg conf.Remote, l *log.Adapter) (*RC, error) {
SentinelUsername: cfg.Redis.RedisSentinel.Username,
SentinelPassword: cfg.Redis.RedisSentinel.Password,
Password: cfg.Redis.Password,
Username: cfg.Redis.UserName,
})
}
@@ -82,6 +85,7 @@ func (rc *RC) Lock(key string, ttl time.Duration) (string, error) {
rc.l.Debug(context.Background(), "пробуем залочить "+key+" на "+ttl.String())
mtx := rc.cli.NewMutex(key, redsync.WithExpiry(ttl))
if err := mtx.Lock(); err != nil {
rc.l.Debug(context.Background(), fmt.Sprintf("err=%#v", err))
return "", err
}
@@ -99,5 +103,12 @@ func (rc *RC) UnLock(key, lockKey string) (bool, error) {
rc.l.Debug(context.Background(), "пробуем разлочить "+key+"+"+lockKey)
return rc.cli.NewMutex(key, redsync.WithValue(lockKey)).Unlock()
ok, err := rc.cli.NewMutex(key, redsync.WithValue(lockKey)).Unlock()
rc.l.Debug(context.Background(), fmt.Sprint("ok=", ok))
if err != nil {
rc.l.Debug(context.Background(), fmt.Sprintf("err=%#v", err))
}
return ok, err
}