bigbluebutton-Github/bbb-graphql-middleware/internal/websrv/writer/writer.go
Gustavo Trott 3c71aab4bc
refactor (gql-middlware): Refactored code to simplify channel handling and remove unnecessary routines (#20640)
* Refactor gql-middleware channels and remove dedicated channel to establish init connection

* remove unnecessary routin to wait mutations to complete before invalidate Hasura connection
2024-07-05 13:35:08 -03:00

58 lines
1.6 KiB
Go

package writer
import (
"bytes"
"encoding/json"
"github.com/iMDT/bbb-graphql-middleware/internal/common"
log "github.com/sirupsen/logrus"
"nhooyr.io/websocket"
"sync"
)
func BrowserConnectionWriter(
browserConnection *common.BrowserConnection,
wg *sync.WaitGroup) {
log := log.WithField("_routine", "BrowserConnectionWriter").WithField("browserConnectionId", browserConnection.Id)
defer log.Debugf("finished")
log.Debugf("starting")
defer wg.Done()
RangeLoop:
for {
select {
case <-browserConnection.Context.Done():
log.Debug("Browser context cancelled.")
break RangeLoop
case toBrowserMessage := <-browserConnection.FromHasuraToBrowserChannel.ReceiveChannel():
{
if toBrowserMessage == nil {
if browserConnection.FromHasuraToBrowserChannel.Closed() {
break RangeLoop
}
continue
}
log.Tracef("sending to browser: %s", string(toBrowserMessage))
err := browserConnection.Websocket.Write(browserConnection.Context, websocket.MessageText, toBrowserMessage)
if err != nil {
log.Debugf("Browser is disconnected, skipping writing of ws message: %v", err)
return
}
// After the error is sent to client, close its connection
// Authentication hook unauthorized this request
if bytes.Contains(toBrowserMessage, []byte("connection_error")) {
type HasuraMessage struct {
Type string `json:"type"`
}
var hasuraMessage HasuraMessage
_ = json.Unmarshal(toBrowserMessage, &hasuraMessage)
if hasuraMessage.Type == "connection_error" {
_ = browserConnection.Websocket.Close(websocket.StatusInternalError, string(toBrowserMessage))
}
}
}
}
}
}