Support redis sentinel configuration 18/1018/1 v0.4.0
authorMarco Tallskog <marco.tallskog@nokia.com>
Thu, 26 Sep 2019 12:51:29 +0000 (15:51 +0300)
committerMarco Tallskog <marco.tallskog@nokia.com>
Thu, 26 Sep 2019 12:54:37 +0000 (15:54 +0300)
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 <marco.tallskog@nokia.com>
internal/sdlgoredis/sdlgoredis.go

index 56ebfa8..f1c7e62 100644 (file)
@@ -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