diff --git a/redis/redis.go b/redis/redis.go new file mode 100644 index 0000000..8f660e8 --- /dev/null +++ b/redis/redis.go @@ -0,0 +1,98 @@ +package redis + +import ( + "fmt" + "log" + "os" + "time" + + "github.com/gomodule/redigo/redis" + "qoobing.com/gomod/redis/logging" + "qoobing.com/gomod/redis/sentinel" +) + +type Config = sentinel.Config + +func NewPool(cfg Config) *redis.Pool { + if cfg.Master == "" { + panic("config is invalid: Master is empty") + } else if cfg.Master != "" && cfg.MasterName != "" { + //panic("config is invalid: Master & MasterName must not set both") + } + var ( + masterAddr = cfg.Master + masterUsername = cfg.Username + masterPassword = cfg.Password + ) + 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 + } + + var ( + logPrefix = "redis" + logStdPrefix = "DBUG " + logStdWriter = os.Stdout + logStdFlags = log.Ldate | log.Lmicroseconds | log.Lshortfile + logStdLogger = log.New(logStdWriter, logStdPrefix, logStdFlags) + ) + return &redis.Pool{ + MaxIdle: *cfg.MaxIdle, + MaxActive: *cfg.MaxActive, + Wait: *cfg.Wait, + IdleTimeout: time.Duration(*cfg.IdleTimeout) * time.Second, + Dial: func() (redis.Conn, error) { + c, err := redis.Dial("tcp", masterAddr) + if err != nil { + return nil, err + } + + var okstr = "OK" + if masterPassword != "" && masterUsername != "" { + okstr, err = redis.String(c.Do("AUTH", masterUsername, masterPassword)) + } else if masterPassword != "" { + okstr, err = redis.String(c.Do("AUTH", masterPassword)) + } + + if err != nil { + return nil, fmt.Errorf("redis master AUTH failed: <%s>", err.Error()) + } else if okstr != "OK" { + return nil, fmt.Errorf("redis master AUTH failed: <%s>", okstr) + } + + //// if !TestRole(c, "master") { + //// c.Close() + //// err = fmt.Errorf( + //// "master(%s) got by name '%s' is not redis master", + //// masterAddr, masterName) + //// return nil, err + //// } + + if cfg.Debug { + c = logging.NewLoggingConn(c, logStdLogger, logPrefix) + } + return c, nil + }, + } +}