-
Notifications
You must be signed in to change notification settings - Fork 0
/
endpoints.go
95 lines (88 loc) · 2.16 KB
/
endpoints.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
package drovedns
import (
"sync"
"time"
)
type DroveEndpoints struct {
appsMutex *sync.RWMutex
AppsDB *DroveAppsResponse
DroveClient IDroveClient
AppsByVhost map[string]DroveApp
}
func (dr *DroveEndpoints) setApps(appDB *DroveAppsResponse) {
var appsByVhost map[string]DroveApp = make(map[string]DroveApp)
if appDB != nil {
for _, app := range appDB.Apps {
appsByVhost[app.Vhost+"."] = app
}
}
dr.appsMutex.Lock()
dr.AppsDB = appDB
dr.AppsByVhost = appsByVhost
dr.appsMutex.Unlock()
}
func (dr *DroveEndpoints) getApps() *DroveAppsResponse {
dr.appsMutex.RLock()
defer dr.appsMutex.RUnlock()
if dr.AppsDB == nil {
return nil
}
return dr.AppsDB
}
func (dr *DroveEndpoints) searchApps(questionName string) *DroveApp {
dr.appsMutex.RLock()
defer dr.appsMutex.RUnlock()
if dr.AppsByVhost == nil {
return nil
}
if app, ok := dr.AppsByVhost[questionName]; ok {
return &app
}
return nil
}
func newDroveEndpoints(client IDroveClient) *DroveEndpoints {
endpoints := DroveEndpoints{DroveClient: client, appsMutex: &sync.RWMutex{}}
ticker := time.NewTicker(10 * time.Second)
done := make(chan bool)
reload := make(chan bool)
endpoints.DroveClient.PollEvents(func(eventSummary *DroveEventSummary) {
if len(eventSummary.EventsCount) > 0 {
if _, ok := eventSummary.EventsCount["APP_STATE_CHANGE"]; ok {
log.Debugf("App State Change %+v", eventSummary.EventsCount["APP_STATE_CHANGE"])
reload <- true
return
}
if _, ok := eventSummary.EventsCount["INSTANCE_STATE_CHANGE"]; ok {
log.Debugf("Instance State Change %+v", eventSummary.EventsCount["INSTANCE_STATE_CHANGE"])
reload <- true
return
}
}
})
go func() {
var syncApp = func() {
DroveQueryTotal.Inc()
apps, err := endpoints.DroveClient.FetchApps()
if err != nil {
DroveQueryFailure.Inc()
log.Errorf("Error refreshing nodes data")
return
}
endpoints.setApps(apps)
}
syncApp()
for {
select {
case <-done:
return
case <-reload:
log.Debug("Refreshing Apps due to event change from drove")
syncApp()
case _ = <-ticker.C:
log.Debug("Refreshing Apps data from drove")
syncApp()
}
}
}()
return &endpoints
}