From bc7422a50cee32e6db1eaca8cb06ec01606e050f Mon Sep 17 00:00:00 2001 From: William Petit Date: Thu, 15 Feb 2024 15:17:57 +0100 Subject: [PATCH] feat: add configurable redis timeouts --- internal/config/redis.go | 16 ++++++++++++---- internal/jwk/jwk.go | 5 ++--- internal/setup/proxy_repository.go | 6 +----- internal/setup/queue_layer.go | 7 +------ internal/setup/redis.go | 20 ++++++++++++++++++++ misc/packaging/common/config.yml | 3 +++ 6 files changed, 39 insertions(+), 18 deletions(-) create mode 100644 internal/setup/redis.go diff --git a/internal/config/redis.go b/internal/config/redis.go index 61faefd..9fbaddc 100644 --- a/internal/config/redis.go +++ b/internal/config/redis.go @@ -1,5 +1,7 @@ package config +import "time" + const ( RedisModeSimple = "simple" RedisModeSentinel = "sentinel" @@ -7,13 +9,19 @@ const ( ) type RedisConfig struct { - Adresses InterpolatedStringSlice `yaml:"addresses"` - Master InterpolatedString `yaml:"master"` + Adresses InterpolatedStringSlice `yaml:"addresses"` + Master InterpolatedString `yaml:"master"` + ReadTimeout InterpolatedDuration `yaml:"readTimeout"` + WriteTimeout InterpolatedDuration `yaml:"writeTimeout"` + DialTimeout InterpolatedDuration `yaml:"dialTimeout"` } func NewDefaultRedisConfig() RedisConfig { return RedisConfig{ - Adresses: InterpolatedStringSlice{"localhost:6379"}, - Master: "", + Adresses: InterpolatedStringSlice{"localhost:6379"}, + Master: "", + ReadTimeout: InterpolatedDuration(30 * time.Second), + WriteTimeout: InterpolatedDuration(30 * time.Second), + DialTimeout: InterpolatedDuration(30 * time.Second), } } diff --git a/internal/jwk/jwk.go b/internal/jwk/jwk.go index afa0bdf..727b2dd 100644 --- a/internal/jwk/jwk.go +++ b/internal/jwk/jwk.go @@ -4,7 +4,6 @@ import ( "crypto/rand" "crypto/rsa" "encoding/json" - "io/ioutil" "os" "github.com/btcsuite/btcd/btcutil/base58" @@ -56,7 +55,7 @@ func PublicKeySet(keys ...jwk.Key) (jwk.Set, error) { } func LoadOrGenerate(path string, size int) (jwk.Key, error) { - data, err := ioutil.ReadFile(path) + data, err := os.ReadFile(path) if err != nil && !errors.Is(err, os.ErrNotExist) { return nil, errors.WithStack(err) } @@ -72,7 +71,7 @@ func LoadOrGenerate(path string, size int) (jwk.Key, error) { return nil, errors.WithStack(err) } - if err := ioutil.WriteFile(path, data, 0o640); err != nil { + if err := os.WriteFile(path, data, 0o640); err != nil { return nil, errors.WithStack(err) } } diff --git a/internal/setup/proxy_repository.go b/internal/setup/proxy_repository.go index 3ebc6f5..67b777b 100644 --- a/internal/setup/proxy_repository.go +++ b/internal/setup/proxy_repository.go @@ -10,11 +10,7 @@ import ( ) func NewProxyRepository(ctx context.Context, conf config.RedisConfig) (store.ProxyRepository, error) { - rdb := redis.NewUniversalClient(&redis.UniversalOptions{ - Addrs: conf.Adresses, - MasterName: string(conf.Master), - }) - + rdb := newRedisClient(conf) return redisStore.NewProxyRepository(rdb), nil } diff --git a/internal/setup/queue_layer.go b/internal/setup/queue_layer.go index 15ed188..8b97f70 100644 --- a/internal/setup/queue_layer.go +++ b/internal/setup/queue_layer.go @@ -8,7 +8,6 @@ import ( "forge.cadoles.com/cadoles/bouncer/internal/proxy/director/layer/queue" queueRedis "forge.cadoles.com/cadoles/bouncer/internal/proxy/director/layer/queue/redis" "github.com/pkg/errors" - "github.com/redis/go-redis/v9" ) func init() { @@ -36,10 +35,6 @@ func setupQueueLayer(conf *config.Config) (director.Layer, error) { } func newQueueAdapter(redisConf config.RedisConfig) (queue.Adapter, error) { - rdb := redis.NewUniversalClient(&redis.UniversalOptions{ - Addrs: redisConf.Adresses, - MasterName: string(redisConf.Master), - }) - + rdb := newRedisClient(redisConf) return queueRedis.NewAdapter(rdb, 2), nil } diff --git a/internal/setup/redis.go b/internal/setup/redis.go new file mode 100644 index 0000000..3ec583a --- /dev/null +++ b/internal/setup/redis.go @@ -0,0 +1,20 @@ +package setup + +import ( + "time" + + "forge.cadoles.com/cadoles/bouncer/internal/config" + "github.com/redis/go-redis/v9" +) + +func newRedisClient(conf config.RedisConfig) redis.UniversalClient { + return redis.NewUniversalClient(&redis.UniversalOptions{ + Addrs: conf.Adresses, + MasterName: string(conf.Master), + ReadTimeout: time.Duration(conf.ReadTimeout), + WriteTimeout: time.Duration(conf.WriteTimeout), + DialTimeout: time.Duration(conf.DialTimeout), + RouteByLatency: true, + ContextTimeoutEnabled: true, + }) +} diff --git a/misc/packaging/common/config.yml b/misc/packaging/common/config.yml index d0e79d5..31e41cb 100644 --- a/misc/packaging/common/config.yml +++ b/misc/packaging/common/config.yml @@ -147,6 +147,9 @@ redis: addresses: - localhost:6379 master: "" + writeTimeout: 30s + readTimeout: 30s + dialTimeout: 30s # Configuration des logs logger: