Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[chore] Fix loop issue in streaming 🤦 #3457

Merged
merged 1 commit into from
Oct 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions internal/api/client/streaming/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ func (m *Module) writeToWSConn(
// - receipt of msg
// - timeout of pingCtx
// - stream closed.
msg, gotMsg := stream.Recv(pingCtx)
msg, haveMsg := stream.Recv(pingCtx)

// If ping context has timed
// out, we should send a ping.
Expand All @@ -410,35 +410,38 @@ func (m *Module) writeToWSConn(
cncl()

switch {
case !haveMsg && !shouldPing:
// We have no message and we shouldn't
// send a ping; this means the stream
// has been closed from the client's end,
// so there's nothing further to do here.
l.Trace("no message and we shouldn't ping, returning...")
return

// We have a message to stream.
case gotMsg:
case haveMsg:
// We have a message to stream.
l.Tracef("writing websocket message: %+v", msg)

if err := wsConn.WriteJSON(msg); err != nil {
// If there's an error writing then drop the
// connection, as client may have disappeared
// suddenly; they can reconnect if necessary.
l.Debugf("error writing websocket message: %v", err)
break
return
}

// We have no message but we
// need to send a keep-alive ping.
case shouldPing:
// We have no message but we do
// need to send a keep-alive ping.
l.Trace("writing websocket ping")

if err := wsConn.WriteControl(websocket.PingMessage, pingMsg, time.Time{}); err != nil {
// If there's an error writing then drop the
// connection, as client may have disappeared
// suddenly; they can reconnect if necessary.
l.Debugf("error writing websocket ping: %v", err)
break
return
}

// We have no message and we shouldn't
// send a ping; this means the stream
// has been closed from the client's end.
default:
return
}
}
}