Skip to content

Commit

Permalink
revert back to less aggressive ping timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
scottfeldman committed Dec 12, 2024
1 parent 8feab88 commit a5054ea
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions pkg/device/ws.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,18 @@ func (l *wsLink) Close() {
l.conn.Close()
}

var wsPingPeriod = 5 * time.Second
var pingDuration = 4 * time.Second

// var pingTimeout = pingDuration + time.Second
// TODO allow two ping periods before timing out, rather than one, to
// workaround some issue I'm having deploying to cloud where ping (or pong)
// packet's are getting buffered and timing out.
var pingTimeout = 2*pingDuration + time.Second

func (l *wsLink) setPongHandler() {
l.conn.SetReadDeadline(time.Now().Add(wsPingPeriod + 2*time.Second))
l.conn.SetReadDeadline(time.Now().Add(pingTimeout))
l.conn.SetPongHandler(func(appData string) error {
l.conn.SetReadDeadline(time.Now().Add(wsPingPeriod + 2*time.Second))
l.conn.SetReadDeadline(time.Now().Add(pingTimeout))
//LogInfo("Pong received, read deadline extended")
return nil
})
Expand All @@ -50,15 +56,15 @@ func (l *wsLink) setPongHandler() {
func (l *wsLink) startPing() {
go func() {
for {
time.Sleep(wsPingPeriod)
l.Lock()
if err := l.conn.WriteMessage(websocket.PingMessage, nil); err != nil {
LogInfo("Ping error:", "err", err)
//LogError("Ping error:", "err", err)
l.Unlock()
return
}
l.Unlock()
//LogInfo("Ping sent")
l.Unlock()
time.Sleep(pingDuration)
}
}()
}
Expand Down

0 comments on commit a5054ea

Please sign in to comment.