Skip to content

Commit bcce435

Browse files
safe close
1 parent 9f24ffc commit bcce435

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

graphql/client.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"io"
1010
"net/http"
1111
"net/url"
12+
"reflect"
1213
"strings"
1314

1415
"github.com/vektah/gqlparser/v2/gqlerror"
@@ -370,3 +371,10 @@ func SafeSend[T any](dataChan_ chan T, wsResp T) {
370371
}()
371372
dataChan_ <- wsResp
372373
}
374+
375+
func safeClose(interfaceChan any) {
376+
defer func() {
377+
_ = recover()
378+
}()
379+
reflect.ValueOf(interfaceChan).Close()
380+
}

graphql/subscription.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,14 @@ func (s *subscriptionMap) Unsubscribe(subscriptionID string) error {
3737
if !success {
3838
return fmt.Errorf("tried to unsubscribe from unknown subscription with ID '%s'", subscriptionID)
3939
}
40+
hasBeenUnsubscribed := unsub.hasBeenUnsubscribed
4041
unsub.hasBeenUnsubscribed = true
4142
s.map_[subscriptionID] = unsub
43+
44+
if !hasBeenUnsubscribed {
45+
safeClose(unsub.interfaceChan)
46+
}
47+
4248
return nil
4349
}
4450

graphql/websocket.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ func (w *webSocketClient) forwardWebSocketData(message []byte) error {
146146
if sub == nil {
147147
return nil
148148
}
149+
149150
return sub.forwardDataFunc(sub.interfaceChan, wsMsg.Payload)
150151
}
151152

0 commit comments

Comments
 (0)