-
Notifications
You must be signed in to change notification settings - Fork 0
/
dispatcher.go
83 lines (74 loc) · 1.89 KB
/
dispatcher.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
package domain
import (
"sort"
"sync"
"github.com/coderc/im/ipconfig/source"
)
type Dispatcher struct {
candidateTable map[string]*EndPoint
sync.RWMutex
}
var (
dp *Dispatcher // 全局服务调度器,用于定制和处理流量分配的逻辑
)
func Init() {
dp = &Dispatcher{}
dp.candidateTable = make(map[string]*EndPoint)
go func() {
for event := range source.EventChan() {
switch event.Type {
case source.AddNodeEvent:
dp.addNode(event)
case source.DelNodeEvent:
dp.delNode(event)
}
}
}()
}
func Dispatch(ctx *IpConfigContext) []*EndPoint {
// 获取候选endpoint
eds := dp.getCandidateEndpoint(ctx)
// 计算得分
for _, ed := range eds {
ed.CalculateScore(ctx)
}
// 排序
sort.Slice(eds, func(i, j int) bool {
// 优先基于active score进行排序,当active score相等时基于static score排序
return eds[i].ActiveScore > eds[j].ActiveScore || (eds[i].ActiveScore == eds[j].ActiveScore && eds[i].StaticScore > eds[j].StaticScore)
})
return eds
}
// getCandidateEndpoint 获取调度器当前持有的下游服务的endpoint list
func (dp *Dispatcher) getCandidateEndpoint(ctx *IpConfigContext) []*EndPoint {
dp.RLock()
defer dp.RUnlock()
candidateList := make([]*EndPoint, 0, len(dp.candidateTable))
for _, ed := range dp.candidateTable {
candidateList = append(candidateList, ed)
}
return candidateList
}
// delNode 下游服务下线
func (dp *Dispatcher) delNode(event *source.Event) {
dp.Lock()
defer dp.Unlock()
delete(dp.candidateTable, event.Key())
}
// addNode 下游服务上线/更新
func (dp *Dispatcher) addNode(event *source.Event) {
dp.Lock()
defer dp.Unlock()
var (
ed *EndPoint
ok bool
)
if ed, ok = dp.candidateTable[event.Key()]; !ok {
ed = NewEndPoint(event.IP, event.Port)
dp.candidateTable[event.Key()] = ed
}
ed.UpdateStat(&Stat{
ConnectNum: event.ConnectNum,
MessageBytes: event.MessageBytes,
})
}