2023-04-27 09:03:40 +08:00
|
|
|
package reader
|
|
|
|
|
|
|
|
import (
|
2024-01-24 02:28:32 +08:00
|
|
|
"context"
|
|
|
|
"errors"
|
2023-04-27 09:03:40 +08:00
|
|
|
"github.com/iMDT/bbb-graphql-middleware/internal/common"
|
2023-12-15 01:01:47 +08:00
|
|
|
"github.com/iMDT/bbb-graphql-middleware/internal/hascli/retransmiter"
|
2023-05-10 02:37:58 +08:00
|
|
|
"github.com/iMDT/bbb-graphql-middleware/internal/msgpatch"
|
2023-04-27 09:03:40 +08:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"nhooyr.io/websocket/wsjson"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
// HasuraConnectionReader consumes messages from Hasura connection and add send to the browser channel
|
2023-12-15 01:01:47 +08:00
|
|
|
func HasuraConnectionReader(hc *common.HasuraConnection, fromHasuraToBrowserChannel *common.SafeChannel, fromBrowserToHasuraChannel *common.SafeChannel, wg *sync.WaitGroup) {
|
2023-04-27 09:03:40 +08:00
|
|
|
log := log.WithField("_routine", "HasuraConnectionReader").WithField("browserConnectionId", hc.Browserconn.Id).WithField("hasuraConnectionId", hc.Id)
|
2023-09-30 07:05:23 +08:00
|
|
|
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)
|
|
|
|
if err != nil {
|
2024-01-24 02:28:32 +08:00
|
|
|
if errors.Is(err, context.Canceled) {
|
|
|
|
log.Debugf("Closing ws connection as Context was cancelled!")
|
|
|
|
} else {
|
|
|
|
log.Errorf("Error reading message from Hasura: %v", err)
|
|
|
|
}
|
2023-04-27 09:03:40 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Tracef("received from hasura: %v", message)
|
|
|
|
|
|
|
|
var messageAsMap = message.(map[string]interface{})
|
2023-07-06 20:34:48 +08:00
|
|
|
|
2023-05-10 02:37:58 +08:00
|
|
|
if messageAsMap != nil {
|
|
|
|
var messageType = messageAsMap["type"]
|
|
|
|
var queryId, _ = messageAsMap["id"].(string)
|
2023-04-27 09:03:40 +08:00
|
|
|
|
2023-05-10 02:37:58 +08:00
|
|
|
//Check if subscription is still active!
|
|
|
|
if queryId != "" {
|
2023-07-06 20:34:48 +08:00
|
|
|
hc.Browserconn.ActiveSubscriptionsMutex.RLock()
|
2023-05-10 02:37:58 +08:00
|
|
|
subscription, ok := hc.Browserconn.ActiveSubscriptions[queryId]
|
2023-07-06 20:34:48 +08:00
|
|
|
hc.Browserconn.ActiveSubscriptionsMutex.RUnlock()
|
2023-05-10 02:37:58 +08:00
|
|
|
if !ok {
|
|
|
|
log.Debugf("Subscription with Id %s doesn't exist anymore, skiping response.", queryId)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
//When Hasura send msg type "complete", this query is finished
|
|
|
|
if messageType == "complete" {
|
|
|
|
hc.Browserconn.ActiveSubscriptionsMutex.Lock()
|
|
|
|
delete(hc.Browserconn.ActiveSubscriptions, queryId)
|
|
|
|
hc.Browserconn.ActiveSubscriptionsMutex.Unlock()
|
2023-09-07 22:54:27 +08:00
|
|
|
log.Debugf("Subscription with Id %s finished by Hasura.", queryId)
|
2023-05-10 02:37:58 +08:00
|
|
|
}
|
2023-04-27 09:03:40 +08:00
|
|
|
|
2023-05-10 02:37:58 +08:00
|
|
|
//Apply msg patch when it supports it
|
2023-05-25 06:31:31 +08:00
|
|
|
if subscription.JsonPatchSupported &&
|
2023-05-10 02:37:58 +08:00
|
|
|
messageType == "data" &&
|
|
|
|
subscription.Type == common.Subscription {
|
|
|
|
msgpatch.PatchMessage(&messageAsMap, hc.Browserconn)
|
|
|
|
}
|
2024-01-24 02:28:32 +08:00
|
|
|
|
|
|
|
//Set last cursor value for stream
|
|
|
|
if subscription.Type == common.Streaming {
|
|
|
|
lastCursor := common.GetLastStreamCursorValueFromReceivedMessage(messageAsMap, subscription.StreamCursorField)
|
|
|
|
if lastCursor != nil && subscription.StreamCursorCurrValue != lastCursor {
|
|
|
|
subscription.StreamCursorCurrValue = lastCursor
|
|
|
|
|
|
|
|
hc.Browserconn.ActiveSubscriptionsMutex.Lock()
|
|
|
|
hc.Browserconn.ActiveSubscriptions[queryId] = subscription
|
|
|
|
hc.Browserconn.ActiveSubscriptionsMutex.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2023-05-10 02:37:58 +08:00
|
|
|
}
|
|
|
|
|
2023-12-15 01:01:47 +08:00
|
|
|
// Retransmit the subscription start commands when hasura confirms the connection
|
2023-05-10 02:37:58 +08:00
|
|
|
// this is useful in case of a connection invalidation
|
|
|
|
if messageType == "connection_ack" {
|
2024-01-24 07:20:16 +08:00
|
|
|
//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
|
|
|
|
if hc.Browserconn.ConnAckSentToBrowser == false {
|
|
|
|
fromHasuraToBrowserChannel.Send(messageAsMap)
|
|
|
|
hc.Browserconn.ConnAckSentToBrowser = true
|
|
|
|
}
|
|
|
|
|
2023-12-15 01:01:47 +08:00
|
|
|
go retransmiter.RetransmitSubscriptionStartMessages(hc, fromBrowserToHasuraChannel)
|
2024-01-24 07:20:16 +08:00
|
|
|
} else {
|
|
|
|
// Forward the message to browser
|
|
|
|
fromHasuraToBrowserChannel.Send(messageAsMap)
|
2023-05-10 02:37:58 +08:00
|
|
|
}
|
|
|
|
}
|
2023-04-27 09:03:40 +08:00
|
|
|
}
|
|
|
|
}
|