forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dashboard_register.go
199 lines (162 loc) · 4.36 KB
/
dashboard_register.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
191
192
193
194
195
196
197
198
199
package main
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"sync"
"time"
"github.com/Sirupsen/logrus"
"github.com/TykTechnologies/tyk/config"
)
type NodeResponseOK struct {
Status string
Message map[string]string
Nonce string
}
type DashboardServiceSender interface {
Init() error
Register() error
DeRegister() error
StartBeating() error
StopBeating()
}
type HTTPDashboardHandler struct {
RegistrationEndpoint string
DeRegistrationEndpoint string
HeartBeatEndpoint string
Secret string
heartBeatStopSentinel bool
}
func reLogin() {
if !config.Global.UseDBAppConfigs {
return
}
log.WithFields(logrus.Fields{
"prefix": "main",
}).Info("Registering node (again).")
DashService.StopBeating()
if err := DashService.DeRegister(); err != nil {
log.WithFields(logrus.Fields{
"prefix": "main",
}).Error("Could not deregister: ", err)
}
time.Sleep(30 * time.Second)
if err := DashService.Register(); err != nil {
log.WithFields(logrus.Fields{
"prefix": "main",
}).Error("Could not register: ", err)
} else {
go DashService.StartBeating()
}
log.WithFields(logrus.Fields{
"prefix": "main",
}).Info("Recovering configurations, reloading...")
var wg sync.WaitGroup
wg.Add(1)
reloadURLStructure(wg.Done)
wg.Wait()
}
func (h *HTTPDashboardHandler) Init() error {
h.RegistrationEndpoint = buildConnStr("/register/node")
h.DeRegistrationEndpoint = buildConnStr("/system/node")
h.HeartBeatEndpoint = buildConnStr("/register/ping")
h.Secret = config.Global.NodeSecret
return nil
}
func (h *HTTPDashboardHandler) Register() error {
req := h.newRequest(h.RegistrationEndpoint)
c := &http.Client{Timeout: 5 * time.Second}
resp, err := c.Do(req)
if err != nil {
log.Errorf("Request failed with error %v; retrying in 5s", err)
time.Sleep(time.Second * 5)
return h.Register()
} else if resp != nil && resp.StatusCode != 200 {
log.Errorf("Response failed with code %d; retrying in 5s", resp.StatusCode)
time.Sleep(time.Second * 5)
return h.Register()
}
defer resp.Body.Close()
val := NodeResponseOK{}
if err := json.NewDecoder(resp.Body).Decode(&val); err != nil {
return err
}
// Set the NodeID
var found bool
NodeID, found = val.Message["NodeID"]
if !found {
log.Error("Failed to register node, retrying in 5s")
time.Sleep(time.Second * 5)
return h.Register()
}
log.WithFields(logrus.Fields{
"prefix": "dashboard",
"id": NodeID,
}).Info("Node registered")
// Set the nonce
ServiceNonce = val.Nonce
log.Debug("Registration Finished: Nonce Set: ", ServiceNonce)
return nil
}
func (h *HTTPDashboardHandler) StartBeating() error {
for !h.heartBeatStopSentinel {
if err := h.sendHeartBeat(); err != nil {
log.Warning(err)
}
time.Sleep(time.Second * 2)
}
log.Info("Stopped Heartbeat")
h.heartBeatStopSentinel = false
return nil
}
func (h *HTTPDashboardHandler) StopBeating() {
h.heartBeatStopSentinel = true
}
func (h *HTTPDashboardHandler) newRequest(endpoint string) *http.Request {
req, err := http.NewRequest("GET", endpoint, nil)
if err != nil {
panic(err)
}
req.Header.Set("authorization", h.Secret)
req.Header.Set("x-tyk-hostname", hostDetails.Hostname)
return req
}
func (h *HTTPDashboardHandler) sendHeartBeat() error {
req := h.newRequest(h.HeartBeatEndpoint)
req.Header.Set("x-tyk-nodeid", NodeID)
req.Header.Set("x-tyk-nonce", ServiceNonce)
c := &http.Client{Timeout: 5 * time.Second}
resp, err := c.Do(req)
if err != nil || resp.StatusCode != 200 {
return errors.New("dashboard is down? Heartbeat is failing")
}
defer resp.Body.Close()
val := NodeResponseOK{}
if err := json.NewDecoder(resp.Body).Decode(&val); err != nil {
return err
}
// Set the nonce
ServiceNonce = val.Nonce
//log.Debug("Heartbeat Finished: Nonce Set: ", ServiceNonce)
return nil
}
func (h *HTTPDashboardHandler) DeRegister() error {
req := h.newRequest(h.DeRegistrationEndpoint)
req.Header.Set("x-tyk-nodeid", NodeID)
req.Header.Set("x-tyk-nonce", ServiceNonce)
c := &http.Client{Timeout: 5 * time.Second}
resp, err := c.Do(req)
if err != nil || resp.StatusCode != 200 {
return fmt.Errorf("request failed with code %d and error %v", resp.StatusCode, err)
}
defer resp.Body.Close()
val := NodeResponseOK{}
if err := json.NewDecoder(resp.Body).Decode(&val); err != nil {
return err
}
// Set the nonce
ServiceNonce = val.Nonce
log.Info("De-registered.")
return nil
}