mirror of
https://gitee.com/johng/gf
synced 2026-06-07 10:22:11 +08:00
fix issue #1605
This commit is contained in:
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
@ -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.
|
||||
|
||||
@ -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...)
|
||||
|
||||
Reference in New Issue
Block a user