forked from mijia/adoc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonitors.go
103 lines (93 loc) · 2.64 KB
/
monitors.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
package adoc
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
)
func (client *DockerClient) StopMonitor(monitorId int64) {
client.monitorLock.Lock()
defer client.monitorLock.Unlock()
delete(client.monitors, monitorId)
}
func (client *DockerClient) newMonitorItem() int64 {
client.monitorLock.Lock()
defer client.monitorLock.Unlock()
var monitorId int64
for trial := 5; trial > 0; trial -= 1 {
monitorId = random.Int63()
if _, ok := client.monitors[monitorId]; !ok {
client.monitors[monitorId] = struct{}{}
break
}
}
// we have some change to conflict, but I think maybe we live with that
return monitorId
}
type EventCallback func(event Event, err error)
func (client *DockerClient) MonitorEvents(filters string, callback EventCallback) int64 {
v := url.Values{}
if filters != "" {
v.Set("filters", filters)
}
uri := "events"
if len(v) > 0 {
uri += "?" + v.Encode()
}
monitorId := client.newMonitorItem()
go client.monitorEvents(monitorId, uri, callback)
return monitorId
}
// will be running inside a goroutine
func (client *DockerClient) monitorEvents(monitorId int64, uri string, callback EventCallback) {
err := client.sendRequestCallback("GET", uri, nil, nil, func(resp *http.Response) error {
decoder := json.NewDecoder(resp.Body)
client.monitorLock.RLock()
_, toContinue := client.monitors[monitorId]
client.monitorLock.RUnlock()
for toContinue {
var event Event
if err := decoder.Decode(&event); err != nil {
return err
}
callback(event, nil)
client.monitorLock.RLock()
_, toContinue = client.monitors[monitorId]
client.monitorLock.RUnlock()
}
return nil
}, true)
if err != nil && err != io.EOF {
callback(Event{}, err)
}
}
type StatsCallback func(stats Stats, err error)
func (client *DockerClient) MonitorStats(containerId string, callback StatsCallback) int64 {
uri := fmt.Sprintf("containers/%s/stats", containerId)
monitorId := client.newMonitorItem()
go client.monitorStats(monitorId, uri, callback)
return monitorId
}
func (client *DockerClient) monitorStats(monitorId int64, uri string, callback StatsCallback) {
err := client.sendRequestCallback("GET", uri, nil, nil, func(resp *http.Response) error {
decoder := json.NewDecoder(resp.Body)
client.monitorLock.RLock()
_, toContinue := client.monitors[monitorId]
client.monitorLock.RUnlock()
for toContinue {
var stats Stats
if err := decoder.Decode(&stats); err != nil {
return err
}
callback(stats, nil)
client.monitorLock.RLock()
_, toContinue = client.monitors[monitorId]
client.monitorLock.RUnlock()
}
return nil
}, true)
if err != nil && err != io.EOF {
callback(Stats{}, err)
}
}