2023-04-27 09:03:40 +08:00
|
|
|
package reader
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-03-13 21:35:51 +08:00
|
|
|
"errors"
|
2023-10-05 04:11:12 +08:00
|
|
|
"github.com/iMDT/bbb-graphql-middleware/internal/common"
|
2023-04-27 09:03:40 +08:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"nhooyr.io/websocket"
|
|
|
|
"nhooyr.io/websocket/wsjson"
|
2023-04-27 09:26:55 +08:00
|
|
|
"sync"
|
|
|
|
"time"
|
2023-04-27 09:03:40 +08:00
|
|
|
)
|
|
|
|
|
2024-03-13 07:12:55 +08:00
|
|
|
func BrowserConnectionReader(browserConnectionId string, ctx context.Context, ctxCancel context.CancelFunc, browserWsConn *websocket.Conn, fromBrowserToHasuraChannel *common.SafeChannel, fromBrowserToHasuraConnectionEstablishingChannel *common.SafeChannel, waitGroups []*sync.WaitGroup) {
|
2023-04-27 09:45:19 +08:00
|
|
|
log := log.WithField("_routine", "BrowserConnectionReader").WithField("browserConnectionId", browserConnectionId)
|
2023-09-30 07:05:23 +08:00
|
|
|
defer log.Debugf("finished")
|
|
|
|
log.Debugf("starting")
|
2023-04-27 09:03:40 +08:00
|
|
|
|
|
|
|
defer func() {
|
2024-01-24 07:20:16 +08:00
|
|
|
fromBrowserToHasuraChannel.Close()
|
|
|
|
fromBrowserToHasuraConnectionEstablishingChannel.Close()
|
2023-04-27 09:26:55 +08:00
|
|
|
}()
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
// Let other routines know this is about to die
|
2023-04-27 09:03:40 +08:00
|
|
|
for _, wg := range waitGroups {
|
|
|
|
wg.Done()
|
|
|
|
}
|
2023-04-27 09:26:55 +08:00
|
|
|
// Wait a little bit before closing the channels
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
2023-04-27 09:03:40 +08:00
|
|
|
}()
|
2023-04-27 09:26:55 +08:00
|
|
|
|
2024-03-13 07:12:55 +08:00
|
|
|
defer ctxCancel()
|
2023-04-27 09:03:40 +08:00
|
|
|
|
|
|
|
for {
|
|
|
|
var v interface{}
|
2024-03-13 07:12:55 +08:00
|
|
|
err := wsjson.Read(ctx, browserWsConn, &v)
|
2023-04-27 09:03:40 +08:00
|
|
|
if err != nil {
|
2024-03-13 21:35:51 +08:00
|
|
|
if errors.Is(err, context.Canceled) {
|
|
|
|
log.Debugf("Closing Browser ws connection as Context was cancelled!")
|
|
|
|
} else {
|
|
|
|
log.Debugf("Hasura is disconnected, skiping reading of ws message: %v", err)
|
|
|
|
}
|
2023-04-27 09:03:40 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Tracef("received from browser: %v", v)
|
|
|
|
|
2024-01-24 07:20:16 +08:00
|
|
|
fromBrowserToHasuraChannel.Send(v)
|
|
|
|
fromBrowserToHasuraConnectionEstablishingChannel.Send(v)
|
2023-04-27 09:03:40 +08:00
|
|
|
}
|
|
|
|
}
|