2023-04-27 09:03:40 +08:00
|
|
|
package hascli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"github.com/iMDT/bbb-graphql-middleware/internal/hascli/conn/reader"
|
|
|
|
"github.com/iMDT/bbb-graphql-middleware/internal/hascli/conn/writer"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"math"
|
|
|
|
"net/http"
|
|
|
|
"net/http/cookiejar"
|
|
|
|
"net/url"
|
2023-11-30 19:58:37 +08:00
|
|
|
"os"
|
2023-04-27 09:03:40 +08:00
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/iMDT/bbb-graphql-middleware/internal/common"
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
"nhooyr.io/websocket"
|
|
|
|
)
|
|
|
|
|
|
|
|
var lastHasuraConnectionId int
|
2023-11-30 19:58:37 +08:00
|
|
|
var hasuraEndpoint = os.Getenv("BBB_GRAPHQL_MIDDLEWARE_HASURA_WS")
|
2023-04-27 09:03:40 +08:00
|
|
|
|
|
|
|
// Hasura client connection
|
2023-12-15 01:01:47 +08:00
|
|
|
func HasuraClient(browserConnection *common.BrowserConnection, cookies []*http.Cookie, fromBrowserToHasuraChannel *common.SafeChannel, fromHasuraToBrowserChannel *common.SafeChannel) error {
|
2023-04-27 09:03:40 +08:00
|
|
|
log := log.WithField("_routine", "HasuraClient").WithField("browserConnectionId", browserConnection.Id)
|
2024-03-28 22:26:56 +08:00
|
|
|
common.ActivitiesOverviewIncIndex("__HasuraConnection-Added")
|
|
|
|
defer common.ActivitiesOverviewIncIndex("__HasuraConnection-Removed")
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
//Remove subscriptions from ActivitiesOverview here once Hasura-Reader will ignore "complete" msg for them
|
|
|
|
browserConnection.ActiveSubscriptionsMutex.RLock()
|
|
|
|
for _, subscription := range browserConnection.ActiveSubscriptions {
|
|
|
|
common.ActivitiesOverviewIncIndex("Hasura-" + subscription.OperationName + "-Completed")
|
|
|
|
common.ActivitiesOverviewIncIndex("_Hasura-" + string(subscription.Type) + "-Completed")
|
|
|
|
}
|
|
|
|
browserConnection.ActiveSubscriptionsMutex.RUnlock()
|
|
|
|
}()
|
2023-04-27 09:03:40 +08:00
|
|
|
|
|
|
|
// Obtain id for this connection
|
|
|
|
lastHasuraConnectionId++
|
|
|
|
hasuraConnectionId := "HC" + fmt.Sprintf("%010d", lastHasuraConnectionId)
|
|
|
|
log = log.WithField("hasuraConnectionId", hasuraConnectionId)
|
|
|
|
|
2023-09-07 22:54:27 +08:00
|
|
|
defer log.Debugf("finished")
|
2023-04-27 09:03:40 +08:00
|
|
|
|
|
|
|
// Add sub-protocol
|
|
|
|
var dialOptions websocket.DialOptions
|
|
|
|
dialOptions.Subprotocols = append(dialOptions.Subprotocols, "graphql-ws")
|
|
|
|
|
|
|
|
// Create cookie jar
|
|
|
|
jar, err := cookiejar.New(nil)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("failed to create cookie jar: %w", err)
|
|
|
|
}
|
|
|
|
parsedURL, err := url.Parse(hasuraEndpoint)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("failed to parse url: %w", err)
|
|
|
|
}
|
|
|
|
parsedURL.Scheme = "http"
|
|
|
|
jar.SetCookies(parsedURL, cookies)
|
|
|
|
hc := &http.Client{
|
|
|
|
Jar: jar,
|
|
|
|
}
|
|
|
|
dialOptions.HTTPClient = hc
|
|
|
|
|
|
|
|
// Create a context for the hasura connection, that depends on the browser context
|
|
|
|
// this means that if browser connection is closed, the hasura connection will close also
|
|
|
|
// this also means that we can close the hasura connection without closing the browser one
|
|
|
|
// this is used for the invalidation process (reconnection only on the hasura side )
|
|
|
|
var hasuraConnectionContext, hasuraConnectionContextCancel = context.WithCancel(browserConnection.Context)
|
|
|
|
defer hasuraConnectionContextCancel()
|
|
|
|
|
|
|
|
var thisConnection = common.HasuraConnection{
|
2024-03-13 21:35:51 +08:00
|
|
|
Id: hasuraConnectionId,
|
|
|
|
BrowserConn: browserConnection,
|
|
|
|
Context: hasuraConnectionContext,
|
|
|
|
ContextCancelFunc: hasuraConnectionContextCancel,
|
|
|
|
FreezeMsgFromBrowserChan: common.NewSafeChannel(1),
|
2023-04-27 09:03:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
browserConnection.HasuraConnection = &thisConnection
|
2024-01-24 07:20:16 +08:00
|
|
|
defer func() {
|
|
|
|
browserConnection.HasuraConnection = nil
|
|
|
|
|
|
|
|
//It's necessary to freeze the channel to avoid client trying to start subscriptions before Hasura connection is initialised
|
|
|
|
//It will unfreeze after `connection_ack` is sent by Hasura
|
|
|
|
fromBrowserToHasuraChannel.FreezeChannel()
|
|
|
|
}()
|
2023-04-27 09:03:40 +08:00
|
|
|
|
|
|
|
// Make the connection
|
|
|
|
c, _, err := websocket.Dial(hasuraConnectionContext, hasuraEndpoint, &dialOptions)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("error connecting to hasura: %v", err)
|
|
|
|
}
|
|
|
|
defer c.Close(websocket.StatusInternalError, "the sky is falling")
|
|
|
|
|
|
|
|
c.SetReadLimit(math.MaxInt64 - 1)
|
|
|
|
|
|
|
|
thisConnection.Websocket = c
|
|
|
|
|
|
|
|
// Log the connection success
|
2023-09-07 22:54:27 +08:00
|
|
|
log.Debugf("connected with Hasura")
|
2023-04-27 09:03:40 +08:00
|
|
|
|
|
|
|
// Configure the wait group
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(2)
|
|
|
|
|
|
|
|
// Start routines
|
|
|
|
|
|
|
|
// reads from browser, writes to hasura
|
2024-01-24 07:20:16 +08:00
|
|
|
go writer.HasuraConnectionWriter(&thisConnection, fromBrowserToHasuraChannel, &wg, browserConnection.ConnectionInitMessage)
|
2023-04-27 09:03:40 +08:00
|
|
|
|
|
|
|
// reads from hasura, writes to browser
|
2023-12-15 01:01:47 +08:00
|
|
|
go reader.HasuraConnectionReader(&thisConnection, fromHasuraToBrowserChannel, fromBrowserToHasuraChannel, &wg)
|
2023-04-27 09:03:40 +08:00
|
|
|
|
|
|
|
// Wait
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|