Skip to content

Commit

Permalink
fix: update websocket handlers to eliminate goroutine leaks (#796) (#797
Browse files Browse the repository at this point in the history
)

Hi team,

Please see the suggested fixes to address the issue I raised:
#796

After this change, the websocket handlers and their goroutines are
closing as expected. As a result, my memory usage is much more lower and
no longer leaking.

![image](https://github.com/wundergraph/graphql-go-tools/assets/53241741/b2a6667f-6fc7-48f4-b20f-afd2fdca2220)

Thanks,
Benny

Co-authored-by: Aenimus <47415099+Aenimus@users.noreply.github.com>
  • Loading branch information
BenjaminYong and Aenimus authored Jun 4, 2024
1 parent 155cdab commit 34ad449
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
14 changes: 10 additions & 4 deletions pkg/engine/datasource/graphql_datasource/graphql_ws_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,18 @@ func (h *gqlWSConnectionHandler) readBlocking(ctx context.Context, dataCh chan [
for {
msgType, data, err := h.conn.Read(ctx)
if ctx.Err() != nil {
errCh <- ctx.Err()
return
select {
case errCh <- ctx.Err():
case <-ctx.Done():
return
}
}
if err != nil {
errCh <- err
return
select {
case errCh <- err:
case <-ctx.Done():
return
}
}
if msgType != websocket.MessageText {
continue
Expand Down
3 changes: 3 additions & 0 deletions pkg/subscription/legacy_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,9 @@ func (h *Handler) handleKeepAlive(ctx context.Context) {
case <-ctx.Done():
return
case <-time.After(h.keepAliveInterval):
if !h.client.IsConnected() {
return
}
h.sendKeepAlive()
}
}
Expand Down

0 comments on commit 34ad449

Please sign in to comment.