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

{- | Bound registry transfers by body progress during upstream waits.
Consumer time and waits for response headers do not count.
-}
module Ecluse.Core.Registry.Progress (
    -- * The progress floor
    Watch,
    watched,
    watchedRaising,
    isProgressFloorFailure,

    -- * Metered transfers
    meteredReader,
    meteredUpload,
) where

import Data.ByteString qualified as BS
import Data.ByteString.Builder (toLazyByteString)
import Data.ByteString.Lazy qualified as LBS
import GHC.Clock (getMonotonicTimeNSec)
import GHC.IORef qualified as IORef
import Network.HTTP.Client (BodyReader, GivesPopper, Popper, RequestBody (..))
import UnliftIO (try)

import Ecluse.Core.Deadline (Check, Deadlines, withDeadline)
import Ecluse.Core.Registry.Progress.Internal (Window, beginAt, endAt, freshWindow, waitedBy)
import Ecluse.Core.Security (ProgressFloor, floorWindowMicros)

-- | One transfer's progress in the current window, shared by its meters and the sweeper's check.
data Watch = Watch ProgressFloor (IORef Window)

-- Raised in a transfer's thread as a synchronous failure of the transfer, as a socket error would be.
data BelowProgressFloor = BelowProgressFloor
    deriving stock (Int -> BelowProgressFloor -> ShowS
[BelowProgressFloor] -> ShowS
BelowProgressFloor -> String
(Int -> BelowProgressFloor -> ShowS)
-> (BelowProgressFloor -> String)
-> ([BelowProgressFloor] -> ShowS)
-> Show BelowProgressFloor
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> BelowProgressFloor -> ShowS
showsPrec :: Int -> BelowProgressFloor -> ShowS
$cshow :: BelowProgressFloor -> String
show :: BelowProgressFloor -> String
$cshowList :: [BelowProgressFloor] -> ShowS
showList :: [BelowProgressFloor] -> ShowS
Show)

instance Exception BelowProgressFloor

-- | Identify the floor's transport-timeout interruption without exposing its constructor.
isProgressFloorFailure :: SomeException -> Bool
isProgressFloorFailure :: SomeException -> Bool
isProgressFloorFailure SomeException
fault = case SomeException -> Maybe BelowProgressFloor
forall e. Exception e => SomeException -> Maybe e
fromException SomeException
fault of
    Just BelowProgressFloor
BelowProgressFloor -> Bool
True
    Maybe BelowProgressFloor
Nothing -> Bool
False

-- | Return 'Nothing' below the floor. The catch outlives the check, so its interruption cannot escape later.
watched :: Deadlines -> ProgressFloor -> (Watch -> IO a) -> IO (Maybe a)
watched :: forall a.
Deadlines -> ProgressFloor -> (Watch -> IO a) -> IO (Maybe a)
watched Deadlines
deadlines ProgressFloor
progress Watch -> IO a
transfer =
    IO a -> IO (Either BelowProgressFloor a)
forall (m :: * -> *) e a.
(MonadUnliftIO m, Exception e) =>
m a -> m (Either e a)
try (Deadlines -> ProgressFloor -> (Watch -> IO a) -> IO a
forall a. Deadlines -> ProgressFloor -> (Watch -> IO a) -> IO a
watchedRaising Deadlines
deadlines ProgressFloor
progress Watch -> IO a
transfer) IO (Either BelowProgressFloor a)
-> (Either BelowProgressFloor a -> Maybe a) -> IO (Maybe a)
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \case
        Left BelowProgressFloor
BelowProgressFloor -> Maybe a
forall a. Maybe a
Nothing
        Right a
result -> a -> Maybe a
forall a. a -> Maybe a
Just a
result

-- | Raise a floor failure so an already committed response tears down. It lands about one sweep late.
watchedRaising :: Deadlines -> ProgressFloor -> (Watch -> IO a) -> IO a
watchedRaising :: forall a. Deadlines -> ProgressFloor -> (Watch -> IO a) -> IO a
watchedRaising Deadlines
deadlines ProgressFloor
progress Watch -> IO a
transfer = do
    watch <- ProgressFloor -> IORef Window -> Watch
Watch ProgressFloor
progress (IORef Window -> Watch) -> IO (IORef Window) -> IO Watch
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Window -> IO (IORef Window)
forall (m :: * -> *) a. MonadIO m => a -> m (IORef a)
newIORef Window
freshWindow
    withDeadline deadlines (belowFloor watch) (transfer watch)

-- The sweeper raises the plain exception, which the synchronous catch in 'watched' sees.
belowFloor :: Watch -> Check
belowFloor :: Watch -> Check
belowFloor (Watch ProgressFloor
progress IORef Window
window) Word64
now =
    IORef Window -> IO Window
forall (m :: * -> *) a. MonadIO m => IORef a -> m a
readIORef IORef Window
window IO Window
-> (Window -> Maybe SomeException) -> IO (Maybe SomeException)
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \Window
current ->
        if Word64 -> Window -> Word64
waitedBy Word64
now Window
current Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
>= Word64
budget then SomeException -> Maybe SomeException
forall a. a -> Maybe a
Just (BelowProgressFloor -> SomeException
forall e. Exception e => e -> SomeException
toException BelowProgressFloor
BelowProgressFloor) else Maybe SomeException
forall a. Maybe a
Nothing
  where
    budget :: Word64
budget = Int -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ProgressFloor -> Int
floorWindowMicros ProgressFloor
progress) Word64 -> Word64 -> Word64
forall a. Num a => a -> a -> a
* Word64
1_000

-- | A reader whose every read counts as a wait on the upstream, and its chunk as progress.
meteredReader :: Watch -> BodyReader -> BodyReader
meteredReader :: Watch -> BodyReader -> BodyReader
meteredReader Watch
watch BodyReader
readChunk = do
    Watch -> IO ()
beginWait Watch
watch
    chunk <- BodyReader
readChunk
    endWait watch (BS.length chunk)
    pure chunk

-- | Count each slice once written. An empty body has no upload to watch.
meteredUpload :: Watch -> RequestBody -> RequestBody
meteredUpload :: Watch -> RequestBody -> RequestBody
meteredUpload Watch
watch RequestBody
body = case RequestBody
body of
    RequestBodyLBS ByteString
bytes | Bool -> Bool
not (ByteString -> Bool
LBS.null ByteString
bytes) -> Int64 -> [ByteString] -> RequestBody
fromChunks (ByteString -> Int64
LBS.length ByteString
bytes) (ByteString -> [ByteString]
LBS.toChunks ByteString
bytes)
    RequestBodyBS ByteString
bytes | Bool -> Bool
not (ByteString -> Bool
BS.null ByteString
bytes) -> Int64 -> [ByteString] -> RequestBody
fromChunks (Int -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString -> Int
BS.length ByteString
bytes)) [ByteString
bytes]
    RequestBodyBuilder Int64
size Builder
builder | Int64
size Int64 -> Int64 -> Bool
forall a. Ord a => a -> a -> Bool
> Int64
0 -> Int64 -> [ByteString] -> RequestBody
fromChunks Int64
size (ByteString -> [ByteString]
LBS.toChunks (Builder -> ByteString
toLazyByteString Builder
builder))
    RequestBodyStream Int64
size GivesPopper ()
gives -> Int64 -> GivesPopper () -> RequestBody
RequestBodyStream Int64
size (Watch -> GivesPopper () -> GivesPopper ()
meteredGives Watch
watch GivesPopper ()
gives)
    RequestBodyStreamChunked GivesPopper ()
gives -> GivesPopper () -> RequestBody
RequestBodyStreamChunked (Watch -> GivesPopper () -> GivesPopper ()
meteredGives Watch
watch GivesPopper ()
gives)
    RequestBodyIO IO RequestBody
io -> IO RequestBody -> RequestBody
RequestBodyIO (Watch -> RequestBody -> RequestBody
meteredUpload Watch
watch (RequestBody -> RequestBody) -> IO RequestBody -> IO RequestBody
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO RequestBody
io)
    RequestBody
_ -> RequestBody
body
  where
    fromChunks :: Int64 -> [ByteString] -> RequestBody
fromChunks Int64
size [ByteString]
chunks = Int64 -> GivesPopper () -> RequestBody
RequestBodyStream Int64
size (Watch -> GivesPopper () -> GivesPopper ()
meteredGives Watch
watch ([ByteString] -> GivesPopper ()
givesChunks [ByteString]
chunks))

givesChunks :: [ByteString] -> GivesPopper ()
givesChunks :: [ByteString] -> GivesPopper ()
givesChunks [ByteString]
chunks NeedsPopper ()
needsPopper = do
    remaining <- [ByteString] -> IO (IORef [ByteString])
forall (m :: * -> *) a. MonadIO m => a -> m (IORef a)
newIORef [ByteString]
chunks
    needsPopper . atomicModifyIORef' remaining $ \case
        [] -> ([], ByteString
BS.empty)
        ByteString
chunk : [ByteString]
rest -> ([ByteString]
rest, ByteString
chunk)

{- The connection asks for the next slice only once it has written the last, so each call closes
the wait that slice opened. Slicing keeps a large chunk from counting only when it all lands. -}
meteredGives :: Watch -> GivesPopper () -> GivesPopper ()
meteredGives :: Watch -> GivesPopper () -> GivesPopper ()
meteredGives Watch
watch GivesPopper ()
gives NeedsPopper ()
needsPopper = GivesPopper ()
gives GivesPopper () -> GivesPopper ()
forall a b. (a -> b) -> a -> b
$ \BodyReader
popper -> do
    handed <- Int -> IO (IORef Int)
forall (m :: * -> *) a. MonadIO m => a -> m (IORef a)
newIORef Int
0
    pending <- newIORef BS.empty
    needsPopper $ do
        readIORef handed >>= \Int
size -> Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
size Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0) (Watch -> Int -> IO ()
endWait Watch
watch Int
size)
        slice <- nextSlice pending popper
        writeIORef handed (BS.length slice)
        unless (BS.null slice) (beginWait watch)
        pure slice

nextSlice :: IORef ByteString -> Popper -> IO ByteString
nextSlice :: IORef ByteString -> BodyReader -> BodyReader
nextSlice IORef ByteString
pending BodyReader
popper = do
    held <- IORef ByteString -> BodyReader
forall (m :: * -> *) a. MonadIO m => IORef a -> m a
readIORef IORef ByteString
pending
    chunk <- if BS.null held then popper else pure held
    let (slice, rest) = BS.splitAt uploadSliceBytes chunk
    writeIORef pending rest
    pure slice

uploadSliceBytes :: Int
uploadSliceBytes :: Int
uploadSliceBytes = Int
64 Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
1024

-- GHC.IORef's atomicModifyIORef'_ builds no pair per update, unlike relude's namesake.
beginWait :: Watch -> IO ()
beginWait :: Watch -> IO ()
beginWait (Watch ProgressFloor
_ IORef Window
window) = do
    now <- IO Word64
getMonotonicTimeNSec
    void (IORef.atomicModifyIORef'_ window (beginAt now))

endWait :: Watch -> Int -> IO ()
endWait :: Watch -> Int -> IO ()
endWait (Watch ProgressFloor
progress IORef Window
window) Int
bytes = do
    now <- IO Word64
getMonotonicTimeNSec
    void (IORef.atomicModifyIORef'_ window (endAt progress now bytes))