Skip to content

Commit 27c7dba

Browse files
committed
Add keep alive pings to the server
1 parent 4b3b81b commit 27c7dba

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

api.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -329,13 +329,22 @@ func (a *API) connectToWS(idx int) {
329329
a.WebsocketRespLock.Unlock()
330330

331331
go func(ws *chan []byte) {
332+
// Send keep alive messages every 20 seconds
333+
// There is at least one proxy service in our cluster that closes the connection after 30 seconds of inactivity
334+
// This is to prevent the connection beeing closed
335+
keepAliveTicker := time.NewTicker(time.Second * 20)
336+
keepAliveBody := []byte("pnig")
332337
for {
333-
// TODO: if the response fails to send data might get lost.
334-
// It would be nice if the response is retried when WriteMessage fails
335-
resp := <-a.WebsocketResp[idx]
336-
err := c.WriteMessage(1, resp)
337-
if err != nil {
338-
fmt.Println("unable to write ws response:", err)
338+
select {
339+
case <-keepAliveTicker.C:
340+
c.WriteMessage(websocket.PingMessage, keepAliveBody)
341+
case resp := <-a.WebsocketResp[idx]:
342+
// TODO: if the response fails to send data might get lost.
343+
// It would be nice if the response is retried when WriteMessage fails
344+
err := c.WriteMessage(1, resp)
345+
if err != nil {
346+
fmt.Println("unable to write ws response:", err)
347+
}
339348
}
340349
}
341350
}(listenChan)

0 commit comments

Comments
 (0)