-
Notifications
You must be signed in to change notification settings - Fork 1
/
soft_state.go
84 lines (72 loc) · 1.68 KB
/
soft_state.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
package selector
import "context"
const (
CtxSoftStateTag = "ctx_soft_state_tag"
)
type SoftState struct {
servers []*SoftStateNode
serverTimes []uint64
times uint64
}
func (s *SoftState) Select(ctx context.Context, servicePath, serviceMethod string, args interface{}) string {
var ss = s.servers
if len(ss) == 0 {
return ""
}
playTag, ok := ctx.Value(CtxSoftStateTag).(string)
if !ok {
return ""
}
var (
optionList = make([]int, 0, len(s.servers))
i, j int
)
for i = range s.servers {
if len(s.servers[i].Games) == 0 {
optionList = append(optionList, i)
continue
}
for j = range s.servers[i].Games {
if s.servers[i].Games[j] == playTag {
optionList = append(optionList, i)
break
}
}
}
count := uint64(len(optionList))
if count == 0 {
return ""
}
var (
useServerIndex = -1
index uint64
)
for i := s.times; i < s.times+count; i++ {
index = i % count
if s.servers[optionList[index]].AbandonArray[s.serverTimes[optionList[index]]%10] == 0 {
useServerIndex = optionList[index]
break
}
s.serverTimes[optionList[index]]++
}
// 未选出, 按最小调用次数获取
if useServerIndex == -1 {
useServerIndex = optionList[0]
var minTime = s.serverTimes[useServerIndex]
for i = 1; i < len(optionList); i++ {
if s.serverTimes[optionList[i]] < minTime {
useServerIndex = optionList[i]
minTime = s.serverTimes[useServerIndex]
}
}
}
s.serverTimes[useServerIndex]++
s.times++
return s.servers[useServerIndex].Server
}
func (s *SoftState) UpdateServer(servers map[string]string) {
ss := createSoftStateNode(servers)
s.servers = ss
s.serverTimes = make([]uint64, len(ss))
s.times = 0
}