rpc: set pong read deadline (#23556)

This PR adds a 30s timeout for the remote part to answer a ping message, thus detecting (silent) disconnnects
This commit is contained in:
Jordan Krage
2021-09-27 09:31:45 -05:00
committed by GitHub
parent 57a3fab8a7
commit 51ececb64e
2 changed files with 95 additions and 0 deletions

View File

@ -37,6 +37,7 @@ const (
wsWriteBuffer = 1024
wsPingInterval = 60 * time.Second
wsPingWriteTimeout = 5 * time.Second
wsPongTimeout = 30 * time.Second
wsMessageSizeLimit = 15 * 1024 * 1024
)
@ -241,6 +242,10 @@ type websocketCodec struct {
func newWebsocketCodec(conn *websocket.Conn) ServerCodec {
conn.SetReadLimit(wsMessageSizeLimit)
conn.SetPongHandler(func(appData string) error {
conn.SetReadDeadline(time.Time{})
return nil
})
wc := &websocketCodec{
jsonCodec: NewFuncCodec(conn, conn.WriteJSON, conn.ReadJSON).(*jsonCodec),
conn: conn,
@ -287,6 +292,7 @@ func (wc *websocketCodec) pingLoop() {
wc.jsonCodec.encMu.Lock()
wc.conn.SetWriteDeadline(time.Now().Add(wsPingWriteTimeout))
wc.conn.WriteMessage(websocket.PingMessage, nil)
wc.conn.SetReadDeadline(time.Now().Add(wsPongTimeout))
wc.jsonCodec.encMu.Unlock()
timer.Reset(wsPingInterval)
}