[CODE-717]: redis sentinel mode (#292)

jobatzil/rename
Hitesh Aringa 2023-08-11 16:28:53 +00:00 committed by Harness
parent 7b70f8c18d
commit ce90c63d01
3 changed files with 26 additions and 4 deletions

View File

@ -5,15 +5,31 @@
package server
import (
"strings"
"github.com/harness/gitness/types"
"github.com/go-redis/redis/v8"
)
// ProvideRedis provides a redis client based on the configuration.
// TODO: add support for sentinal / cluster
// TODO: add support for TLS
func ProvideRedis(config *types.Config) (redis.UniversalClient, error) {
if config.Redis.SentinelMode {
addrs := strings.Split(config.Redis.SentinelEndpoint, ",")
failoverOptions := &redis.FailoverOptions{
MasterName: config.Redis.SentinelMaster,
SentinelAddrs: addrs,
MaxRetries: config.Redis.MaxRetries,
MinIdleConns: config.Redis.MinIdleConnections,
}
if config.Redis.Password != "" {
failoverOptions.Password = config.Redis.Password
}
return redis.NewFailoverClient(failoverOptions), nil
}
options := &redis.Options{
Addr: config.Redis.Endpoint,
MaxRetries: config.Redis.MaxRetries,

View File

@ -34,6 +34,9 @@ type Config struct {
MaxRetries int `envconfig:"GITRPC_REDIS_MAX_RETRIES" default:"3"`
MinIdleConnections int `envconfig:"GITRPC_REDIS_MIN_IDLE_CONNECTIONS" default:"0"`
Password string `envconfig:"GITRPC_REDIS_PASSWORD"`
SentinelMode bool `envconfig:"GITRPC_REDIS_USE_SENTINEL" default:"false"`
SentinelMaster string `envconfig:"GITRPC_REDIS_SENTINEL_MASTER"`
SentinelEndpoint string `envconfig:"GITRPC_REDIS_SENTINEL_ENDPOINT"`
}
}

View File

@ -128,10 +128,13 @@ type Config struct {
}
Redis struct {
Endpoint string `envconfig:"GITNESS_REDIS_ENDPOINT" default:"localhost:6379"`
MaxRetries int `envconfig:"GITNESS_REDIS_MAX_RETRIES" default:"3"`
MinIdleConnections int `envconfig:"GITNESS_REDIS_MIN_IDLE_CONNECTIONS" default:"0"`
Endpoint string `envconfig:"GITNESS_REDIS_ENDPOINT" default:"localhost:6379"`
MaxRetries int `envconfig:"GITNESS_REDIS_MAX_RETRIES" default:"3"`
MinIdleConnections int `envconfig:"GITNESS_REDIS_MIN_IDLE_CONNECTIONS" default:"0"`
Password string `envconfig:"GITNESS_REDIS_PASSWORD"`
SentinelMode bool `envconfig:"GITNESS_REDIS_USE_SENTINEL" default:"false"`
SentinelMaster string `envconfig:"GITNESS_REDIS_SENTINEL_MASTER"`
SentinelEndpoint string `envconfig:"GITNESS_REDIS_SENTINEL_ENDPOINT"`
}
Lock struct {