MSP-1388 [+] добавлена настройка для sentinel
This commit is contained in:
1 parent
2e2b903463
commit
5ed8f259e4
3 files changed
+34
-5
No files matched your search
@@ -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"))
|
||||
|
||||
+8
-1
@@ -25,11 +25,18 @@ func init() {
|
||||
type Remote struct {
|
||||
LogLevel log.Level `schemaGen:"logLevel" schema:"Уровень логирования"`
|
||||
Redis struct {
|
||||
Server string `schemaGen:"server" schema:"Адрес сервера redis"`
|
||||
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:"Префикс ключей для модуля"`
|
||||
DefaultTimeOut time.Duration `schemaGen:"defaultTimeOut" schema:"TTL по умолчанию, в секундах"`
|
||||
RedisSentinel *struct {
|
||||
Addresses []string `schema:"Адреса нод в кластере"`
|
||||
MasterName string `schema:"Имя мастера"`
|
||||
Username string `schema:"Имя пользователя в sentinel"`
|
||||
Password string `schema:"Пароль в sentinel"`
|
||||
} `schema:"Настройки sentinel,обязательна, если redis.server не указан"`
|
||||
} `schemaGen:"redis" schema:"Настройки redis"`
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ package rc
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"isp-lock-service/conf"
|
||||
@@ -41,11 +42,25 @@ func NewRC(cfg conf.Remote, l *log.Adapter) (*RC, error) {
|
||||
l: l,
|
||||
}
|
||||
|
||||
r.cli = redsync.New(goredis.NewPool(goredislib.NewClient(&goredislib.Options{
|
||||
cli := goredislib.NewClient(&goredislib.Options{
|
||||
Addr: cfg.Redis.Server,
|
||||
Username: cfg.Redis.UserName,
|
||||
Password: cfg.Redis.Password,
|
||||
DB: cfg.Redis.DB,
|
||||
})))
|
||||
})
|
||||
|
||||
if cfg.Redis.RedisSentinel != nil {
|
||||
cli = goredislib.NewFailoverClient(&goredislib.FailoverOptions{
|
||||
MasterName: cfg.Redis.RedisSentinel.MasterName,
|
||||
SentinelAddrs: cfg.Redis.RedisSentinel.Addresses,
|
||||
SentinelUsername: cfg.Redis.RedisSentinel.Username,
|
||||
SentinelPassword: cfg.Redis.RedisSentinel.Password,
|
||||
Password: cfg.Redis.Password,
|
||||
Username: cfg.Redis.UserName,
|
||||
})
|
||||
}
|
||||
|
||||
r.cli = redsync.New(goredis.NewPool(cli))
|
||||
|
||||
return &r, nil
|
||||
}
|
||||
@@ -70,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
|
||||
}
|
||||
|
||||
@@ -87,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
|
||||
}
|
||||
Reference in new issue
Block a user