bigbluebutton-Github/bbb-graphql-middleware/internal/hasura/conn/reader/reader.go

199 lines
7.6 KiB
Go
Raw Normal View History

2023-04-27 09:03:40 +08:00
package reader
import (
"context"
"encoding/json"
"errors"
"fmt"
2023-04-27 09:03:40 +08:00
"github.com/iMDT/bbb-graphql-middleware/internal/common"
"github.com/iMDT/bbb-graphql-middleware/internal/hasura/retransmiter"
"github.com/iMDT/bbb-graphql-middleware/internal/msgpatch"
2023-04-27 09:03:40 +08:00
log "github.com/sirupsen/logrus"
"hash/crc32"
"nhooyr.io/websocket"
2023-04-27 09:03:40 +08:00
"nhooyr.io/websocket/wsjson"
"sync"
)
// HasuraConnectionReader consumes messages from Hasura connection and add send to the browser channel
func HasuraConnectionReader(
hc *common.HasuraConnection,
fromHasuraToBrowserChannel *common.SafeChannel,
fromBrowserToHasuraChannel *common.SafeChannel,
wg *sync.WaitGroup) {
2024-03-13 21:35:51 +08:00
log := log.WithField("_routine", "HasuraConnectionReader").WithField("browserConnectionId", hc.BrowserConn.Id).WithField("hasuraConnectionId", hc.Id)
defer log.Debugf("finished")
log.Debugf("starting")
2023-04-27 09:03:40 +08:00
defer wg.Done()
defer hc.ContextCancelFunc()
for {
// Read a message from hasura
var message interface{}
err := wsjson.Read(hc.Context, hc.Websocket, &message)
var closeError *websocket.CloseError
2023-04-27 09:03:40 +08:00
if err != nil {
if errors.Is(err, context.Canceled) {
2024-03-13 21:35:51 +08:00
log.Debugf("Closing Hasura ws connection as Context was cancelled!")
} else if errors.As(err, &closeError) {
hc.WebsocketCloseError = closeError
log.Debug("WebSocket connection closed: status = %v, reason = %s", closeError.Code, closeError.Reason)
//TODO check if it should send {"type":"connection_error","payload":"Authentication hook unauthorized this request"}
} else {
if websocket.CloseStatus(err) == -1 {
//It doesn't have a CloseError, it will reconnect do Hasura
} else {
//In case Hasura sent an CloseError, it will forward it to browser and disconnect
hc.WebsocketCloseError = &websocket.CloseError{
Code: websocket.CloseStatus(err),
Reason: "Graphql connection closed with error" + err.Error(),
}
}
2024-03-13 21:35:51 +08:00
log.Debugf("Error reading message from Hasura: %v", err)
}
2023-04-27 09:03:40 +08:00
return
}
log.Tracef("received from hasura: %v", message)
handleMessageReceivedFromHasura(hc, fromHasuraToBrowserChannel, fromBrowserToHasuraChannel, message)
}
}
func handleMessageReceivedFromHasura(hc *common.HasuraConnection, fromHasuraToBrowserChannel *common.SafeChannel, fromBrowserToHasuraChannel *common.SafeChannel, message interface{}) {
var messageMap = message.(map[string]interface{})
if messageMap != nil {
var messageType = messageMap["type"]
var queryId, _ = messageMap["id"].(string)
//Check if subscription is still active!
if queryId != "" {
2024-03-13 21:35:51 +08:00
hc.BrowserConn.ActiveSubscriptionsMutex.RLock()
subscription, ok := hc.BrowserConn.ActiveSubscriptions[queryId]
hc.BrowserConn.ActiveSubscriptionsMutex.RUnlock()
if !ok {
log.Debugf("Subscription with Id %s doesn't exist anymore, skipping response.", queryId)
return
}
2023-04-27 09:03:40 +08:00
//When Hasura send msg type "complete", this query is finished
if messageType == "complete" {
handleCompleteMessage(hc, queryId)
common.ActivitiesOverviewCompleted(string(subscription.Type) + "-" + subscription.OperationName)
common.ActivitiesOverviewCompleted("_Sum-" + string(subscription.Type))
}
if messageType == "next" {
common.ActivitiesOverviewDataReceived(string(subscription.Type) + "-" + subscription.OperationName)
}
if messageType == "next" &&
subscription.Type == common.Subscription {
hasNoPreviousOccurrence := handleSubscriptionMessage(hc, messageMap, subscription, queryId)
2023-04-27 09:03:40 +08:00
if !hasNoPreviousOccurrence {
return
}
}
//Set last cursor value for stream
if subscription.Type == common.Streaming {
handleStreamingMessage(hc, messageMap, subscription, queryId)
}
}
// Retransmit the subscription start commands when hasura confirms the connection
// this is useful in case of a connection invalidation
if messageType == "connection_ack" {
handleConnectionAckMessage(hc, messageMap, fromHasuraToBrowserChannel, fromBrowserToHasuraChannel)
} else {
// Forward the message to browser
fromHasuraToBrowserChannel.Send(messageMap)
}
}
}
func handleSubscriptionMessage(hc *common.HasuraConnection, messageMap map[string]interface{}, subscription common.GraphQlSubscription, queryId string) bool {
if payload, okPayload := messageMap["payload"].(map[string]interface{}); okPayload {
if data, okData := payload["data"].(map[string]interface{}); okData {
for dataKey, dataItem := range data {
if currentDataProp, okCurrentDataProp := dataItem.([]interface{}); okCurrentDataProp {
if dataAsJson, err := json.Marshal(currentDataProp); err == nil {
if common.ActivitiesOverviewEnabled {
dataSize := len(string(dataAsJson))
dataCount := len(currentDataProp)
common.ActivitiesOverviewDataSize(string(subscription.Type)+"-"+subscription.OperationName, int64(dataSize), int64(dataCount))
}
//Check whether ReceivedData is different from the LastReceivedData
//Otherwise stop forwarding this message
dataChecksum := crc32.ChecksumIEEE(dataAsJson)
if subscription.LastReceivedDataChecksum == dataChecksum {
return false
}
lastDataChecksumWas := subscription.LastReceivedDataChecksum
lastDataAsJsonWas := subscription.LastReceivedData
cacheKey := fmt.Sprintf("%s-%s-%v-%v", string(subscription.Type), subscription.OperationName, subscription.LastReceivedDataChecksum, dataChecksum)
//Store LastReceivedData Checksum
if msgpatch.RawDataCacheStorageMode == "memory" {
subscription.LastReceivedData = dataAsJson
}
subscription.LastReceivedDataChecksum = dataChecksum
2024-03-13 21:35:51 +08:00
hc.BrowserConn.ActiveSubscriptionsMutex.Lock()
hc.BrowserConn.ActiveSubscriptions[queryId] = subscription
hc.BrowserConn.ActiveSubscriptionsMutex.Unlock()
//Apply msg patch when it supports it
if subscription.JsonPatchSupported {
msgpatch.PatchMessage(&messageMap, queryId, dataKey, lastDataAsJsonWas, dataAsJson, hc.BrowserConn.Id, hc.BrowserConn.SessionToken, cacheKey, lastDataChecksumWas, dataChecksum)
}
}
}
}
}
}
return true
}
func handleStreamingMessage(hc *common.HasuraConnection, messageMap map[string]interface{}, subscription common.GraphQlSubscription, queryId string) {
lastCursor := common.GetLastStreamCursorValueFromReceivedMessage(messageMap, subscription.StreamCursorField)
if lastCursor != nil && subscription.StreamCursorCurrValue != lastCursor {
subscription.StreamCursorCurrValue = lastCursor
2024-03-13 21:35:51 +08:00
hc.BrowserConn.ActiveSubscriptionsMutex.Lock()
hc.BrowserConn.ActiveSubscriptions[queryId] = subscription
hc.BrowserConn.ActiveSubscriptionsMutex.Unlock()
2023-04-27 09:03:40 +08:00
}
}
func handleCompleteMessage(hc *common.HasuraConnection, queryId string) {
2024-03-13 21:35:51 +08:00
hc.BrowserConn.ActiveSubscriptionsMutex.Lock()
queryType := hc.BrowserConn.ActiveSubscriptions[queryId].Type
operationName := hc.BrowserConn.ActiveSubscriptions[queryId].OperationName
delete(hc.BrowserConn.ActiveSubscriptions, queryId)
hc.BrowserConn.ActiveSubscriptionsMutex.Unlock()
log.Debugf("%s (%s) with Id %s finished by Hasura.", queryType, operationName, queryId)
}
func handleConnectionAckMessage(hc *common.HasuraConnection, messageMap map[string]interface{}, fromHasuraToBrowserChannel *common.SafeChannel, fromBrowserToHasuraChannel *common.SafeChannel) {
log.Debugf("Received connection_ack")
//Hasura connection was initialized, now it's able to send new messages to Hasura
fromBrowserToHasuraChannel.UnfreezeChannel()
//Avoid to send `connection_ack` to the browser when it's a reconnection
2024-03-13 21:35:51 +08:00
if hc.BrowserConn.ConnAckSentToBrowser == false {
fromHasuraToBrowserChannel.Send(messageMap)
2024-03-13 21:35:51 +08:00
hc.BrowserConn.ConnAckSentToBrowser = true
}
go retransmiter.RetransmitSubscriptionStartMessages(hc, fromBrowserToHasuraChannel)
}