------------------------------------------------------------------------------ NewFailoverClusterClient returns a client that supports routing read-only commands to a replica node. Passing nil FailoverOptions will cause a panic.
(failoverOpt *FailoverOptions)
| 1197 | // to a replica node. |
| 1198 | // Passing nil FailoverOptions will cause a panic. |
| 1199 | func 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 | } |
no test coverage detected