-
Notifications
You must be signed in to change notification settings - Fork 0
/
service_watcher.go
206 lines (173 loc) · 4.52 KB
/
service_watcher.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
200
201
202
203
204
205
206
package main
import (
"github.com/cameliot/alpaca"
"github.com/cameliot/alpaca/meta"
"log"
"strings"
"time"
)
type Manifest struct {
Name string `json:"name"`
Handle string `json:"handle"`
Version string `json:"version"`
Description string `json:"description"`
StartedAt time.Time `json:"started_at"`
}
type GroupReport struct {
LastRequest time.Time `json:"last_request"`
LastSuccess time.Time `json:"last_success"`
LastError time.Time `json:"last_error"`
TimeToReponse int64 `json:"time_to_response_us"`
}
type ServiceReport struct {
Manifest Manifest `json:"manifest"`
LastHeartbeat time.Time `json:"last_heartbeat"`
Groups map[string]GroupReport `json:"groups"`
}
type WatchGroup struct {
config *WatchConfig
lastRequest time.Time
lastSuccess time.Time
lastError time.Time
lastResponse time.Time
responseTime time.Duration
}
/*
Update times matching the configured action mapping
*/
func (self *WatchGroup) Update(handle string) {
// Update times
switch handle {
case self.config.Request:
self.lastRequest = time.Now().UTC()
break
case self.config.Success:
self.lastSuccess = time.Now().UTC()
break
case self.config.Error:
self.lastError = time.Now().UTC()
}
// Update response time:
// Only recalculate response time if the last
// Success or Error was before a request.
//
// This prevents updating the response time with an incorrect
// (as in too high value) if another Success / Error was dispatched
// not as a direct response, but as an 'event' generated by
// external input or the service.
if self.lastRequest.After(self.lastResponse) {
// Update last response time
lastResponse := self.lastSuccess
if self.lastError.After(self.lastSuccess) {
lastResponse = self.lastError
}
// Calculate response time
self.responseTime = lastResponse.Sub(self.lastRequest)
self.lastResponse = lastResponse
}
}
type ServiceWatcher struct {
config *ServiceConfig
iama meta.IamaPayload
lastHeartbeat time.Time
watchGroups map[string]*WatchGroup
}
/*
Initialize new service watcher
*/
func NewServiceWatcher(config *ServiceConfig, dispatch alpaca.Dispatch) *ServiceWatcher {
// Create watch groups
watchGroups := map[string]*WatchGroup{}
for handle, watchConfig := range config.Watches {
watchGroups[handle] = &WatchGroup{
config: watchConfig,
}
}
watcher := &ServiceWatcher{
config: config,
watchGroups: watchGroups,
}
// Periodically request WHOIS information from
// this service
go func() {
for {
dispatch(meta.Whois(config.Handle))
time.Sleep(1 * time.Minute)
}
}()
return watcher
}
/*
Handle incoming actions and update watch groups
*/
func (self *ServiceWatcher) Handle(action alpaca.Action) {
if action.Type == meta.PONG {
self.handlePong(action)
return
} else if action.Type == meta.IAMA {
self.handleIama(action)
return
}
// Handle all other incoming actions, matching
// this service
if !strings.HasPrefix(action.Type, "@"+self.config.Handle) {
return // Not our concern
}
// Get action handle
tokens := strings.Split(action.Type, "/")
handle := tokens[len(tokens)-1]
for _, group := range self.watchGroups {
group.Update(handle)
}
}
func (self *ServiceWatcher) handlePong(action alpaca.Action) {
// Decode Payload
pong := meta.DecodePong(action)
if pong.Handle != self.config.Handle {
return // Not our concern
}
// Update heartbeat
self.lastHeartbeat = pong.Timestamp()
log.Println(
"Received Heartbeat for", pong.Handle,
":", pong.Timestamp(),
)
}
func (self *ServiceWatcher) handleIama(action alpaca.Action) {
// Decode Payload
iama := meta.DecodeIama(action)
if iama.Handle != self.config.Handle {
return // Not our concern
}
log.Println("Received Service Manifest for:", iama.Handle)
self.iama = iama
}
/*
Generate service report
*/
func (self *ServiceWatcher) Report() ServiceReport {
groupsReport := map[string]GroupReport{}
for name, group := range self.watchGroups {
// Make report
groupsReport[name] = GroupReport{
LastRequest: group.lastRequest,
LastSuccess: group.lastSuccess,
LastError: group.lastError,
TimeToReponse: int64(group.responseTime) / 1000,
}
}
// Decode iama
manifest := Manifest{
Name: self.iama.Name,
Handle: self.iama.Handle,
Version: self.iama.Version,
Description: self.iama.Description,
StartedAt: self.iama.StartedAt(),
}
report := ServiceReport{
LastHeartbeat: self.lastHeartbeat,
Manifest: manifest,
Groups: groupsReport,
}
return report
}