Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[release-v1.7] rpcserver: Update websocket ping timeout handling. #2866

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 9 additions & 6 deletions internal/rpcserver/rpcserver.go
@@ -1,5 +1,5 @@
// Copyright (c) 2013-2016 The btcsuite developers
// Copyright (c) 2015-2021 The Decred developers
// Copyright (c) 2015-2022 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

Expand Down Expand Up @@ -6358,19 +6358,22 @@ func (s *Server) route(ctx context.Context) *http.Server {
return
}
ws.SetPingHandler(func(payload string) error {
err := ws.WriteControl(websocket.PongMessage, []byte(payload),
time.Now().Add(time.Second))
log.Debugf("ping received: len %d", len(payload))
log.Tracef("ping payload: %s", payload)
if err != nil {
log.Tracef("ping payload: %q", payload)
var netErr net.Error
err := ws.WriteControl(websocket.PongMessage, []byte(payload),
time.Now().Add(websocketPongTimeout))
if err != nil && !errors.Is(err, websocket.ErrCloseSent) &&
!(errors.As(err, &netErr) && netErr.Timeout()) {

log.Errorf("Failed to send pong: %v", err)
return err
}
return nil
})
ws.SetPongHandler(func(payload string) error {
log.Debugf("pong received: len %d", len(payload))
log.Tracef("pong payload: %s", payload)
log.Tracef("pong payload: %q", payload)
return nil
})
if !authenticated {
Expand Down
6 changes: 5 additions & 1 deletion internal/rpcserver/rpcwebsocket.go
@@ -1,5 +1,5 @@
// Copyright (c) 2013-2016 The btcsuite developers
// Copyright (c) 2015-2021 The Decred developers
// Copyright (c) 2015-2022 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

Expand Down Expand Up @@ -52,6 +52,10 @@ const (
// websocketReadLimitAuthenticated is the maximum number of bytes allowed
// for an authenticated JSON-RPC message read from a websocket client.
websocketReadLimitAuthenticated = 1 << 24 // 16 MiB

// websocketPongTimeout is the maximum amount of time attempts to respond to
// websocket ping messages with a pong will wait before giving up.
websocketPongTimeout = time.Second * 5
)

type semaphore chan struct{}
Expand Down