forked from go-kratos/kratos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eureka.go
137 lines (114 loc) · 2.84 KB
/
eureka.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
package eureka
import (
"context"
"strings"
"sync"
"time"
)
type subscriber struct {
appID string
callBack func()
}
type API struct {
cli *Client
allInstances map[string][]Instance
subscribers map[string]*subscriber
refreshInterval time.Duration
lock sync.Mutex
}
func NewAPI(ctx context.Context, client *Client, refreshInterval time.Duration) *API {
e := &API{
cli: client,
allInstances: make(map[string][]Instance),
subscribers: make(map[string]*subscriber),
refreshInterval: refreshInterval,
}
// it is required to broadcast for the first time
go e.broadcast()
go e.refresh(ctx)
return e
}
func (e *API) refresh(ctx context.Context) {
ticker := time.NewTicker(e.refreshInterval)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
e.broadcast()
}
}
}
func (e *API) broadcast() {
instances := e.cacheAllInstances()
if instances == nil {
return
}
for _, subscriber := range e.subscribers {
go subscriber.callBack()
}
defer e.lock.Unlock()
e.lock.Lock()
e.allInstances = instances
}
func (e *API) cacheAllInstances() map[string][]Instance {
items := make(map[string][]Instance)
instances := e.cli.FetchAllUpInstances(context.Background())
for _, instance := range instances {
items[e.ToAppID(instance.App)] = append(items[instance.App], instance)
}
return items
}
func (e *API) Register(ctx context.Context, serviceName string, endpoints ...Endpoint) error {
appID := e.ToAppID(serviceName)
upInstances := make(map[string]struct{})
for _, ins := range e.GetService(ctx, appID) {
upInstances[ins.InstanceID] = struct{}{}
}
for _, ep := range endpoints {
if _, ok := upInstances[ep.InstanceID]; !ok {
if err := e.cli.Register(ctx, ep); err != nil {
return err
}
go e.cli.Heartbeat(ep)
}
}
return nil
}
// Deregister ctx is the same as register ctx
func (e *API) Deregister(ctx context.Context, endpoints []Endpoint) error {
for _, ep := range endpoints {
if err := e.cli.Deregister(ctx, ep.AppID, ep.InstanceID); err != nil {
return err
}
}
return nil
}
func (e *API) Subscribe(serverName string, fn func()) error {
e.lock.Lock()
defer e.lock.Unlock()
appID := e.ToAppID(serverName)
e.subscribers[appID] = &subscriber{
appID: appID,
callBack: fn,
}
go e.broadcast()
return nil
}
func (e *API) GetService(ctx context.Context, serverName string) []Instance {
appID := e.ToAppID(serverName)
if ins, ok := e.allInstances[appID]; ok {
return ins
}
// if not in allInstances of API, you can try to obtain it separately again
return e.cli.FetchAppUpInstances(ctx, appID)
}
func (e *API) Unsubscribe(serverName string) {
e.lock.Lock()
defer e.lock.Unlock()
delete(e.subscribers, e.ToAppID(serverName))
}
func (e *API) ToAppID(serverName string) string {
return strings.ToUpper(serverName)
}