diff --git a/etc/judge.yml b/etc/judge.yml index e86f7d39..1df87c5a 100644 --- a/etc/judge.yml +++ b/etc/judge.yml @@ -6,6 +6,7 @@ query: redis: addrs: - 127.0.0.1:6379 + db: 0 pass: "" # timeout: # conn: 500 diff --git a/etc/monapi.yml b/etc/monapi.yml index 030c3a46..ba7c2238 100644 --- a/etc/monapi.yml +++ b/etc/monapi.yml @@ -52,6 +52,7 @@ link: # for alarm event and message queue redis: addr: "127.0.0.1:6379" + db: 0 pass: "" # in ms # timeout: diff --git a/src/modules/judge/backend/redi/redis.go b/src/modules/judge/backend/redi/redis.go index 8b167630..8957b8b2 100644 --- a/src/modules/judge/backend/redi/redis.go +++ b/src/modules/judge/backend/redi/redis.go @@ -15,6 +15,7 @@ var Config RedisSection type RedisSection struct { Addrs []string `yaml:"addrs"` Pass string `yaml:"pass"` + DB int `yaml:"db"` Idle int `yaml:"idle"` Timeout TimeoutSection `yaml:"timeout"` Prefix string `yaml:"prefix"` @@ -31,6 +32,7 @@ func Init(cfg RedisSection) { addrs := cfg.Addrs pass := cfg.Pass + db := cfg.DB maxIdle := cfg.Idle idleTimeout := 240 * time.Second @@ -59,6 +61,15 @@ func Init(cfg RedisSection) { } } + if db != 0 { + if _, err := c.Do("SELECT", db); err != nil { + c.Close() + logger.Error("redis select db fail, db: ", db) + stats.Counter.Set("redis.conn.failed", 1) + return nil, err + } + } + return c, err }, TestOnBorrow: PingRedis, diff --git a/src/modules/monapi/config/yaml.go b/src/modules/monapi/config/yaml.go index ad0e870a..1745c1f8 100644 --- a/src/modules/monapi/config/yaml.go +++ b/src/modules/monapi/config/yaml.go @@ -43,6 +43,7 @@ type cleanerSection struct { type redisSection struct { Addr string `yaml:"addr"` Pass string `yaml:"pass"` + DB int `yaml:"db"` Idle int `yaml:"idle"` Timeout timeoutSection `yaml:"timeout"` } diff --git a/src/modules/monapi/redisc/redis.go b/src/modules/monapi/redisc/redis.go index 576e3455..962a57f1 100644 --- a/src/modules/monapi/redisc/redis.go +++ b/src/modules/monapi/redisc/redis.go @@ -17,6 +17,7 @@ func InitRedis() { addr := cfg.Redis.Addr pass := cfg.Redis.Pass + db := cfg.Redis.DB maxIdle := cfg.Redis.Idle idleTimeout := 240 * time.Second @@ -44,6 +45,15 @@ func InitRedis() { } } + if db != 0 { + if _, err := c.Do("SELECT", db); err != nil { + c.Close() + logger.Error("redis select db fail, db: ", db) + stats.Counter.Set("redis.conn.failed", 1) + return nil, err + } + } + return c, err }, TestOnBorrow: PingRedis,