From 76fd7b38df55638c3901cb1c4fd5bb83ad18f3b9 Mon Sep 17 00:00:00 2001 From: wulorn Date: Wed, 25 Mar 2020 16:36:27 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0redis=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E9=A1=B9=EF=BC=8C=E5=8F=AF=E9=80=9A=E8=BF=87=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E9=85=8D=E7=BD=AE=E6=8C=87=E5=AE=9A=E7=9A=84?= =?UTF-8?q?redis=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- etc/judge.yml | 1 + etc/monapi.yml | 1 + src/modules/judge/backend/redi/redis.go | 11 +++++++++++ src/modules/monapi/config/yaml.go | 1 + src/modules/monapi/redisc/redis.go | 10 ++++++++++ 5 files changed, 24 insertions(+) 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,