...
1 package redis
2
3 import (
4 "context"
5 "fmt"
6 "net"
7 "strings"
8
9 "github.com/redis/go-redis/v9/internal"
10 "github.com/redis/go-redis/v9/internal/hashtag"
11 "github.com/redis/go-redis/v9/internal/pool"
12 )
13
14 func (c *baseClient) Pool() pool.Pooler {
15 return c.connPool
16 }
17
18 func (c *PubSub) SetNetConn(netConn net.Conn) {
19 c.cn = pool.NewConn(netConn)
20 }
21
22 func (c *ClusterClient) LoadState(ctx context.Context) (*clusterState, error) {
23
24 return c.loadState(ctx)
25 }
26
27 func (c *ClusterClient) SlotAddrs(ctx context.Context, slot int) []string {
28 state, err := c.state.Get(ctx)
29 if err != nil {
30 panic(err)
31 }
32
33 var addrs []string
34 for _, n := range state.slotNodes(slot) {
35 addrs = append(addrs, n.Client.getAddr())
36 }
37 return addrs
38 }
39
40 func (c *ClusterClient) Nodes(ctx context.Context, key string) ([]*clusterNode, error) {
41 state, err := c.state.Reload(ctx)
42 if err != nil {
43 return nil, err
44 }
45
46 slot := hashtag.Slot(key)
47 nodes := state.slotNodes(slot)
48 if len(nodes) != 2 {
49 return nil, fmt.Errorf("slot=%d does not have enough nodes: %v", slot, nodes)
50 }
51 return nodes, nil
52 }
53
54 func (c *ClusterClient) SwapNodes(ctx context.Context, key string) error {
55 nodes, err := c.Nodes(ctx, key)
56 if err != nil {
57 return err
58 }
59 nodes[0], nodes[1] = nodes[1], nodes[0]
60 return nil
61 }
62
63 func (c *clusterState) IsConsistent(ctx context.Context) bool {
64 if len(c.Masters) < 3 {
65 return false
66 }
67 for _, master := range c.Masters {
68 s := master.Client.Info(ctx, "replication").Val()
69 if !strings.Contains(s, "role:master") {
70 return false
71 }
72 }
73
74 if len(c.Slaves) < 3 {
75 return false
76 }
77 for _, slave := range c.Slaves {
78 s := slave.Client.Info(ctx, "replication").Val()
79 if !strings.Contains(s, "role:slave") {
80 return false
81 }
82 }
83
84 return true
85 }
86
87 func GetSlavesAddrByName(ctx context.Context, c *SentinelClient, name string) []string {
88 addrs, err := c.Replicas(ctx, name).Result()
89 if err != nil {
90 internal.Logger.Printf(ctx, "sentinel: Replicas name=%q failed: %s",
91 name, err)
92 return []string{}
93 }
94 return parseReplicaAddrs(addrs, false)
95 }
96
97 func (c *Ring) ShardByName(name string) *ringShard {
98 shard, _ := c.sharding.GetByName(name)
99 return shard
100 }
101
102 func (c *ModuleLoadexConfig) ToArgs() []interface{} {
103 return c.toArgs()
104 }
105
106 func ShouldRetry(err error, retryTimeout bool) bool {
107 return shouldRetry(err, retryTimeout)
108 }
109
View as plain text