2021-08-24 19:25:12 +03:00
|
|
|
module Hasura.GraphQL.Transport.WSServerApp
|
2021-09-24 01:56:37 +03:00
|
|
|
( createWSServerApp,
|
|
|
|
stopWSServerApp,
|
|
|
|
createWSServerEnv,
|
|
|
|
)
|
|
|
|
where
|
|
|
|
|
|
|
|
import Control.Concurrent.Async.Lifted.Safe qualified as LA
|
|
|
|
import Control.Concurrent.STM qualified as STM
|
|
|
|
import Control.Exception.Lifted
|
|
|
|
import Control.Monad.Trans.Control qualified as MC
|
2021-11-04 15:38:57 +03:00
|
|
|
import Data.Aeson (object, toJSON, (.=))
|
2021-09-24 01:56:37 +03:00
|
|
|
import Data.ByteString.Char8 qualified as B (pack)
|
|
|
|
import Data.Environment qualified as Env
|
|
|
|
import Data.Text (pack, unpack)
|
|
|
|
import Hasura.GraphQL.Execute qualified as E
|
|
|
|
import Hasura.GraphQL.Execute.Backend qualified as EB
|
2022-03-21 13:39:49 +03:00
|
|
|
import Hasura.GraphQL.Execute.Subscription.State qualified as ES
|
2021-09-24 01:56:37 +03:00
|
|
|
import Hasura.GraphQL.Logging
|
|
|
|
import Hasura.GraphQL.Transport.HTTP (MonadExecuteQuery)
|
|
|
|
import Hasura.GraphQL.Transport.Instances ()
|
|
|
|
import Hasura.GraphQL.Transport.WebSocket
|
|
|
|
import Hasura.GraphQL.Transport.WebSocket.Protocol
|
|
|
|
import Hasura.GraphQL.Transport.WebSocket.Server qualified as WS
|
|
|
|
import Hasura.GraphQL.Transport.WebSocket.Types
|
|
|
|
import Hasura.Logging qualified as L
|
|
|
|
import Hasura.Metadata.Class
|
|
|
|
import Hasura.Prelude
|
2022-04-27 16:57:28 +03:00
|
|
|
import Hasura.RQL.Types.Common
|
|
|
|
import Hasura.RQL.Types.SchemaCache
|
2021-09-24 01:56:37 +03:00
|
|
|
import Hasura.Server.Auth (AuthMode, UserAuthentication)
|
|
|
|
import Hasura.Server.Cors
|
|
|
|
import Hasura.Server.Init.Config
|
|
|
|
( KeepAliveDelay,
|
|
|
|
WSConnectionInitTimeout,
|
|
|
|
)
|
2021-09-29 19:20:06 +03:00
|
|
|
import Hasura.Server.Limits
|
2021-09-24 01:56:37 +03:00
|
|
|
import Hasura.Server.Metrics (ServerMetrics (..))
|
2022-07-24 00:18:01 +03:00
|
|
|
import Hasura.Server.Prometheus
|
|
|
|
( PrometheusMetrics (..),
|
|
|
|
decWebsocketConnections,
|
|
|
|
incWebsocketConnections,
|
|
|
|
)
|
2021-12-08 09:26:46 +03:00
|
|
|
import Hasura.Server.Types (ReadOnlyMode)
|
2021-09-24 01:56:37 +03:00
|
|
|
import Hasura.Tracing qualified as Tracing
|
2022-02-16 10:08:51 +03:00
|
|
|
import Network.HTTP.Client qualified as HTTP
|
2021-09-24 01:56:37 +03:00
|
|
|
import Network.WebSockets qualified as WS
|
|
|
|
import System.Metrics.Gauge qualified as EKG.Gauge
|
2021-08-24 19:25:12 +03:00
|
|
|
|
|
|
|
createWSServerApp ::
|
2021-10-13 19:38:56 +03:00
|
|
|
( MonadIO m,
|
2021-09-24 01:56:37 +03:00
|
|
|
MC.MonadBaseControl IO m,
|
|
|
|
LA.Forall (LA.Pure m),
|
|
|
|
UserAuthentication (Tracing.TraceT m),
|
|
|
|
E.MonadGQLExecutionCheck m,
|
|
|
|
WS.MonadWSLog m,
|
|
|
|
MonadQueryLog m,
|
|
|
|
Tracing.HasReporter m,
|
|
|
|
MonadExecuteQuery m,
|
|
|
|
MonadMetadataStorage (MetadataStorageT m),
|
2021-09-29 19:20:06 +03:00
|
|
|
EB.MonadQueryTags m,
|
|
|
|
HasResourceLimits m
|
2021-09-24 01:56:37 +03:00
|
|
|
) =>
|
|
|
|
Env.Environment ->
|
|
|
|
HashSet (L.EngineLogType L.Hasura) ->
|
|
|
|
AuthMode ->
|
|
|
|
WSServerEnv ->
|
|
|
|
WSConnectionInitTimeout ->
|
|
|
|
WS.HasuraServerApp m
|
2021-08-24 19:25:12 +03:00
|
|
|
-- -- ^ aka generalized 'WS.ServerApp'
|
|
|
|
createWSServerApp env enabledLogTypes authMode serverEnv connInitTimeout = \ !ipAddress !pendingConn ->
|
|
|
|
WS.createServerApp connInitTimeout (_wseServer serverEnv) handlers ipAddress pendingConn
|
|
|
|
where
|
2021-09-24 01:56:37 +03:00
|
|
|
handlers =
|
|
|
|
WS.WSHandlers
|
|
|
|
onConnHandler
|
|
|
|
onMessageHandler
|
|
|
|
onCloseHandler
|
2021-08-24 19:25:12 +03:00
|
|
|
|
|
|
|
logger = _wseLogger serverEnv
|
|
|
|
serverMetrics = _wseServerMetrics serverEnv
|
2022-07-24 00:18:01 +03:00
|
|
|
prometheusMetrics = _wsePrometheusMetrics serverEnv
|
2021-08-24 19:25:12 +03:00
|
|
|
|
|
|
|
wsActions = mkWSActions logger
|
|
|
|
|
|
|
|
-- Mask async exceptions during event processing to help maintain integrity of mutable vars:
|
|
|
|
-- here `sp` stands for sub-protocol
|
|
|
|
onConnHandler rid rh ip sp = mask_ do
|
|
|
|
liftIO $ EKG.Gauge.inc $ smWebsocketConnections serverMetrics
|
2022-07-24 00:18:01 +03:00
|
|
|
liftIO $ incWebsocketConnections $ pmConnections prometheusMetrics
|
2021-08-24 19:25:12 +03:00
|
|
|
flip runReaderT serverEnv $ onConn rid rh ip (wsActions sp)
|
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
onMessageHandler conn bs sp =
|
|
|
|
mask_ $
|
|
|
|
onMessage env enabledLogTypes authMode serverEnv conn bs (wsActions sp)
|
2021-08-24 19:25:12 +03:00
|
|
|
|
|
|
|
onCloseHandler conn = mask_ do
|
|
|
|
liftIO $ EKG.Gauge.dec $ smWebsocketConnections serverMetrics
|
2022-07-24 00:18:01 +03:00
|
|
|
liftIO $ decWebsocketConnections $ pmConnections prometheusMetrics
|
|
|
|
onClose logger serverMetrics prometheusMetrics (_wseSubscriptionState serverEnv) conn
|
2021-08-24 19:25:12 +03:00
|
|
|
|
|
|
|
stopWSServerApp :: WSServerEnv -> IO ()
|
|
|
|
stopWSServerApp wsEnv = WS.shutdown (_wseServer wsEnv)
|
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
createWSServerEnv ::
|
|
|
|
(MonadIO m) =>
|
|
|
|
L.Logger L.Hasura ->
|
2022-03-21 13:39:49 +03:00
|
|
|
ES.SubscriptionsState ->
|
2021-09-24 01:56:37 +03:00
|
|
|
IO (SchemaCache, SchemaCacheVer) ->
|
2022-02-16 10:08:51 +03:00
|
|
|
HTTP.Manager ->
|
2021-09-24 01:56:37 +03:00
|
|
|
CorsPolicy ->
|
|
|
|
SQLGenCtx ->
|
2021-12-08 09:26:46 +03:00
|
|
|
ReadOnlyMode ->
|
2021-09-24 01:56:37 +03:00
|
|
|
Bool ->
|
|
|
|
KeepAliveDelay ->
|
|
|
|
ServerMetrics ->
|
2022-07-24 00:18:01 +03:00
|
|
|
PrometheusMetrics ->
|
2021-09-24 01:56:37 +03:00
|
|
|
m WSServerEnv
|
|
|
|
createWSServerEnv
|
|
|
|
logger
|
|
|
|
lqState
|
|
|
|
getSchemaCache
|
|
|
|
httpManager
|
|
|
|
corsPolicy
|
|
|
|
sqlGenCtx
|
2021-12-08 09:26:46 +03:00
|
|
|
readOnlyMode
|
2021-09-24 01:56:37 +03:00
|
|
|
enableAL
|
|
|
|
keepAliveDelay
|
2022-07-24 00:18:01 +03:00
|
|
|
serverMetrics
|
|
|
|
prometheusMetrics = do
|
2021-09-24 01:56:37 +03:00
|
|
|
wsServer <- liftIO $ STM.atomically $ WS.createWSServer logger
|
|
|
|
pure $
|
|
|
|
WSServerEnv
|
|
|
|
logger
|
|
|
|
lqState
|
|
|
|
getSchemaCache
|
|
|
|
httpManager
|
|
|
|
corsPolicy
|
|
|
|
sqlGenCtx
|
2021-12-08 09:26:46 +03:00
|
|
|
readOnlyMode
|
2021-09-24 01:56:37 +03:00
|
|
|
wsServer
|
|
|
|
enableAL
|
|
|
|
keepAliveDelay
|
|
|
|
serverMetrics
|
2022-07-24 00:18:01 +03:00
|
|
|
prometheusMetrics
|
2021-08-24 19:25:12 +03:00
|
|
|
|
|
|
|
mkWSActions :: L.Logger L.Hasura -> WSSubProtocol -> WS.WSActions WSConnData
|
|
|
|
mkWSActions logger subProtocol =
|
|
|
|
WS.WSActions
|
|
|
|
mkPostExecErrMessageAction
|
|
|
|
mkOnErrorMessageAction
|
|
|
|
mkConnectionCloseAction
|
|
|
|
keepAliveAction
|
|
|
|
getServerMsgType
|
|
|
|
mkAcceptRequest
|
2021-11-04 15:38:57 +03:00
|
|
|
fmtErrorMessage
|
2021-08-24 19:25:12 +03:00
|
|
|
where
|
|
|
|
mkPostExecErrMessageAction wsConn opId execErr =
|
|
|
|
sendMsg wsConn $ case subProtocol of
|
2021-09-24 01:56:37 +03:00
|
|
|
Apollo -> SMData $ DataMsg opId $ throwError execErr
|
|
|
|
GraphQLWS -> SMErr $ ErrorMsg opId $ toJSON execErr
|
2021-08-24 19:25:12 +03:00
|
|
|
|
|
|
|
mkOnErrorMessageAction wsConn err mErrMsg = case subProtocol of
|
2021-09-24 01:56:37 +03:00
|
|
|
Apollo -> sendMsg wsConn $ SMConnErr err
|
2021-11-04 15:38:57 +03:00
|
|
|
GraphQLWS -> sendCloseWithMsg logger wsConn (GenericError4400 $ (fromMaybe "" mErrMsg) <> (unpack . unConnErrMsg $ err)) Nothing Nothing
|
2021-08-24 19:25:12 +03:00
|
|
|
|
|
|
|
mkConnectionCloseAction wsConn opId errMsg =
|
|
|
|
when (subProtocol == GraphQLWS) $
|
2021-11-04 15:38:57 +03:00
|
|
|
sendCloseWithMsg logger wsConn (GenericError4400 errMsg) (Just . SMErr $ ErrorMsg opId $ toJSON (pack errMsg)) (Just 1000)
|
2021-08-24 19:25:12 +03:00
|
|
|
|
|
|
|
getServerMsgType = case subProtocol of
|
2021-09-24 01:56:37 +03:00
|
|
|
Apollo -> SMData
|
2021-08-24 19:25:12 +03:00
|
|
|
GraphQLWS -> SMNext
|
|
|
|
|
|
|
|
keepAliveAction wsConn = sendMsg wsConn $
|
|
|
|
case subProtocol of
|
2021-09-24 01:56:37 +03:00
|
|
|
Apollo -> SMConnKeepAlive
|
2021-08-24 19:25:12 +03:00
|
|
|
GraphQLWS -> SMPing . Just $ keepAliveMessage
|
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
mkAcceptRequest =
|
|
|
|
WS.defaultAcceptRequest
|
|
|
|
{ WS.acceptSubprotocol = Just . B.pack . showSubProtocol $ subProtocol
|
|
|
|
}
|
2021-11-04 15:38:57 +03:00
|
|
|
|
|
|
|
fmtErrorMessage errMsgs = case subProtocol of
|
|
|
|
Apollo -> object ["errors" .= errMsgs]
|
|
|
|
GraphQLWS -> toJSON errMsgs
|