/
rpc.go
150 lines (138 loc) · 3.15 KB
/
rpc.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package api
import (
"errors"
"hash/crc32"
"math/rand"
"strconv"
"strings"
)
type RpcClient struct {
rpc string
}
func (s *RpcClient) Services(name string, state StateType) (service Service) {
client, err := NewClient(s.rpc)
if err != nil {
return
}
defer client.Close()
err = client.Call("Query.Service", ParamService{Name: name, State: state}, &service)
if err != nil {
return
}
return
}
func reserveAddress(url string) (string, string) {
prefix := "http://"
serviceName := ""
urls := strings.SplitN(url, "//", 2)
if len(urls) == 1 {
return "", url
} else {
prefix = urls[0] + "//"
serviceName = urls[1]
if prefix == "rpc://" {
prefix = ""
}
if prefix == "tcp://" {
prefix = ""
}
return prefix, serviceName
}
}
/*
random
url: http://serviceName or rpc://serviceName
http url return http://ip:port
rpc url only return ip:port
*/
func (s *RpcClient) Random(url string) (string, error) {
prefix, serviceName := reserveAddress(url)
service := s.Services(serviceName, StateAlive)
count := len(service.NestNodes)
if count > 0 {
r := generateNumber(0, count-1)
return prefix + service.NestNodes[r].IP + ":" + strconv.Itoa(service.NestNodes[r].Port), nil
}
return "", errors.New("404")
}
var counter = make(map[string]int)
/*
round
url: http://serviceName or rpc://serviceName
http url return http://ip:port
rpc url only return ip:port
*/
func (s *RpcClient) RoundRobin(url string) (string, error) {
prefix, serviceName := reserveAddress(url)
service := s.Services(serviceName, StateAlive)
count := len(service.NestNodes)
if count > 0 {
r := counter[serviceName]
if r >= count {
r = 0
}
u := prefix + service.NestNodes[r].IP + ":" + strconv.Itoa(service.NestNodes[r].Port)
if r == count-1 {
r = 0
} else {
r++
}
counter[serviceName] = r
return u, nil
}
return "", errors.New("404")
}
/*
hash
url: http://serviceName or rpc://serviceName
http url return http://ip:port
rpc url only return ip:port
*/
func (s *RpcClient) Hash(key, url string) (string, error) {
prefix, serviceName := reserveAddress(url)
service := s.Services(serviceName, StateAlive)
count := len(service.NestNodes)
if count > 0 {
r := hash(key) % count
return prefix + service.NestNodes[r].IP + ":" + strconv.Itoa(service.NestNodes[r].Port), nil
}
return "", errors.New("404")
}
/*
hashring
url: http://serviceName or rpc://serviceName
http url return http://ip:port
rpc url only return ip:port
*/
func (s *RpcClient) ConsistentHash(key, url string) (string, error) {
prefix, serviceName := reserveAddress(url)
service := s.Services(serviceName, StateAlive)
count := len(service.NestNodes)
//make hashring
if count > 0 {
nodes := []string{}
for i := 0; i < count; i++ {
nodes = append(nodes, service.NestNodes[i].IP+":"+strconv.Itoa(service.NestNodes[i].Port))
}
ring := newConsistentHash(nodes)
if server, ok := ring.GetNode(key); ok {
return prefix + server, nil
}
}
return "", errors.New("404")
}
func generateNumber(min, max int) int {
i := rand.Intn(max-min) + min
return i
}
func hash(s string) int {
v := int(crc32.ChecksumIEEE([]byte(s)))
if v >= 0 {
return v
}
if -v >= 0 {
return -v
}
// v == MinInt
return 0
}