Compare commits

..

5 Commits

Author SHA1 Message Date
34d1b513ba add GetSecDsn to get config description 2025-04-08 08:44:20 +08:00
bf3cd9bc27 fix typo 2024-11-13 22:59:38 +08:00
5a26f88da7 Merge branch 'master' 2024-11-13 12:29:14 +08:00
7c4e176775 add error log 2024-11-12 00:42:57 +08:00
bryanqiu
576807b94e dev 2024-06-28 18:25:26 +08:00
3 changed files with 41 additions and 2 deletions

View File

@ -1,11 +1,25 @@
package redis
import (
redigo "github.com/gomodule/redigo/redis"
"qoobing.com/gomod/redis/redis"
"qoobing.com/gomod/redis/sentinel"
)
type Config = sentinel.Config
type (
Conn = redigo.Conn
Pool = redigo.Pool
Config = sentinel.Config
)
var NewPool = redis.NewPool
var NewRedisPool = redis.NewPool
var NewSentinelPool = sentinel.NewPool
func NewPool(cfg Config) *redigo.Pool {
if cfg.Master != "" {
return NewRedisPool(cfg)
} else if cfg.MasterName != "" {
return NewSentinelPool(cfg)
}
panic("invalid config: Master & MasterName are both empty")
}

View File

@ -56,6 +56,10 @@ func NewPool(cfg Config) *redis.Pool {
logStdWriter = os.Stdout
logStdFlags = log.Ldate | log.Lmicroseconds | log.Lshortfile
logStdLogger = log.New(logStdWriter, logStdPrefix, logStdFlags)
logErrPrefix = "ERRO "
logErrWriter = os.Stderr
logErrFlags = log.Ldate | log.Lmicroseconds | log.Lshortfile
logErrLogger = log.New(logErrWriter, logErrPrefix, logErrFlags)
)
return &redis.Pool{
MaxIdle: *cfg.MaxIdle,
@ -65,6 +69,7 @@ func NewPool(cfg Config) *redis.Pool {
Dial: func() (redis.Conn, error) {
c, err := redis.Dial("tcp", masterAddr)
if err != nil {
logErrLogger.Output(2, fmt.Sprintf("connect [%s] error: %s", masterAddr, err))
return nil, err
}
@ -76,8 +81,10 @@ func NewPool(cfg Config) *redis.Pool {
}
if err != nil {
logErrLogger.Output(2, fmt.Sprintf("auth [%s] error: %s", masterAddr, err))
return nil, fmt.Errorf("redis master AUTH failed: <%s>", err.Error())
} else if okstr != "OK" {
logErrLogger.Output(2, fmt.Sprintf("auth [%s] not return ok but '%s'", masterAddr, okstr))
return nil, fmt.Errorf("redis master AUTH failed: <%s>", okstr)
}

View File

@ -29,6 +29,15 @@ type Config struct {
IdleTimeout *int `toml:"idle_timeout"` //空闲超时时间
}
func GetSecDsn(cfg Config) (dsn string) {
if cfg.Master != "" {
dsn += fmt.Sprintf("mode=redis address=%s", cfg.Master)
} else if cfg.MasterName != "" {
dsn += fmt.Sprintf("mode=sentinel address=%s mastername=%s", cfg.Sentinels, cfg.MasterName)
}
return dsn
}
type Sentinel struct {
// Addrs is a slice with known Sentinel addresses.
Addrs []string
@ -121,6 +130,10 @@ func NewPool(cfg Config) *redis.Pool {
logStdWriter = os.Stdout
logStdFlags = log.Ldate | log.Lmicroseconds | log.Lshortfile
logStdLogger = log.New(logStdWriter, logStdPrefix, logStdFlags)
logErrPrefix = "ERRO "
logErrWriter = os.Stderr
logErrFlags = log.Ldate | log.Lmicroseconds | log.Lshortfile
logErrLogger = log.New(logErrWriter, logErrPrefix, logErrFlags)
)
return &redis.Pool{
MaxIdle: *cfg.MaxIdle,
@ -130,11 +143,13 @@ func NewPool(cfg Config) *redis.Pool {
Dial: func() (redis.Conn, error) {
masterAddr, err := sntnl.MasterAddr()
if err != nil {
logErrLogger.Output(2, fmt.Sprintf("get master from sentinel error: %s", err))
return nil, err
}
c, err := redis.Dial("tcp", masterAddr)
if err != nil {
logErrLogger.Output(2, fmt.Sprintf("connect [%s] error: %s", masterAddr, err))
return nil, err
}
@ -146,8 +161,10 @@ func NewPool(cfg Config) *redis.Pool {
}
if err != nil {
logErrLogger.Output(2, fmt.Sprintf("auth [%s] error: %s", masterAddr, err))
return nil, fmt.Errorf("redis master AUTH failed: <%s>", err.Error())
} else if okstr != "OK" {
logErrLogger.Output(2, fmt.Sprintf("auth [%s] not return ok but '%s'", masterAddr, okstr))
return nil, fmt.Errorf("redis master AUTH failed: <%s>", okstr)
}
@ -156,6 +173,7 @@ func NewPool(cfg Config) *redis.Pool {
err = fmt.Errorf(
"master(%s) got by name '%s' is not redis master",
masterAddr, masterName)
logErrLogger.Output(2, err.Error())
return nil, err
}