module Ecluse.Core.Server.Admission.Internal (
ServeAdmission (..),
newServeAdmission,
withServeAdmission,
serveAdmissionLine,
serveAdmissionOverflow,
newServeAdmissionTuned,
withOverflow,
) where
import Control.Exception (ErrorCall (ErrorCall))
import UnliftIO (MonadUnliftIO, throwIO)
import Ecluse.Core.Deadline (Deadlines)
import Ecluse.Core.Server.Admission.Overflow (Line, Overflow, newOverflow)
import Ecluse.Core.Server.Admission.Types (Shed)
import Ecluse.Core.Server.Admission.Weighted (
AdmissionObservers (..),
WeightedAdmission,
admissionWaitMicros,
newWeightedAdmission,
weightedAdmissionLine,
withWeightedAdmission,
)
import Ecluse.Core.Telemetry.Record (MetricsPort (..))
data ServeAdmission = ServeAdmission
{ ServeAdmission -> WeightedAdmission
saSlots :: WeightedAdmission
, ServeAdmission -> Maybe Overflow
saOverflow :: Maybe Overflow
}
newServeAdmission :: Deadlines -> Int -> IO ServeAdmission
newServeAdmission :: Deadlines -> Int -> IO ServeAdmission
newServeAdmission Deadlines
deadlines Int
capacity
| Int
capacity Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = ErrorCall -> IO ServeAdmission
forall (m :: * -> *) e a. (MonadIO m, Exception e) => e -> m a
throwIO (String -> ErrorCall
ErrorCall String
"ServeAdmission capacity must be positive")
| Bool
otherwise = do
slots <- Deadlines -> Int -> Int -> Int -> IO ServeAdmission
newServeAdmissionTuned Deadlines
deadlines Int
capacity Int
capacity Int
admissionWaitMicros
withOverflow slots <$> newOverflow deadlines capacity
newServeAdmissionTuned :: Deadlines -> Int -> Int -> Int -> IO ServeAdmission
newServeAdmissionTuned :: Deadlines -> Int -> Int -> Int -> IO ServeAdmission
newServeAdmissionTuned Deadlines
deadlines Int
capacity Int
room Int
waitMicros = do
slots <- Deadlines -> Int -> Int -> Int -> IO WeightedAdmission
newWeightedAdmission Deadlines
deadlines Int
capacity Int
room Int
waitMicros
pure ServeAdmission{saSlots = slots, saOverflow = Nothing}
withOverflow :: ServeAdmission -> Overflow -> ServeAdmission
withOverflow :: ServeAdmission -> Overflow -> ServeAdmission
withOverflow ServeAdmission
admission Overflow
overflow = ServeAdmission
admission{saOverflow = Just overflow}
serveAdmissionLine :: ServeAdmission -> Line
serveAdmissionLine :: ServeAdmission -> Line
serveAdmissionLine = WeightedAdmission -> Line
weightedAdmissionLine (WeightedAdmission -> Line)
-> (ServeAdmission -> WeightedAdmission) -> ServeAdmission -> Line
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ServeAdmission -> WeightedAdmission
saSlots
serveAdmissionOverflow :: ServeAdmission -> Maybe Overflow
serveAdmissionOverflow :: ServeAdmission -> Maybe Overflow
serveAdmissionOverflow = ServeAdmission -> Maybe Overflow
saOverflow
{-# INLINE withServeAdmission #-}
withServeAdmission :: (MonadUnliftIO m) => MetricsPort -> ServeAdmission -> m a -> m (Either Shed a)
withServeAdmission :: forall (m :: * -> *) a.
MonadUnliftIO m =>
MetricsPort -> ServeAdmission -> m a -> m (Either Shed a)
withServeAdmission MetricsPort
metrics ServeAdmission
admission =
AdmissionObservers
-> WeightedAdmission -> Int -> m a -> m (Either Shed a)
forall (m :: * -> *) a.
MonadUnliftIO m =>
AdmissionObservers
-> WeightedAdmission -> Int -> m a -> m (Either Shed a)
withWeightedAdmission AdmissionObservers
observers (ServeAdmission -> WeightedAdmission
saSlots ServeAdmission
admission) Int
1
where
observers :: AdmissionObservers
observers =
AdmissionObservers
{ onQueued :: IO ()
onQueued = MetricsPort -> IO ()
mpServeAdmissionQueued MetricsPort
metrics
, onShed :: IO ()
onShed = () -> IO ()
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
, onInFlightDelta :: Int -> IO ()
onInFlightDelta = MetricsPort -> Int -> IO ()
mpServeAdmissionInFlight MetricsPort
metrics
}