From 48aa45cb39ed78c5bee391e8c7043078963eb0c2 Mon Sep 17 00:00:00 2001 From: Toan Nguyen Date: Sun, 7 Jan 2024 00:00:01 +0700 Subject: [PATCH] fix: handle closed error on subscription (#119) --- subscription.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/subscription.go b/subscription.go index e34d6c9..0318fc1 100644 --- a/subscription.go +++ b/subscription.go @@ -6,6 +6,7 @@ import ( "errors" "fmt" "io" + "net" "net/http" "strconv" "strings" @@ -293,6 +294,9 @@ func (sc *SubscriptionContext) Close() error { sc.Cancel() + if errors.Is(err, net.ErrClosed) { + return nil + } return err } @@ -767,7 +771,7 @@ func (sc *SubscriptionClient) Run() error { var message OperationMessage if err := conn.ReadJSON(&message); err != nil { // manual EOF check - if err == io.EOF || strings.Contains(err.Error(), "EOF") || strings.Contains(err.Error(), "connection reset by peer") { + if err == io.EOF || strings.Contains(err.Error(), "EOF") || errors.Is(err, net.ErrClosed) || strings.Contains(err.Error(), "connection reset by peer") { sc.errorChan <- errRetry return } @@ -922,7 +926,7 @@ func (sc *SubscriptionClient) close(ctx *SubscriptionContext) (err error) { continue } if sub.status == SubscriptionRunning { - if err := sc.protocol.Unsubscribe(ctx, sub); err != nil { + if err := sc.protocol.Unsubscribe(ctx, sub); err != nil && !errors.Is(err, net.ErrClosed) { unsubscribeErrors[key] = err } }