-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcluster.go
116 lines (103 loc) · 2.64 KB
/
cluster.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package rediscluster
import (
"github.com/fzzbt/radix/redis"
"github.com/joaojeronimo/go-crc16"
"strconv"
"strings"
)
func clusterSlot(key string) int {
return (int(crc16.Crc16(key)) % 4096)
}
type SlotInterval struct {
LowerLimit int
UpperLimit int
}
func ParseNode(unparsedNode string) (node Node) {
parts := strings.Split(unparsedNode, " ")
node.Name = parts[0]
node.Address = parts[1]
node.Flags = parts[2]
//node.LastPingSent, _ = strconv.Atoi(parts[4])
//node.LastPongReceived, _ = strconv.Atoi(parts[5])
node.State = parts[6]
slots := strings.Split(parts[7], "-")
var slotInterval SlotInterval
slotInterval.LowerLimit, _ = strconv.Atoi(slots[0])
slotInterval.UpperLimit, _ = strconv.Atoi(slots[1])
node.Slots = slotInterval
return
}
type Node struct {
Name string
Address string
Client *redis.Client
Flags string
LastPingSent int
LastPongReceived int
State string
Slots SlotInterval
}
type Cluster struct {
nodes []Node
}
func (n *Node) Connect() {
conf := redis.DefaultConfig()
conf.Address = n.Address
n.Client = redis.NewClient(conf)
}
func discoverNodes(firstLink string) (cluster Cluster) {
conf := redis.DefaultConfig()
conf.Address = firstLink
c := redis.NewClient(conf)
s, err := c.Cluster("nodes").Str()
if err != nil {
return
}
unparsedNodes := strings.Split(s, "\n")
var parsedNodes []Node
for i := 0; i < len(unparsedNodes)-1; i++ { // -1 because the last line is empty
parsedNode := ParseNode(unparsedNodes[i])
if parsedNode.Flags == "myself" {
parsedNode.Address = firstLink
parsedNode.Client = c
} else {
parsedNode.Connect()
}
parsedNodes = append(parsedNodes, parsedNode)
}
cluster.nodes = parsedNodes
return
}
func NewCluster(firstLink string) (cc Cluster) {
return discoverNodes(firstLink)
}
func (cc *Cluster) Call(command string, args ...interface{}) (reply *redis.Reply) {
slot := clusterSlot(args[0].(string))
var slots SlotInterval
for i := 0; i < len(cc.nodes); i++ {
slots = cc.nodes[i].Slots
if slot > slots.LowerLimit && slot < slots.UpperLimit {
reply = cc.nodes[i].Client.Call(command, args...)
return
}
}
return
}
func (cc *Cluster) AsyncCall(command string, args ...interface{}) (future redis.Future) {
slot := clusterSlot(args[0].(string))
var slots SlotInterval
for i := 0; i < len(cc.nodes); i++ {
slots = cc.nodes[i].Slots
if slot >= slots.LowerLimit && slot <= slots.UpperLimit {
future = cc.nodes[i].Client.AsyncCall(command, args...)
return
}
}
return
}
func (cc *Cluster) Close() bool {
for i := 0; i < len(cc.nodes); i++ {
cc.nodes[i].Client.Close()
}
return true
}