mirror of
https://github.com/coder/coder.git
synced 2026-06-03 04:58:23 +00:00
612aae2523
Relates to https://github.com/coder/coder/pull/21676 * Replaces all existing usages of `httpapi.Heartbeat` with `httpapi.HeartbeatClose` * Removes `httpapi.HeartbeatClose`
51 lines
1.2 KiB
Go
51 lines
1.2 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"time"
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
"cdr.dev/slog/v3"
|
|
"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) {
|
|
ticker := time.NewTicker(HeartbeatInterval)
|
|
defer ticker.Stop()
|
|
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
return
|
|
case <-ticker.C:
|
|
}
|
|
err := pingWithTimeout(ctx, conn, HeartbeatInterval)
|
|
if err != nil {
|
|
// context.DeadlineExceeded is expected when the client disconnects without sending a close frame
|
|
if !errors.Is(err, context.DeadlineExceeded) {
|
|
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
|
|
}
|