Introduce DefaultWriteTimeout for WebSocket operations, replace hardcoded timeouts, and upgrade version to v0.4.1.
Some checks failed
Go / build (push) Has been cancelled
Some checks failed
Go / build (push) Has been cancelled
This commit is contained in:
@@ -20,6 +20,7 @@ const (
|
|||||||
DefaultPongWait = 60 * time.Second
|
DefaultPongWait = 60 * time.Second
|
||||||
DefaultPingWait = DefaultPongWait / 2
|
DefaultPingWait = DefaultPongWait / 2
|
||||||
DefaultReadTimeout = 3 * time.Second // Read timeout to detect stalled connections
|
DefaultReadTimeout = 3 * time.Second // Read timeout to detect stalled connections
|
||||||
|
DefaultWriteTimeout = 3 * time.Second
|
||||||
DefaultMaxMessageSize = 1 * units.Mb
|
DefaultMaxMessageSize = 1 * units.Mb
|
||||||
|
|
||||||
// CloseMessage denotes a close control message. The optional message
|
// CloseMessage denotes a close control message. The optional message
|
||||||
@@ -140,9 +141,15 @@ whitelist:
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
if typ == PingMessage {
|
if typ == PingMessage {
|
||||||
if err = conn.Write(ctx, PongMessage, msg); chk.E(err) {
|
// Create a write context with timeout for pong response
|
||||||
|
writeCtx, writeCancel := context.WithTimeout(
|
||||||
|
ctx, DefaultWriteTimeout,
|
||||||
|
)
|
||||||
|
if err = conn.Write(writeCtx, PongMessage, msg); chk.E(err) {
|
||||||
|
writeCancel()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
writeCancel()
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
log.T.F("received message from %s: %s", remote, string(msg))
|
log.T.F("received message from %s: %s", remote, string(msg))
|
||||||
@@ -162,9 +169,13 @@ func (s *Server) Pinger(
|
|||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-ticker.C:
|
case <-ticker.C:
|
||||||
if err = conn.Ping(ctx); chk.E(err) {
|
// Create a write context with timeout for ping operation
|
||||||
|
pingCtx, pingCancel := context.WithTimeout(ctx, DefaultWriteTimeout)
|
||||||
|
if err = conn.Ping(pingCtx); chk.E(err) {
|
||||||
|
pingCancel()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
pingCancel()
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,15 +3,12 @@ package app
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/coder/websocket"
|
"github.com/coder/websocket"
|
||||||
"lol.mleku.dev/chk"
|
"lol.mleku.dev/chk"
|
||||||
"utils.orly/atomic"
|
"utils.orly/atomic"
|
||||||
)
|
)
|
||||||
|
|
||||||
const WriteTimeout = 10 * time.Second
|
|
||||||
|
|
||||||
type Listener struct {
|
type Listener struct {
|
||||||
*Server
|
*Server
|
||||||
conn *websocket.Conn
|
conn *websocket.Conn
|
||||||
@@ -31,7 +28,9 @@ func (l *Listener) Ctx() context.Context {
|
|||||||
func (l *Listener) Write(p []byte) (n int, err error) {
|
func (l *Listener) Write(p []byte) (n int, err error) {
|
||||||
// Use a separate context with timeout for writes to prevent race conditions
|
// Use a separate context with timeout for writes to prevent race conditions
|
||||||
// where the main connection context gets cancelled while writing events
|
// where the main connection context gets cancelled while writing events
|
||||||
writeCtx, cancel := context.WithTimeout(context.Background(), WriteTimeout)
|
writeCtx, cancel := context.WithTimeout(
|
||||||
|
context.Background(), DefaultWriteTimeout,
|
||||||
|
)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
if err = l.conn.Write(writeCtx, websocket.MessageText, p); chk.E(err) {
|
if err = l.conn.Write(writeCtx, websocket.MessageText, p); chk.E(err) {
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
v0.4.0
|
v0.4.1
|
||||||
Reference in New Issue
Block a user