correct handling of pings and pongs

server:
- always has read & write timeouts
- ping handler never blocks the reader (see A)
- sends regular pings to check up on a client

A:
at some point server write buffer can become full, so in order not to
block reads from a client (see
https://github.com/gorilla/websocket/issues/97), server may skip some
pongs. As a result, client may disconnect. But you either have to do
that or block the reader. There is no third way.

client:
- optional read & write timeouts
- optional ping/pong to measure latency
This commit is contained in:
Anton Kaliaev
2017-08-10 17:39:38 -04:00
parent 236489aecf
commit 2fd8496bc1
5 changed files with 152 additions and 124 deletions

View File

@ -31,8 +31,6 @@ const (
unixAddr = "unix://" + unixSocket
websocketEndpoint = "/websocket/endpoint"
testPongWait = 2 * time.Second
)
type ResultEcho struct {
@ -115,7 +113,7 @@ func setup() {
tcpLogger := logger.With("socket", "tcp")
mux := http.NewServeMux()
server.RegisterRPCFuncs(mux, Routes, tcpLogger)
wm := server.NewWebsocketManager(Routes, nil, server.PingPong((testPongWait*9)/10, testPongWait))
wm := server.NewWebsocketManager(Routes, nil, server.ReadWait(5*time.Second), server.PingPeriod(1*time.Second))
wm.SetLogger(tcpLogger)
mux.HandleFunc(websocketEndpoint, wm.WebsocketHandler)
go func() {
@ -364,7 +362,7 @@ func TestWSClientPingPong(t *testing.T) {
require.Nil(t, err)
defer cl.Stop()
time.Sleep((testPongWait * 11) / 10)
time.Sleep(3 * time.Second)
}
func randBytes(t *testing.T) []byte {