-
Notifications
You must be signed in to change notification settings - Fork 198
/
discovery.go
175 lines (146 loc) · 3.13 KB
/
discovery.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package discovery
import (
"errors"
"github.com/eolinker/eosc"
"github.com/eolinker/eosc/eocontext"
"sync"
"sync/atomic"
"time"
)
var (
ErrDiscoveryDown = errors.New("discovery down")
)
// IDiscovery 服务发现接口
type IDiscovery interface {
GetApp(config string) (IApp, error)
}
// CheckSkill 检查目标技能是否符合
func CheckSkill(skill string) bool {
return skill == "github.com/eolinker/apinto/discovery.discovery.IDiscovery"
}
type NodeInfo struct {
Ip string
Port int
Labels map[string]string
}
type IAppContainer interface {
INodes
Set(id string, info []NodeInfo) (app IAppAgent)
Reset(info map[string][]NodeInfo)
GetApp(id string) (IAppAgent, bool)
Keys() []string
}
type appContainer struct {
lock sync.RWMutex
nodes eosc.Untyped[string, INode]
apps map[string]*_AppAgent
isHealthCheck int32
}
func (ac *appContainer) status(status NodeStatus) NodeStatus {
if atomic.LoadInt32(&ac.isHealthCheck) > 0 {
return status
}
return Running
}
func (ac *appContainer) SetHealthCheck(isHealthCheck bool) {
if isHealthCheck {
atomic.StoreInt32(&ac.isHealthCheck, 1)
} else {
atomic.StoreInt32(&ac.isHealthCheck, 0)
}
}
func NewAppContainer() IAppContainer {
return &appContainer{
apps: make(map[string]*_AppAgent),
nodes: eosc.BuildUntyped[string, INode](),
}
}
func (ac *appContainer) Keys() []string {
ac.lock.RLock()
defer ac.lock.RUnlock()
if ac.apps == nil {
return nil
}
keys := make([]string, 0, len(ac.apps))
for k := range ac.apps {
keys = append(keys, k)
}
return keys
}
func (ac *appContainer) create(infos []NodeInfo) []eocontext.INode {
ns := make([]eocontext.INode, 0, len(infos))
for _, i := range infos {
n := ac.Get(i.Ip, i.Port)
ns = append(ns, NewNode(n, i.Labels))
}
return ns
}
func (ac *appContainer) Set(name string, infos []NodeInfo) IAppAgent {
ns := ac.create(infos)
ac.lock.RLock()
app, has := ac.apps[name]
ac.lock.RUnlock()
if has {
app.reset(ns)
return app
}
ac.lock.Lock()
app, has = ac.apps[name]
needCheck := false
if !has {
if len(ac.apps) == 0 {
needCheck = true
}
app = newApp(ns)
ac.apps[name] = app
}
ac.lock.Unlock()
if needCheck {
go ac.doCheck()
}
return app
}
func (ac *appContainer) doCheck() {
t := time.NewTicker(time.Second * 10)
defer t.Stop()
for range t.C {
ac.lock.Lock()
if len(ac.apps) == 0 {
ac.lock.Unlock()
return
}
for key, app := range ac.apps {
if atomic.LoadInt64(&app.use) <= 0 {
delete(ac.apps, key)
}
}
nodeUse := make(map[string]int)
for _, app := range ac.apps {
for _, n := range app.nodes {
nodeUse[n.ID()] += 1
}
}
nodeList := ac.nodes.List()
for _, n := range nodeList {
if nodeUse[n.ID()] == 0 {
ac.nodes.Del(n.ID())
}
}
ac.lock.Unlock()
}
}
func (ac *appContainer) Reset(infos map[string][]NodeInfo) {
nm := make(map[string]*_AppAgent)
for name, info := range infos {
nm[name] = newApp(ac.create(info))
}
ac.lock.Lock()
ac.apps = nm
ac.lock.Unlock()
}
func (ac *appContainer) GetApp(name string) (IAppAgent, bool) {
ac.lock.RLock()
defer ac.lock.RUnlock()
app, has := ac.apps[name]
return app, has
}