fix(server): suppress expected websocket close write errors

This commit is contained in:
richjohnrambo
2026-10-11 22:50:54 +08:00
parent 1e0b4c181d
commit c219ef6455
2 changed files with 38 additions and 2 deletions
+13 -2
View File
@@ -214,11 +214,22 @@ func (sess *Session) writeWSFrame(mt int, msg any) error {
}
reason := classifyWSError(err)
immetrics.ObserveWSWrite(frame, "error", reason, started, payloadBytes)
logs.Err.Printf("ws: writeLoop sid=%s uid=%s frame=%s queue=%d capacity=%d reason=%s err=%v",
sess.sid, sess.uid.UserId(), frame, len(sess.send), cap(sess.send), reason, err)
if shouldLogWSWriteError(reason) {
logs.Err.Printf("ws: writeLoop sid=%s uid=%s frame=%s queue=%d capacity=%d reason=%s err=%v",
sess.sid, sess.uid.UserId(), frame, len(sess.send), cap(sess.send), reason, err)
}
return err
}
func shouldLogWSWriteError(reason string) bool {
switch reason {
case "close_normal", "close_going_away", "closed", "eof", "broken_pipe", "connection_reset":
return false
default:
return true
}
}
func classifyWSError(err error) string {
if err == nil {
return "none"
+25
View File
@@ -37,6 +37,31 @@ func TestClassifyWSError(t *testing.T) {
}
}
func TestShouldLogWSWriteError(t *testing.T) {
tests := []struct {
reason string
want bool
}{
{reason: "close_normal", want: false},
{reason: "close_going_away", want: false},
{reason: "closed", want: false},
{reason: "eof", want: false},
{reason: "broken_pipe", want: false},
{reason: "connection_reset", want: false},
{reason: "timeout", want: true},
{reason: "close_abnormal", want: true},
{reason: "other", want: true},
}
for _, tc := range tests {
t.Run(tc.reason, func(t *testing.T) {
if got := shouldLogWSWriteError(tc.reason); got != tc.want {
t.Fatalf("shouldLogWSWriteError(%q)=%v want %v", tc.reason, got, tc.want)
}
})
}
}
func TestWebSocketOutboundQueueTiming(t *testing.T) {
sess := &Session{proto: WEBSOCK, send: make(chan any, 2)}
payload := []byte("message")