Skip to content

Commit 9f24ffc

Browse files
refactor
1 parent f9a5e5d commit 9f24ffc

File tree

6 files changed

+28
-28
lines changed

6 files changed

+28
-28
lines changed

generate/operation.go.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func {{.Name}}ForwardData(interfaceChan interface{}, jsonRawMsg json.RawMessage)
7474
if !ok {
7575
return errors.New("failed to cast interface into 'chan {{.Name}}WsResponse'")
7676
}
77-
graphql.WriteToChannelOrRecover(dataChan_, wsResp)
77+
graphql.SafeSend(dataChan_, wsResp)
7878
return nil
7979
}
8080
{{end}}

generate/testdata/snapshots/TestGenerate-SimpleSubscription.graphql-SimpleSubscription.graphql.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

graphql/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ func (c *client) createGetRequest(req *Request) (*http.Request, error) {
364364
return httpReq, nil
365365
}
366366

367-
func WriteToChannelOrRecover[T any](dataChan_ chan T, wsResp T) {
367+
func SafeSend[T any](dataChan_ chan T, wsResp T) {
368368
defer func() {
369369
_ = recover()
370370
}()

graphql/subscription.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package graphql
22

33
import (
44
"fmt"
5+
"reflect"
56
"sync"
67
)
78

@@ -55,3 +56,23 @@ func (s *subscriptionMap) Delete(subscriptionID string) {
5556
defer s.Unlock()
5657
delete(s.map_, subscriptionID)
5758
}
59+
60+
func (s *subscriptionMap) GetOrClose(subscriptionID string, subscriptionType string) (*subscription, error) {
61+
s.Lock()
62+
defer s.Unlock()
63+
sub, success := s.map_[subscriptionID]
64+
if !success {
65+
return nil, fmt.Errorf("received message for unknown subscription ID '%s'", subscriptionID)
66+
}
67+
if sub.hasBeenUnsubscribed {
68+
return nil, nil
69+
}
70+
if subscriptionType == webSocketTypeComplete {
71+
sub.hasBeenUnsubscribed = true
72+
s.map_[subscriptionID] = sub
73+
reflect.ValueOf(sub.interfaceChan).Close()
74+
return nil, nil
75+
}
76+
77+
return &sub, nil
78+
}

graphql/websocket.go

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"encoding/json"
77
"fmt"
88
"net/http"
9-
"reflect"
109
"strings"
1110
"sync"
1211
"sync/atomic"
@@ -131,26 +130,6 @@ func (w *webSocketClient) listenWebSocket() {
131130
}
132131
}
133132

134-
func (w *webSocketClient) getSubscriptionOrHandleComplete(subscriptionID string, subscriptionType string) (*subscription, error) {
135-
w.subscriptions.Lock()
136-
defer w.subscriptions.Unlock()
137-
sub, success := w.subscriptions.map_[subscriptionID]
138-
if !success {
139-
return nil, fmt.Errorf("received message for unknown subscription ID '%s'", subscriptionID)
140-
}
141-
if sub.hasBeenUnsubscribed {
142-
return nil, nil
143-
}
144-
if subscriptionType == webSocketTypeComplete {
145-
sub.hasBeenUnsubscribed = true
146-
w.subscriptions.map_[subscriptionID] = sub
147-
reflect.ValueOf(sub.interfaceChan).Close()
148-
return nil, nil
149-
}
150-
151-
return &sub, nil
152-
}
153-
154133
func (w *webSocketClient) forwardWebSocketData(message []byte) error {
155134
var wsMsg webSocketReceiveMessage
156135
err := json.Unmarshal(message, &wsMsg)
@@ -160,7 +139,7 @@ func (w *webSocketClient) forwardWebSocketData(message []byte) error {
160139
if wsMsg.ID == "" { // e.g. keep-alive messages
161140
return nil
162141
}
163-
sub, err := w.getSubscriptionOrHandleComplete(wsMsg.ID, wsMsg.Type)
142+
sub, err := w.subscriptions.GetOrClose(wsMsg.ID, wsMsg.Type)
164143
if err != nil {
165144
return err
166145
}

internal/integration/generated.go

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)