2021-11-04 19:08:33 +03:00
|
|
|
module Hasura.Server.Limits
|
|
|
|
( HasResourceLimits (..),
|
|
|
|
ResourceLimits (..),
|
|
|
|
)
|
|
|
|
where
|
2021-09-29 19:20:06 +03:00
|
|
|
|
|
|
|
import Control.Monad.Trans.Control (MonadBaseControl)
|
|
|
|
import Hasura.Base.Error
|
|
|
|
import Hasura.Prelude
|
|
|
|
import Hasura.RQL.Types.ApiLimit (ApiLimit)
|
2022-07-27 09:38:41 +03:00
|
|
|
import Hasura.Server.Types qualified as HGE
|
2021-09-29 19:20:06 +03:00
|
|
|
import Hasura.Session (UserInfo)
|
|
|
|
import Hasura.Tracing qualified as Tracing
|
|
|
|
|
|
|
|
-- | Resource limits, represented by a function which modifies IO actions to
|
|
|
|
-- enforce those limits by throwing errors using 'MonadError' in the case
|
|
|
|
-- where they are exceeded.
|
|
|
|
data ResourceLimits = ResourceLimits
|
|
|
|
{ runResourceLimits ::
|
|
|
|
forall m a.
|
|
|
|
(MonadBaseControl IO m, MonadError QErr m) =>
|
|
|
|
m a ->
|
|
|
|
m a
|
|
|
|
}
|
|
|
|
|
|
|
|
-- | Monads which support resource (memory, CPU time, etc.) limiting
|
2023-05-24 16:51:56 +03:00
|
|
|
class (Monad m) => HasResourceLimits m where
|
2021-09-29 19:20:06 +03:00
|
|
|
askHTTPHandlerLimit :: m ResourceLimits
|
2022-10-27 18:34:43 +03:00
|
|
|
askGraphqlOperationLimit :: HGE.RequestId -> UserInfo -> ApiLimit -> m ResourceLimits
|
2021-09-29 19:20:06 +03:00
|
|
|
|
|
|
|
-- A default for monad transformer instances
|
|
|
|
default askHTTPHandlerLimit ::
|
|
|
|
(m ~ t n, MonadTrans t, HasResourceLimits n) =>
|
|
|
|
m ResourceLimits
|
|
|
|
askHTTPHandlerLimit = lift askHTTPHandlerLimit
|
|
|
|
|
|
|
|
default askGraphqlOperationLimit ::
|
|
|
|
(m ~ t n, MonadTrans t, HasResourceLimits n) =>
|
2022-07-27 09:38:41 +03:00
|
|
|
HGE.RequestId ->
|
2022-10-27 18:34:43 +03:00
|
|
|
UserInfo ->
|
|
|
|
ApiLimit ->
|
|
|
|
m ResourceLimits
|
|
|
|
askGraphqlOperationLimit reqId userInfo apiLimit = lift $ askGraphqlOperationLimit reqId userInfo apiLimit
|
2021-09-29 19:20:06 +03:00
|
|
|
|
2023-05-24 16:51:56 +03:00
|
|
|
instance (HasResourceLimits m) => HasResourceLimits (ReaderT r m)
|
2021-09-29 19:20:06 +03:00
|
|
|
|
2023-05-24 16:51:56 +03:00
|
|
|
instance (HasResourceLimits m) => HasResourceLimits (ExceptT e m)
|
2021-09-29 19:20:06 +03:00
|
|
|
|
2023-05-24 16:51:56 +03:00
|
|
|
instance (HasResourceLimits m) => HasResourceLimits (Tracing.TraceT m)
|