module Ecluse.Core.Server.Admission.Bytes (
ByteAdmission,
newByteAdmission,
withByteAdmission,
newByteAdmissionTuned,
) where
import UnliftIO (MonadUnliftIO)
import Ecluse.Core.Server.Admission.Weighted (
AdmissionObservers (..),
WeightedAdmission,
admissionWaitMicros,
newWeightedAdmission,
withWeightedAdmission,
)
import Ecluse.Core.Telemetry.Record (MetricsPort (..))
data ByteAdmission = ByteAdmission
{ ByteAdmission -> WeightedAdmission
baCore :: WeightedAdmission
, ByteAdmission -> Int
baCapacity :: Int
}
byteAdmissionWaiterRoom :: Int
byteAdmissionWaiterRoom :: Int
byteAdmissionWaiterRoom = Int
8
newByteAdmission :: Int -> IO ByteAdmission
newByteAdmission :: Int -> IO ByteAdmission
newByteAdmission Int
capacity = Int -> Int -> Int -> IO ByteAdmission
newByteAdmissionTuned Int
capacity Int
byteAdmissionWaiterRoom Int
admissionWaitMicros
newByteAdmissionTuned :: Int -> Int -> Int -> IO ByteAdmission
newByteAdmissionTuned :: Int -> Int -> Int -> IO ByteAdmission
newByteAdmissionTuned Int
capacity Int
room Int
waitMicros = do
let cap :: Int
cap = Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
1 Int
capacity
core <- Int -> Int -> Int -> IO WeightedAdmission
newWeightedAdmission Int
cap Int
room Int
waitMicros
pure ByteAdmission{baCore = core, baCapacity = cap}
{-# INLINE withByteAdmission #-}
withByteAdmission :: (MonadUnliftIO m) => MetricsPort -> ByteAdmission -> Int -> m a -> m (Maybe a)
withByteAdmission :: forall (m :: * -> *) a.
MonadUnliftIO m =>
MetricsPort -> ByteAdmission -> Int -> m a -> m (Maybe a)
withByteAdmission MetricsPort
metrics ByteAdmission
ba Int
rawWeight =
AdmissionObservers
-> WeightedAdmission -> Int -> m a -> m (Maybe a)
forall (m :: * -> *) a.
MonadUnliftIO m =>
AdmissionObservers
-> WeightedAdmission -> Int -> m a -> m (Maybe a)
withWeightedAdmission AdmissionObservers
observers (ByteAdmission -> WeightedAdmission
baCore ByteAdmission
ba) Int
weight
where
weight :: Int
weight = Int -> Int -> Int
forall a. Ord a => a -> a -> a
min (ByteAdmission -> Int
baCapacity ByteAdmission
ba) (Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
0 Int
rawWeight)
observers :: AdmissionObservers
observers =
AdmissionObservers
{ onQueued :: IO ()
onQueued = () -> IO ()
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
, onShed :: IO ()
onShed = MetricsPort -> IO ()
mpPublishBodyShed MetricsPort
metrics
, onInFlightDelta :: Int -> IO ()
onInFlightDelta = MetricsPort -> Int -> IO ()
mpPublishBodyInFlightBytes MetricsPort
metrics
}