-
Notifications
You must be signed in to change notification settings - Fork 0
/
discovery.go
190 lines (160 loc) · 4.55 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package potens
import (
"errors"
"os"
"strconv"
"strings"
"time"
"github.com/kubex/potens-go/services"
"github.com/kubex/proto-go/discovery"
"go.uber.org/zap"
"net"
)
var ErrDiscoveryUnableToRegister = errors.New("Unable to register with the discovery service")
var ErrDiscoveryFailedToGoOnline = errors.New("Unable to take application online with discovery service")
var ErrDiscoveryFailedToGoOffline = errors.New("Unable to take application offline with discovery service")
func (app *Application) connectToDiscovery() error {
if app.services.discoveryClient == nil {
discoveryConn, err := app.GetServiceConnection(services.Discovery().Key())
if err != nil {
return err
}
app.services.discoveryClient = discovery.NewDiscoveryClient(discoveryConn)
}
return nil
}
func (app *Application) RegisterWithDiscovery(hostname string, port string) error {
app.Log().Debug("Registering with Discovery")
if hostname == "" {
hostname = getLocalIP()
}
if port == "" {
port = app.PortString()
}
err := app.connectToDiscovery()
if err != nil {
app.Log().Info("Discovery Connect Failure", zap.Error(err))
return err
}
app.Log().Debug("Opened Connection to Discovery")
version := os.Getenv(app.ServiceKey() + EnvServiceVersionSuffix)
if version != "" {
v, ok := discovery.AppRelease_value[version]
if ok {
app.appRelease = discovery.AppRelease(v)
}
}
portInt, _ := strconv.ParseInt(port, 10, 32)
app.Log().Debug("Starting Registration")
tCtx, _ := app.GrpcTimeoutContext(time.Second * 5)
regResult, err := app.services.discoveryClient.Register(tCtx, &discovery.RegisterRequest{
AppId: app.GlobalAppID(),
InstanceUuid: app.instanceID,
ServiceHost: hostname,
Release: app.appRelease,
ServicePort: int32(portInt),
})
if err != nil {
app.Log().Info("Registration Failure", zap.Error(err))
return err
}
if !regResult.Recorded {
app.Log().Debug("Unable to register")
return ErrDiscoveryUnableToRegister
}
app.services.discoveryHost = hostname
app.services.discoveryPort = port
app.services.discoveryRegistered = true
return nil
}
func getLocalIP() string {
addrs, err := net.InterfaceAddrs()
if err != nil {
return ""
}
for _, address := range addrs {
// check the address type and if it is not a loopback the display it
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
return ipnet.IP.String()
}
}
}
return ""
}
func (app *Application) discoveryHeartBeat() {
app.connectToDiscovery()
for {
if app.currentStatus != discovery.ServiceStatus_ONLINE {
return
}
if app.logDiscoveryHB {
app.Log().Debug("Sending heartbeat to discovery")
}
tCtx, _ := app.GrpcTimeoutContext(time.Second * 1)
_, err := app.services.discoveryClient.HeartBeat(tCtx, &discovery.HeartBeatRequest{
AppId: app.GlobalAppID(),
InstanceUuid: app.instanceID,
Release: app.appRelease,
})
if err != nil {
if !strings.Contains(err.Error(), "unregistered") {
app.services.discoveryClient = nil
app.connectToDiscovery()
}
app.RegisterWithDiscovery(app.services.discoveryHost, app.services.discoveryPort)
}
time.Sleep(5 * time.Second)
}
}
func (app *Application) DiscoveryOnline() error {
if !app.services.discoveryRegistered {
regErr := app.RegisterWithDiscovery("", "")
if regErr != nil {
return regErr
}
}
err := app.connectToDiscovery()
if err != nil {
return err
}
tCtx, _ := app.GrpcTimeoutContext(time.Second * 5)
statusResult, err := app.services.discoveryClient.Status(tCtx, &discovery.StatusRequest{
AppId: app.GlobalAppID(),
InstanceUuid: app.instanceID,
Release: app.appRelease,
Status: discovery.ServiceStatus_ONLINE,
Target: discovery.StatusTarget_BOTH,
})
if err != nil {
return err
}
if !statusResult.Recorded {
return ErrDiscoveryFailedToGoOnline
}
app.currentStatus = discovery.ServiceStatus_ONLINE
go app.discoveryHeartBeat()
return nil
}
func (app *Application) DiscoveryOffline() error {
err := app.connectToDiscovery()
if err != nil {
return err
}
tCtx, _ := app.GrpcTimeoutContext(time.Second * 5)
statusResult, err := app.services.discoveryClient.Status(tCtx, &discovery.StatusRequest{
AppId: app.GlobalAppID(),
InstanceUuid: app.instanceID,
Release: app.appRelease,
Status: discovery.ServiceStatus_OFFLINE,
Target: discovery.StatusTarget_INSTANCE,
})
if err != nil {
return err
}
if !statusResult.Recorded {
return ErrDiscoveryFailedToGoOffline
}
app.currentStatus = discovery.ServiceStatus_OFFLINE
return nil
}