You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-zero/core/stores/redis/redisclientmanager.go

62 lines
1.2 KiB
Go

4 years ago
package redis
import (
"crypto/tls"
4 years ago
"io"
"runtime"
4 years ago
red "github.com/go-redis/redis/v8"
"github.com/zeromicro/go-zero/core/syncx"
4 years ago
)
const (
defaultDatabase = 0
maxRetries = 3
idleConns = 8
)
var (
clientManager = syncx.NewResourceManager()
// nodePoolSize is default pool size for node type of redis.
nodePoolSize = 10 * runtime.GOMAXPROCS(0)
)
4 years ago
func getClient(r *Redis) (*red.Client, error) {
val, err := clientManager.GetResource(r.Addr, func() (io.Closer, error) {
var tlsConfig *tls.Config
if r.tls {
tlsConfig = &tls.Config{
InsecureSkipVerify: true,
}
}
4 years ago
store := red.NewClient(&red.Options{
Addr: r.Addr,
Password: r.Pass,
4 years ago
DB: defaultDatabase,
MaxRetries: maxRetries,
MinIdleConns: idleConns,
TLSConfig: tlsConfig,
4 years ago
})
store.AddHook(durationHook)
for _, hook := range r.hooks {
store.AddHook(hook)
}
connCollector.registerClient(&statGetter{
clientType: NodeType,
key: r.Addr,
poolSize: nodePoolSize,
poolStats: func() *red.PoolStats {
return store.PoolStats()
},
})
4 years ago
return store, nil
})
if err != nil {
return nil, err
}
return val.(*red.Client), nil
}