From f0b78253b2d69bbdbe989e8b3b632f7e35f2e782 Mon Sep 17 00:00:00 2001 From: John Guo Date: Tue, 15 Feb 2022 01:10:03 +0800 Subject: [PATCH] fix issue #1605 --- database/gredis/gredis_adapter_goredis.go | 8 ++++++++ database/gredis/gredis_config.go | 2 +- database/gredis/gredis_redis.go | 10 ++++++++-- frame/gins/gins_redis.go | 5 +++-- 4 files changed, 20 insertions(+), 5 deletions(-) diff --git a/database/gredis/gredis_adapter_goredis.go b/database/gredis/gredis_adapter_goredis.go index 027d806f3..28ac0ebf1 100644 --- a/database/gredis/gredis_adapter_goredis.go +++ b/database/gredis/gredis_adapter_goredis.go @@ -28,6 +28,7 @@ const ( defaultPoolIdleTimeout = 10 * time.Second defaultPoolWaitTimeout = 10 * time.Second defaultPoolMaxLifeTime = 30 * time.Second + defaultMaxRetries = -1 ) // NewAdapterGoRedis creates and returns a redis adapter using go-redis. @@ -37,6 +38,7 @@ func NewAdapterGoRedis(config *Config) *AdapterGoRedis { Addrs: gstr.SplitAndTrim(config.Address, ","), Password: config.Pass, DB: config.Db, + MaxRetries: defaultMaxRetries, MinIdleConns: config.MinIdle, MaxConnAge: config.MaxConnLifetime, IdleTimeout: config.IdleTimeout, @@ -90,4 +92,10 @@ func fillWithDefaultConfiguration(config *Config) { if config.MaxConnLifetime == 0 { config.MaxConnLifetime = defaultPoolMaxLifeTime } + if config.WriteTimeout == 0 { + config.WriteTimeout = -1 + } + if config.ReadTimeout == 0 { + config.ReadTimeout = -1 + } } diff --git a/database/gredis/gredis_config.go b/database/gredis/gredis_config.go index 26600e124..d381332ff 100644 --- a/database/gredis/gredis_config.go +++ b/database/gredis/gredis_config.go @@ -30,7 +30,7 @@ type Config struct { IdleTimeout time.Duration `json:"idleTimeout"` // Maximum idle time for connection (default is 10 seconds, not allowed to be set to 0) WaitTimeout time.Duration `json:"waitTimeout"` // Timed out duration waiting to get a connection from the connection pool. DialTimeout time.Duration `json:"dialTimeout"` // Dial connection timeout for TCP. - ReadTimeout time.Duration `json:"readTimeout"` // Read timeout for TCP. + ReadTimeout time.Duration `json:"readTimeout"` // Read timeout for TCP. DO NOT set it if not necessary. WriteTimeout time.Duration `json:"writeTimeout"` // Write timeout for TCP. MasterName string `json:"masterName"` // Used in Redis Sentinel mode. TLS bool `json:"tls"` // Specifies whether TLS should be used when connecting to the server. diff --git a/database/gredis/gredis_redis.go b/database/gredis/gredis_redis.go index 445876b74..389a9d4db 100644 --- a/database/gredis/gredis_redis.go +++ b/database/gredis/gredis_redis.go @@ -46,6 +46,12 @@ func (r *Redis) Conn(ctx context.Context) (*RedisConn, error) { if r == nil { return nil, gerror.NewCode(gcode.CodeInvalidParameter, errorNilRedis) } + if r.adapter == nil { + return nil, gerror.NewCodef( + gcode.CodeMissingConfiguration, + `redis adapter not initialized, missing configuration or adapter register?`, + ) + } conn, err := r.adapter.Conn(ctx) if err != nil { return nil, err @@ -67,8 +73,8 @@ func (r *Redis) Do(ctx context.Context, command string, args ...interface{}) (*g return nil, err } defer func() { - if err := conn.Close(ctx); err != nil { - intlog.Errorf(ctx, `%+v`, err) + if closeErr := conn.Close(ctx); closeErr != nil { + intlog.Errorf(ctx, `%+v`, closeErr) } }() return conn.Do(ctx, command, args...) diff --git a/frame/gins/gins_redis.go b/frame/gins/gins_redis.go index 5723b5c88..ea5b2aebb 100644 --- a/frame/gins/gins_redis.go +++ b/frame/gins/gins_redis.go @@ -55,10 +55,11 @@ func Redis(name ...string) *gredis.Redis { if redisConfig, err = gredis.ConfigFromMap(gconv.Map(v)); err != nil { panic(err) } + } else { + intlog.Printf(ctx, `missing configuration for redis group "%s"`, group) } - intlog.Printf(ctx, `missing configuration for redis group "%s"`, group) } else { - intlog.Printf(ctx, `missing configuration for redis: "redis" node not found`) + intlog.Print(ctx, `missing configuration for redis: "redis" node not found`) } if redisClient, err = gredis.New(redisConfig); err != nil { panic(err)