This commit is contained in:
John Guo
2022-02-15 01:10:03 +08:00
parent 2428b27168
commit f0b78253b2
4 changed files with 20 additions and 5 deletions

View File

@ -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
}
}

View File

@ -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.

View File

@ -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...)