add default
This commit is contained in:
parent
adb92a63c3
commit
2666959122
@ -14,60 +14,16 @@ import (
|
||||
"qoobing.com/gomod/redis/logging"
|
||||
)
|
||||
|
||||
// Sentinel provides a way to add high availability (HA) to Redis Pool using
|
||||
// preconfigured addresses of Sentinel servers and name of master which Sentinels
|
||||
// monitor. It works with Redis >= 2.8.12 (mostly because of ROLE command that
|
||||
// was introduced in that version, it's possible though to support old versions
|
||||
// using INFO command).
|
||||
//
|
||||
// Example of the simplest usage to contact master "mymaster":
|
||||
//
|
||||
// func newSentinelPool() *redis.Pool {
|
||||
// sntnl := &sentinel.Sentinel{
|
||||
// Addrs: []string{":26379", ":26380", ":26381"},
|
||||
// MasterName: "mymaster",
|
||||
// Dial: func(addr string) (redis.Conn, error) {
|
||||
// timeout := 500 * time.Millisecond
|
||||
// c, err := redis.DialTimeout("tcp", addr, timeout, timeout, timeout)
|
||||
// if err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
// return c, nil
|
||||
// },
|
||||
// }
|
||||
// return &redis.Pool{
|
||||
// MaxIdle: 3,
|
||||
// MaxActive: 64,
|
||||
// Wait: true,
|
||||
// IdleTimeout: 240 * time.Second,
|
||||
// Dial: func() (redis.Conn, error) {
|
||||
// masterAddr, err := sntnl.MasterAddr()
|
||||
// if err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
// c, err := redis.Dial("tcp", masterAddr)
|
||||
// if err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
// if !sentinel.TestRole(c, "master") {
|
||||
// c.Close()
|
||||
// return nil, fmt.Errorf("%s is not redis master", masterAddr)
|
||||
// }
|
||||
// return c, nil
|
||||
// },
|
||||
// }
|
||||
// }
|
||||
|
||||
type Config struct {
|
||||
Debug bool `toml:"debug"` //调试开关(会在日志打印REDIS语句)
|
||||
Username string `toml:"username"` //REDIS连接用户名
|
||||
Password string `toml:"password"` //REDIS连接用户密码
|
||||
MasterName string `toml:"mastername"` //REDIS主名称(一个哨兵集群可管理多个REDIS主从结构)
|
||||
Sentinels string `toml:"sentinels"` //哨兵节点列表,逗号分隔,一般配置三个
|
||||
Wait bool `toml:"wait"`
|
||||
MaxIdle int `toml:"max_idle"`
|
||||
MaxActive int `toml:"max_active"`
|
||||
IdleTimeout int `toml:"idle_timeout"`
|
||||
Wait *bool `toml:"wait"`
|
||||
MaxIdle *int `toml:"max_idle"`
|
||||
MaxActive *int `toml:"max_active"`
|
||||
IdleTimeout *int `toml:"idle_timeout"`
|
||||
}
|
||||
|
||||
type Sentinel struct {
|
||||
@ -111,12 +67,37 @@ func NewPool(cfg Config) *redis.Pool {
|
||||
},
|
||||
}
|
||||
)
|
||||
if cfg.Wait == nil {
|
||||
cfg.Wait = new(bool)
|
||||
*cfg.Wait = true
|
||||
}
|
||||
|
||||
if cfg.IdleTimeout == nil {
|
||||
cfg.IdleTimeout = new(int)
|
||||
*cfg.IdleTimeout = 300
|
||||
} else if *cfg.IdleTimeout <= 0 {
|
||||
*cfg.IdleTimeout = 86400 * 365 * 10
|
||||
}
|
||||
|
||||
if cfg.MaxIdle == nil {
|
||||
cfg.MaxIdle = new(int)
|
||||
*cfg.MaxIdle = 100
|
||||
} else if *cfg.MaxIdle < 0 {
|
||||
*cfg.MaxIdle = 100
|
||||
}
|
||||
|
||||
if cfg.MaxActive == nil {
|
||||
cfg.MaxActive = new(int)
|
||||
*cfg.MaxActive = 100
|
||||
} else if *cfg.MaxActive < 0 {
|
||||
*cfg.MaxActive = 100
|
||||
}
|
||||
|
||||
return &redis.Pool{
|
||||
MaxIdle: cfg.MaxIdle,
|
||||
MaxActive: cfg.MaxActive,
|
||||
Wait: cfg.Wait,
|
||||
IdleTimeout: time.Duration(cfg.IdleTimeout) * time.Second,
|
||||
MaxIdle: *cfg.MaxIdle,
|
||||
MaxActive: *cfg.MaxActive,
|
||||
Wait: *cfg.Wait,
|
||||
IdleTimeout: time.Duration(*cfg.IdleTimeout) * time.Second,
|
||||
Dial: func() (redis.Conn, error) {
|
||||
masterAddr, err := sntnl.MasterAddr()
|
||||
if err != nil {
|
||||
|
Loading…
Reference in New Issue
Block a user