-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathwebsocket.go
More file actions
69 lines (60 loc) · 1.95 KB
/
websocket.go
File metadata and controls
69 lines (60 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package httpapi
import (
"context"
"errors"
"net"
"time"
"golang.org/x/xerrors"
"cdr.dev/slog/v3"
"github.com/coder/quartz"
"github.com/coder/websocket"
)
const HeartbeatInterval time.Duration = 15 * time.Second
// HeartbeatClose loops to ping a WebSocket to keep it alive.
// It calls `exit` on ping failure.
func HeartbeatClose(ctx context.Context, logger slog.Logger, exit func(), conn *websocket.Conn) {
heartbeatCloseWith(ctx, logger, exit, conn, quartz.NewReal(), HeartbeatInterval)
}
func heartbeatCloseWith(ctx context.Context, logger slog.Logger, exit func(), conn *websocket.Conn, clk quartz.Clock, interval time.Duration) {
ticker := clk.NewTicker(interval, "HeartbeatClose")
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
}
err := pingWithTimeout(ctx, conn, interval)
if err != nil {
// These errors are all expected during normal connection
// teardown and should not be logged at error level:
// - context.DeadlineExceeded: client disconnected
// without sending a close frame.
// - context.Canceled: request context was canceled.
// - net.ErrClosed: connection was already closed by
// another goroutine (e.g. handler returned).
// - websocket.CloseError: a close frame was
// received or sent.
if errors.Is(err, context.DeadlineExceeded) ||
errors.Is(err, context.Canceled) ||
errors.Is(err, net.ErrClosed) ||
websocket.CloseStatus(err) != -1 {
logger.Debug(ctx, "heartbeat ping stopped", slog.Error(err))
} else {
logger.Error(ctx, "failed to heartbeat ping", slog.Error(err))
}
_ = conn.Close(websocket.StatusGoingAway, "Ping failed")
exit()
return
}
}
}
func pingWithTimeout(ctx context.Context, conn *websocket.Conn, timeout time.Duration) error {
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
err := conn.Ping(ctx)
if err != nil {
return xerrors.Errorf("failed to ping: %w", err)
}
return nil
}