From: Marco Tallskog Date: Thu, 26 Sep 2019 12:51:29 +0000 (+0300) Subject: Support redis sentinel configuration X-Git-Tag: v0.4.0 X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F18%2F1018%2F1;p=ric-plt%2Fsdlgo.git Support redis sentinel configuration Based on environment variables set, sdl will select either to connect to standalone redis or sentinel redis. The underlying redis-go implementation takes care of how connection to master is handled and what happens if there is a switcover in redis. Change-Id: Ib413b61e6a7e26e8ad2bd7f83331554271a93c69 Signed-off-by: Marco Tallskog --- diff --git a/internal/sdlgoredis/sdlgoredis.go b/internal/sdlgoredis/sdlgoredis.go index 56ebfa8..f1c7e62 100644 --- a/internal/sdlgoredis/sdlgoredis.go +++ b/internal/sdlgoredis/sdlgoredis.go @@ -92,7 +92,7 @@ func checkIntResultAndError(result interface{}, err error) (bool, error) { if err != nil { return false, err } - if result == 1 { + if result.(int64) == int64(1) { return true, nil } return false, nil @@ -119,6 +119,7 @@ func CreateDB(client RedisClient, subscribe SubscribeFn) *DB { } func Create() *DB { + var client *redis.Client hostname := os.Getenv("DBAAS_SERVICE_HOST") if hostname == "" { hostname = "localhost" @@ -127,14 +128,26 @@ func Create() *DB { if port == "" { port = "6379" } - redisAddress := hostname + ":" + port - client := redis.NewClient(&redis.Options{ - Addr: redisAddress, - Password: "", // no password set - DB: 0, // use default DB - PoolSize: 20, - MaxRetries: 2, - }) + sentinelPort := os.Getenv("DBAAS_SERVICE_SENTINEL_PORT") + masterName := os.Getenv("DBAAS_MASTER_NAME") + if sentinelPort == "" { + redisAddress := hostname + ":" + port + client = redis.NewClient(&redis.Options{ + Addr: redisAddress, + Password: "", // no password set + DB: 0, // use default DB + PoolSize: 20, + MaxRetries: 2, + }) + } else { + sentinelAddress := hostname + ":" + sentinelPort + client = redis.NewFailoverClient(&redis.FailoverOptions{ + MasterName: masterName, + SentinelAddrs: []string{sentinelAddress}, + PoolSize: 20, + MaxRetries: 2, + }) + } db := CreateDB(client, subscribeNotifications) db.CheckCommands() return db