-- SPDX-FileCopyrightText: 2026 Alexandra de Wit
--
-- SPDX-License-Identifier: MIT

-- | Byte-weighted admission over the shared weighted controller.
module Ecluse.Core.Server.Admission.Bytes.Internal (
    ByteAdmission (..),
    newByteAdmission,
    withByteAdmission,

    -- * Internals exported for testing
    newByteAdmissionTuned,
) where

import UnliftIO (MonadUnliftIO)

import Ecluse.Core.Deadline (Deadlines)
import Ecluse.Core.Server.Admission.Weighted (
    AdmissionObservers (..),
    WeightedAdmission,
    admissionWaitMicros,
    newWeightedAdmission,
    withWeightedAdmission,
 )
import Ecluse.Core.Telemetry.Record (MetricsPort (..))

-- | Process-wide byte capacity and bounded waiting-room state.
data ByteAdmission = ByteAdmission
    { ByteAdmission -> WeightedAdmission
baCore :: WeightedAdmission
    , ByteAdmission -> Int
baCapacity :: Int
    }

-- | Publishes use a small waiting room because each request can hold a large body.
byteAdmissionWaiterRoom :: Int
byteAdmissionWaiterRoom :: Int
byteAdmissionWaiterRoom = Int
8

-- | Allocate byte capacity, clamped to one, with the shared wait budget timed on the deadlines.
newByteAdmission :: Deadlines -> Int -> IO ByteAdmission
newByteAdmission :: Deadlines -> Int -> IO ByteAdmission
newByteAdmission Deadlines
deadlines Int
capacity = Deadlines -> Int -> Int -> Int -> IO ByteAdmission
newByteAdmissionTuned Deadlines
deadlines Int
capacity Int
byteAdmissionWaiterRoom Int
admissionWaitMicros

-- | Set the waiting-room bound and microsecond wait budget explicitly.
newByteAdmissionTuned :: Deadlines -> Int -> Int -> Int -> IO ByteAdmission
newByteAdmissionTuned :: Deadlines -> Int -> Int -> Int -> IO ByteAdmission
newByteAdmissionTuned Deadlines
deadlines 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 <- Deadlines -> Int -> Int -> Int -> IO WeightedAdmission
newWeightedAdmission Deadlines
deadlines Int
cap Int
room Int
waitMicros
    pure ByteAdmission{baCore = core, baCapacity = cap}

-- | Hold a weight clamped to capacity. A full room or expired wait returns Nothing.
{-# 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 =
    (Either Shed a -> Maybe a) -> m (Either Shed a) -> m (Maybe a)
forall a b. (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Either Shed a -> Maybe a
forall l r. Either l r -> Maybe r
rightToMaybe (m (Either Shed a) -> m (Maybe a))
-> (m a -> m (Either Shed a)) -> m a -> m (Maybe a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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 (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
            }