...

Source file src/github.com/go-redis/redis/export_test.go

Documentation: github.com/go-redis/redis

     1  package redis
     2  
     3  import (
     4  	"fmt"
     5  	"net"
     6  	"strings"
     7  
     8  	"github.com/go-redis/redis/internal/hashtag"
     9  	"github.com/go-redis/redis/internal/pool"
    10  )
    11  
    12  func (c *baseClient) Pool() pool.Pooler {
    13  	return c.connPool
    14  }
    15  
    16  func (c *PubSub) SetNetConn(netConn net.Conn) {
    17  	c.cn = pool.NewConn(netConn)
    18  }
    19  
    20  func (c *ClusterClient) LoadState() (*clusterState, error) {
    21  	return c.loadState()
    22  }
    23  
    24  func (c *ClusterClient) SlotAddrs(slot int) []string {
    25  	state, err := c.state.Get()
    26  	if err != nil {
    27  		panic(err)
    28  	}
    29  
    30  	var addrs []string
    31  	for _, n := range state.slotNodes(slot) {
    32  		addrs = append(addrs, n.Client.getAddr())
    33  	}
    34  	return addrs
    35  }
    36  
    37  func (c *ClusterClient) Nodes(key string) ([]*clusterNode, error) {
    38  	state, err := c.state.Reload()
    39  	if err != nil {
    40  		return nil, err
    41  	}
    42  
    43  	slot := hashtag.Slot(key)
    44  	nodes := state.slotNodes(slot)
    45  	if len(nodes) != 2 {
    46  		return nil, fmt.Errorf("slot=%d does not have enough nodes: %v", slot, nodes)
    47  	}
    48  	return nodes, nil
    49  }
    50  
    51  func (c *ClusterClient) SwapNodes(key string) error {
    52  	nodes, err := c.Nodes(key)
    53  	if err != nil {
    54  		return err
    55  	}
    56  	nodes[0], nodes[1] = nodes[1], nodes[0]
    57  	return nil
    58  }
    59  
    60  func (state *clusterState) IsConsistent() bool {
    61  	if len(state.Masters) < 3 {
    62  		return false
    63  	}
    64  	for _, master := range state.Masters {
    65  		s := master.Client.Info("replication").Val()
    66  		if !strings.Contains(s, "role:master") {
    67  			return false
    68  		}
    69  	}
    70  
    71  	if len(state.Slaves) < 3 {
    72  		return false
    73  	}
    74  	for _, slave := range state.Slaves {
    75  		s := slave.Client.Info("replication").Val()
    76  		if !strings.Contains(s, "role:slave") {
    77  			return false
    78  		}
    79  	}
    80  
    81  	return true
    82  }
    83  

View as plain text