Skip to content

Commit cd5d2bc

Browse files
recover from panic
1 parent 2523e2a commit cd5d2bc

File tree

6 files changed

+36
-19
lines changed

6 files changed

+36
-19
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-
dataChan_ <- wsResp
77+
graphql.WriteToChannelOrRecover(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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,3 +363,10 @@ func (c *client) createGetRequest(req *Request) (*http.Request, error) {
363363

364364
return httpReq, nil
365365
}
366+
367+
func WriteToChannelOrRecover[T any](dataChan_ chan T, wsResp T) {
368+
defer func() {
369+
_ = recover()
370+
}()
371+
dataChan_ <- wsResp
372+
}

graphql/websocket.go

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,26 @@ func (w *webSocketClient) listenWebSocket() {
131131
}
132132
}
133133

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+
134154
func (w *webSocketClient) forwardWebSocketData(message []byte) error {
135155
var wsMsg webSocketReceiveMessage
136156
err := json.Unmarshal(message, &wsMsg)
@@ -140,22 +160,13 @@ func (w *webSocketClient) forwardWebSocketData(message []byte) error {
140160
if wsMsg.ID == "" { // e.g. keep-alive messages
141161
return nil
142162
}
143-
w.subscriptions.Lock()
144-
defer w.subscriptions.Unlock()
145-
sub, success := w.subscriptions.map_[wsMsg.ID]
146-
if !success {
147-
return fmt.Errorf("received message for unknown subscription ID '%s'", wsMsg.ID)
148-
}
149-
if sub.hasBeenUnsubscribed {
150-
return nil
163+
sub, err := w.getSubscriptionOrHandleComplete(wsMsg.ID, wsMsg.Type)
164+
if err != nil {
165+
return err
151166
}
152-
if wsMsg.Type == webSocketTypeComplete {
153-
sub.hasBeenUnsubscribed = true
154-
w.subscriptions.map_[wsMsg.ID] = sub
155-
reflect.ValueOf(sub.interfaceChan).Close()
167+
if sub == nil {
156168
return nil
157169
}
158-
159170
return sub.forwardDataFunc(sub.interfaceChan, wsMsg.Payload)
160171
}
161172

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.

internal/integration/server/server.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,6 @@ func countTo(ctx context.Context, stopCount int) (<-chan int, error) {
164164
for counter := range stopCount {
165165
select {
166166
case <-ctx.Done():
167-
fmt.Println("ctx done:", ctx.Err())
168167
return
169168
default:
170169
respChan <- counter

0 commit comments

Comments
 (0)