Skip to content

Commit 16817d2

Browse files
committed
Stop polling metrics on websocket disconnect
1 parent 6f8fcda commit 16817d2

File tree

2 files changed

+41
-14
lines changed

2 files changed

+41
-14
lines changed

client/client.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ import (
66
)
77

88
type Client struct {
9-
Socket *websocket.Conn
10-
Send chan *SendMessage
11-
Received chan *ReceiveMessage
9+
Socket *websocket.Conn
10+
Send chan *SendMessage
11+
Received chan *ReceiveMessage
12+
StopHostPolling chan bool
1213
}
1314

1415
// Write to websocket
@@ -19,6 +20,9 @@ func (client *Client) Write() {
1920
err = client.Socket.WriteJSON(msg)
2021
if err != nil {
2122
log.Error("Error inside client write ", err)
23+
// most likely socket connection has been closed so
24+
// just return
25+
return
2226
}
2327
}
2428
}
@@ -31,6 +35,8 @@ func (client *Client) Read() {
3135
err := client.Socket.ReadJSON(&message)
3236
if err != nil {
3337
log.Errorf("While reading from client: %s", err)
38+
client.StopHostPolling <- true
39+
return
3440
} else {
3541
client.Received <- message
3642
}

client/controller.go

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,11 @@ type HostsController struct {
3131
Drivers map[string]*driver.Driver
3232
// ReadOnlyHosts : restrict pinging every other server except these
3333
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
3639
}
3740

3841
func (hosts *HostsController) getDriver(address string) *driver.Driver {
@@ -48,6 +51,18 @@ func (hosts *HostsController) resetDriver(host config.Host) {
4851
hosts.Drivers[host.Address] = &hostDriver
4952
}
5053

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+
5166
func (hosts *HostsController) setReadOnlyHost(hostlist config.HostList) {
5267
hosts.mu.Lock()
5368
defer hosts.mu.Unlock()
@@ -97,7 +112,7 @@ func (hosts *HostsController) sendMetric(host config.Host, client *Client) {
97112
func (hosts *HostsController) Poll(client *Client) {
98113
for {
99114
for _, host := range hosts.Info.Hosts {
100-
if config.Contains(hosts.ReadOnlyHosts, host) {
115+
if config.Contains(hosts.ReadOnlyHosts, host) && hosts.clientConnected() {
101116
go hosts.sendMetric(host, client)
102117
}
103118
}
@@ -117,6 +132,8 @@ func (hosts *HostsController) Run() {
117132
} else {
118133
hosts.setReadOnlyHost([]string{received.FilterBy})
119134
}
135+
case poll := <-hosts.StopPolling:
136+
hosts.setClientConnected(!poll)
120137
}
121138
}
122139

@@ -129,11 +146,13 @@ func (hosts *HostsController) ServeHTTP(w http.ResponseWriter, req *http.Request
129146
return
130147
}
131148
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,
135153
}
136154
hosts.Client <- client
155+
hosts.StopPolling <- false
137156
go client.Write()
138157
client.Read()
139158
}
@@ -142,11 +161,13 @@ func (hosts *HostsController) ServeHTTP(w http.ResponseWriter, req *http.Request
142161
func NewHostsController(cfg *config.Config) *HostsController {
143162
dashboardInfo := config.GetDashboardInfoConfig(cfg)
144163
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,
150171
}
151172
return hosts
152173
}

0 commit comments

Comments
 (0)