MCPcopy
hub / github.com/redis/go-redis / NewFailoverClusterClient

Function NewFailoverClusterClient

sentinel.go:1199–1269  ·  view source on GitHub ↗

------------------------------------------------------------------------------ NewFailoverClusterClient returns a client that supports routing read-only commands to a replica node. Passing nil FailoverOptions will cause a panic.

(failoverOpt *FailoverOptions)

Source from the content-addressed store, hash-verified

1197// to a replica node.
1198// Passing nil FailoverOptions will cause a panic.
1199func NewFailoverClusterClient(failoverOpt *FailoverOptions) *ClusterClient {
1200 if failoverOpt == nil {
1201 panic("redis: NewFailoverClusterClient nil options")
1202 }
1203
1204 sentinelAddrs := make([]string, len(failoverOpt.SentinelAddrs))
1205 copy(sentinelAddrs, failoverOpt.SentinelAddrs)
1206
1207 failover := &sentinelFailover{
1208 opt: failoverOpt,
1209 sentinelAddrs: sentinelAddrs,
1210 }
1211
1212 opt := failoverOpt.clusterOptions()
1213 if failoverOpt.DB != 0 {
1214 onConnect := opt.OnConnect
1215
1216 opt.OnConnect = func(ctx context.Context, cn *Conn) error {
1217 if err := cn.Select(ctx, failoverOpt.DB).Err(); err != nil {
1218 return err
1219 }
1220
1221 if onConnect != nil {
1222 return onConnect(ctx, cn)
1223 }
1224
1225 return nil
1226 }
1227 }
1228
1229 opt.ClusterSlots = func(ctx context.Context) ([]ClusterSlot, error) {
1230 masterAddr, err := failover.MasterAddr(ctx)
1231 if err != nil {
1232 return nil, err
1233 }
1234
1235 nodes := []ClusterNode{{
1236 Addr: masterAddr,
1237 }}
1238
1239 replicaAddrs, err := failover.replicaAddrs(ctx, false)
1240 if err != nil {
1241 return nil, err
1242 }
1243
1244 for _, replicaAddr := range replicaAddrs {
1245 nodes = append(nodes, ClusterNode{
1246 Addr: replicaAddr,
1247 })
1248 }
1249
1250 slots := []ClusterSlot{
1251 {
1252 Start: 0,
1253 End: 16383,
1254 Nodes: nodes,
1255 },
1256 }

Callers 1

NewUniversalClientFunction · 0.85

Calls 7

MasterAddrMethod · 0.95
replicaAddrsMethod · 0.95
ReloadStateMethod · 0.95
NewClusterClientFunction · 0.85
clusterOptionsMethod · 0.80
ErrMethod · 0.65
SelectMethod · 0.65

Tested by

no test coverage detected