export_test.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package redis
  2. import (
  3. "fmt"
  4. "net"
  5. "time"
  6. "github.com/go-redis/redis/internal/hashtag"
  7. "github.com/go-redis/redis/internal/pool"
  8. )
  9. func (c *baseClient) Pool() pool.Pooler {
  10. return c.connPool
  11. }
  12. func (c *PubSub) SetNetConn(netConn net.Conn) {
  13. c.cn = pool.NewConn(netConn)
  14. }
  15. func (c *PubSub) ReceiveMessageTimeout(timeout time.Duration) (*Message, error) {
  16. return c.receiveMessage(timeout)
  17. }
  18. func (c *ClusterClient) GetState() (*clusterState, error) {
  19. return c.state.Get()
  20. }
  21. func (c *ClusterClient) LoadState() (*clusterState, error) {
  22. return c.loadState()
  23. }
  24. func (c *ClusterClient) SlotAddrs(slot int) []string {
  25. state, err := c.state.Get()
  26. if err != nil {
  27. panic(err)
  28. }
  29. var addrs []string
  30. for _, n := range state.slotNodes(slot) {
  31. addrs = append(addrs, n.Client.getAddr())
  32. }
  33. return addrs
  34. }
  35. func (c *ClusterClient) Nodes(key string) ([]*clusterNode, error) {
  36. state, err := c.state.Reload()
  37. if err != nil {
  38. return nil, err
  39. }
  40. slot := hashtag.Slot(key)
  41. nodes := state.slots[slot]
  42. if len(nodes) != 2 {
  43. return nil, fmt.Errorf("slot=%d does not have enough nodes: %v", slot, nodes)
  44. }
  45. return nodes, nil
  46. }
  47. func (c *ClusterClient) SwapNodes(key string) error {
  48. nodes, err := c.Nodes(key)
  49. if err != nil {
  50. return err
  51. }
  52. nodes[0], nodes[1] = nodes[1], nodes[0]
  53. return nil
  54. }