fix(server): suppress expected websocket close write errors
This commit is contained in:
+13
-2
@@ -214,11 +214,22 @@ func (sess *Session) writeWSFrame(mt int, msg any) error {
|
|||||||
}
|
}
|
||||||
reason := classifyWSError(err)
|
reason := classifyWSError(err)
|
||||||
immetrics.ObserveWSWrite(frame, "error", reason, started, payloadBytes)
|
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",
|
if shouldLogWSWriteError(reason) {
|
||||||
sess.sid, sess.uid.UserId(), frame, len(sess.send), cap(sess.send), reason, err)
|
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
|
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 {
|
func classifyWSError(err error) string {
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return "none"
|
return "none"
|
||||||
|
|||||||
@@ -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) {
|
func TestWebSocketOutboundQueueTiming(t *testing.T) {
|
||||||
sess := &Session{proto: WEBSOCK, send: make(chan any, 2)}
|
sess := &Session{proto: WEBSOCK, send: make(chan any, 2)}
|
||||||
payload := []byte("message")
|
payload := []byte("message")
|
||||||
|
|||||||
Reference in New Issue
Block a user