Skip to content

Commit

Permalink
Handle websocket data channel closing
Browse files Browse the repository at this point in the history
  • Loading branch information
HaraldNordgren committed Sep 27, 2024
1 parent 4466fc1 commit 9473b93
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
8 changes: 8 additions & 0 deletions generate/operation.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ type {{.Name}}WsResponse struct {
func {{.Name}}ForwardData(interfaceChan interface{}, jsonRawMsg json.RawMessage) error {
var gqlResp graphql.Response
var wsResp {{.Name}}WsResponse
if len(jsonRawMsg) == 0 {
dataChan_, ok := interfaceChan.(chan {{.Name}}WsResponse)
if !ok {
return errors.New("failed to cast interface into 'chan {{.Name}}WsResponse'")
}
close(dataChan_)
return nil
}
err := json.Unmarshal(jsonRawMsg, &gqlResp)
if err != nil {
return err
Expand Down
8 changes: 8 additions & 0 deletions internal/integration/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion internal/integration/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,15 @@ func (m mutationResolver) CreateUser(ctx context.Context, input NewUser) (*User,
func (s *subscriptionResolver) Count(ctx context.Context) (<-chan int, error) {
respChan := make(chan int, 1)
go func(respChan chan int) {
defer close(respChan)
counter := 0
for {
if counter == 3 {
return
}
respChan <- counter
counter++
time.Sleep(time.Second)
time.Sleep(10 * time.Millisecond)
}
}(respChan)
return respChan, nil
Expand Down

0 comments on commit 9473b93

Please sign in to comment.