From 00b05974ff9f2a26c17598e5cbdab664b27fd84e Mon Sep 17 00:00:00 2001 From: otherview Date: Fri, 20 Dec 2024 11:35:55 +0000 Subject: [PATCH] adding thorclient.Subscription.Unsubscribe errors --- thorclient/common/common.go | 2 +- thorclient/wsclient/client.go | 13 ++++++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/thorclient/common/common.go b/thorclient/common/common.go index 3bb9b0992..92500ef87 100644 --- a/thorclient/common/common.go +++ b/thorclient/common/common.go @@ -29,5 +29,5 @@ type EventWrapper[T any] struct { // Subscription is used to handle the active subscription type Subscription[T any] struct { EventChan <-chan EventWrapper[T] - Unsubscribe func() + Unsubscribe func() error } diff --git a/thorclient/wsclient/client.go b/thorclient/wsclient/client.go index 9eb1519ab..c987704e3 100644 --- a/thorclient/wsclient/client.go +++ b/thorclient/wsclient/client.go @@ -183,10 +183,17 @@ func subscribe[T any](conn *websocket.Conn) *common.Subscription[*T] { return &common.Subscription[*T]{ EventChan: eventChan, - Unsubscribe: func() { + Unsubscribe: func() error { closed = true - conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, "")) - conn.Close() + err := conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, "")) + if err != nil { + return fmt.Errorf("failed to issue close message: %w", err) + } + err = conn.Close() + if err != nil { + return fmt.Errorf("failed to close connections: %w", err) + } + return nil }, } }