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/redisclustermanager.go

73 lines
1.5 KiB
Go

4 years ago
package redis
import (
"crypto/tls"
4 years ago
"io"
"runtime"
"strings"
4 years ago
red "github.com/redis/go-redis/v9"
"github.com/zeromicro/go-zero/core/syncx"
4 years ago
)
const addrSep = ","
var (
clusterManager = syncx.NewResourceManager()
// clusterPoolSize is default pool size for cluster type of redis.
clusterPoolSize = 5 * runtime.GOMAXPROCS(0)
)
4 years ago
func getCluster(r *Redis) (*red.ClusterClient, error) {
val, err := clusterManager.GetResource(r.Addr, func() (io.Closer, error) {
var tlsConfig *tls.Config
if r.tls {
tlsConfig = &tls.Config{
InsecureSkipVerify: true,
}
}
4 years ago
store := red.NewClusterClient(&red.ClusterOptions{
Addrs: splitClusterAddrs(r.Addr),
Password: r.Pass,
4 years ago
MaxRetries: maxRetries,
MinIdleConns: idleConns,
TLSConfig: tlsConfig,
4 years ago
})
store.AddHook(durationHook)
for _, hook := range r.hooks {
store.AddHook(hook)
}
4 years ago
connCollector.registerClient(&statGetter{
clientType: ClusterType,
key: r.Addr,
poolSize: clusterPoolSize,
poolStats: func() *red.PoolStats {
return store.PoolStats()
},
})
4 years ago
return store, nil
})
if err != nil {
return nil, err
}
return val.(*red.ClusterClient), nil
}
func splitClusterAddrs(addr string) []string {
addrs := strings.Split(addr, addrSep)
unique := make(map[string]struct{})
for _, each := range addrs {
unique[strings.TrimSpace(each)] = struct{}{}
}
addrs = addrs[:0]
for k := range unique {
addrs = append(addrs, k)
}
return addrs
}