@@ -31,8 +31,11 @@ type HostsController struct {
31
31
Drivers map [string ]* driver.Driver
32
32
// ReadOnlyHosts : restrict pinging every other server except these
33
33
ReadOnlyHosts []string
34
- Client chan * Client
35
- Received chan * ReceiveMessage
34
+ // ClientConnected : shows that a client is connected
35
+ ClientConnected bool
36
+ Client chan * Client
37
+ Received chan * ReceiveMessage
38
+ StopPolling chan bool
36
39
}
37
40
38
41
func (hosts * HostsController ) getDriver (address string ) * driver.Driver {
@@ -48,6 +51,18 @@ func (hosts *HostsController) resetDriver(host config.Host) {
48
51
hosts .Drivers [host .Address ] = & hostDriver
49
52
}
50
53
54
+ func (hosts * HostsController ) clientConnected () bool {
55
+ hosts .mu .Lock ()
56
+ defer hosts .mu .Unlock ()
57
+ return hosts .ClientConnected
58
+ }
59
+
60
+ func (hosts * HostsController ) setClientConnected (connected bool ) {
61
+ hosts .mu .Lock ()
62
+ defer hosts .mu .Unlock ()
63
+ hosts .ClientConnected = connected
64
+ }
65
+
51
66
func (hosts * HostsController ) setReadOnlyHost (hostlist config.HostList ) {
52
67
hosts .mu .Lock ()
53
68
defer hosts .mu .Unlock ()
@@ -97,7 +112,7 @@ func (hosts *HostsController) sendMetric(host config.Host, client *Client) {
97
112
func (hosts * HostsController ) Poll (client * Client ) {
98
113
for {
99
114
for _ , host := range hosts .Info .Hosts {
100
- if config .Contains (hosts .ReadOnlyHosts , host ) {
115
+ if config .Contains (hosts .ReadOnlyHosts , host ) && hosts . clientConnected () {
101
116
go hosts .sendMetric (host , client )
102
117
}
103
118
}
@@ -117,6 +132,8 @@ func (hosts *HostsController) Run() {
117
132
} else {
118
133
hosts .setReadOnlyHost ([]string {received .FilterBy })
119
134
}
135
+ case poll := <- hosts .StopPolling :
136
+ hosts .setClientConnected (! poll )
120
137
}
121
138
}
122
139
@@ -129,11 +146,13 @@ func (hosts *HostsController) ServeHTTP(w http.ResponseWriter, req *http.Request
129
146
return
130
147
}
131
148
client := & Client {
132
- Socket : socket ,
133
- Send : make (chan * SendMessage , messageBufferSize ),
134
- Received : hosts .Received ,
149
+ Socket : socket ,
150
+ Send : make (chan * SendMessage , messageBufferSize ),
151
+ Received : hosts .Received ,
152
+ StopHostPolling : hosts .StopPolling ,
135
153
}
136
154
hosts .Client <- client
155
+ hosts .StopPolling <- false
137
156
go client .Write ()
138
157
client .Read ()
139
158
}
@@ -142,11 +161,13 @@ func (hosts *HostsController) ServeHTTP(w http.ResponseWriter, req *http.Request
142
161
func NewHostsController (cfg * config.Config ) * HostsController {
143
162
dashboardInfo := config .GetDashboardInfoConfig (cfg )
144
163
hosts := & HostsController {
145
- Info : dashboardInfo ,
146
- Drivers : make (map [string ]* driver.Driver ),
147
- ReadOnlyHosts : dashboardInfo .GetAllHostAddresses (),
148
- Client : make (chan * Client ),
149
- Received : make (chan * ReceiveMessage ),
164
+ Info : dashboardInfo ,
165
+ Drivers : make (map [string ]* driver.Driver ),
166
+ ReadOnlyHosts : dashboardInfo .GetAllHostAddresses (),
167
+ Client : make (chan * Client ),
168
+ Received : make (chan * ReceiveMessage ),
169
+ StopPolling : make (chan bool ),
170
+ ClientConnected : true ,
150
171
}
151
172
return hosts
152
173
}
0 commit comments