diff --git a/etc/judge.yml b/etc/judge.yml index e86f7d39a642ebdf612e232938d99a0e42f41a19..1df87c5a7c26a60227c2dfbe13c3bc10c43a61e6 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 030c3a46f9d117a415195430e6bcf3b4223a0722..ba7c2238b42ad27fdd65823d11c0c6514be13a6c 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 8b167630d79706e4db16d711013d9d9b5c62ae1c..8957b8b2845ba7fd5308f5d84e2773caef9828fe 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 ad0e870ad4d5853976f4b8e1ae7f34cab060877e..1745c1f83763bce02b6fe122d3826e594795be2b 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 576e34552b0480178870fb5996b5f411c43a2c85..962a57f1ba55805b9e13304b7378fd71b4c23c35 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,