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

{- | Resolve caller-authorised private metadata and anonymous public metadata.
Access refusals, absent packages and unread origins retain distinct outcomes, and an unread
origin keeps the cause that sets a private refusal's status.
-}
module Ecluse.Core.Server.Pipeline.Origin (
    -- * The per-origin outcome
    OriginResult (..),
    OriginMiss (..),
    ReadFailure (..),
    ObservedReadBoundExceeded (..),
    originManifest,
    originMiss,

    -- * Why a read failed
    statusReadFailure,
    fetchReadFailure,
    metadataReadFailure,
    readFailureRefusal,

    -- * Fetching the two origins
    fetchPrivateOriginObserved,
    fetchPublicOriginObserved,
    withPublicMetadata,
    withPrivateMetadataClient,

    -- * One origin's coordinates
    mountOrigin,
    publicSource,
) where

import Katip (Severity (DebugS), logFM, ls)
import UnliftIO (withRunInIO)
import UnliftIO.Exception (tryAny)

import Ecluse.Core.Credential (ClientCredential)
import Ecluse.Core.Fault (RetryAfter)
import Ecluse.Core.Package (PackageName, renderPackageName)
import Ecluse.Core.Registry (FetchFault (FetchBoundExceeded, FetchCooldown, FetchTransport, FetchUrlUnformable))
import Ecluse.Core.Registry.Adapter.Capability (AdapterMetadata (metadataChargeFactors, metadataRead))
import Ecluse.Core.Registry.Exchange.Types (ExchangeClient (ecDeadlines))
import Ecluse.Core.Registry.Metadata (
    Manifest (manifestBodyBytes, manifestRetainedEntries),
    MetadataClient (fetchFullManifest),
    MetadataError (
        MetadataAbsent,
        MetadataAuthorisationFailure,
        MetadataBoundExceeded,
        MetadataFetch,
        MetadataHttpFailure,
        MetadataNameMismatch,
        MetadataUndecodable
    ),
    VersionDoc (..),
    VersionRead (..),
 )
import Ecluse.Core.Registry.Metadata.EntryFloor (entryFloorCharge)
import Ecluse.Core.Registry.Metadata.Fetch.Preparation (canChargeFullReadWorkspace)
import Ecluse.Core.Registry.Origin (OriginClient (ocClient), OriginFor, Private, Public, anonymousOrigin, chargingFullReadWorkspace, chargingFullReads, chargingNativeReads, controlPublicOrigin, originBaseUrl, originClientOf, perCallerOrigin)
import Ecluse.Core.Registry.Request (BaseRequest)
import Ecluse.Core.Security (LimitError, Limits (progressFloor))
import Ecluse.Core.Security.Egress (registryUrlText)
import Ecluse.Core.Server.Admission.Budget (scaleCharge)
import Ecluse.Core.Server.Admission.Meter (ChargePart, MemoryTicket, awaitingFlight, charge, chargePart, newChargeRegion, servingFlight, servingPart, stepDown)
import Ecluse.Core.Server.Admission.Types (ChargeFactors (cfFullReadPermille, cfKeptPermille))
import Ecluse.Core.Server.Cache.Store (executePrepared)
import Ecluse.Core.Server.Cache.Types (Source (Source), fullKey, versionKey)
import Ecluse.Core.Server.Cache.VersionWeight (weighVersion)
import Ecluse.Core.Server.Context (
    Handler,
    PackumentDeps (..),
    ServeRuntime (..),
    pdPrivateBase,
    pdPublicBase,
    pdPublicBaseUrl,
 )
import Ecluse.Core.Server.Metadata (MetadataReads, ecosystemMetadataReads, preparePublicVersion, privateMetadataClient, publicMetadataClient, withinRequestCap)
import Ecluse.Core.Server.Pipeline.Diagnostics (logMetadataFailure, logPackumentDrops)
import Ecluse.Core.Server.Pipeline.Shared (flightOf)
import Ecluse.Core.Server.Response (
    RejectReason (UpstreamInvalid),
    Rejection (Rejection),
    ServeDecision (Reject),
    Transience (WillResolve),
    rejectUnavailable,
 )
import Ecluse.Core.Version (Version)

-- | A request-wide observed read limit, distinct from a transport failure.
newtype ObservedReadBoundExceeded = ObservedReadBoundExceeded LimitError
    deriving stock (Int -> ObservedReadBoundExceeded -> ShowS
[ObservedReadBoundExceeded] -> ShowS
ObservedReadBoundExceeded -> String
(Int -> ObservedReadBoundExceeded -> ShowS)
-> (ObservedReadBoundExceeded -> String)
-> ([ObservedReadBoundExceeded] -> ShowS)
-> Show ObservedReadBoundExceeded
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ObservedReadBoundExceeded -> ShowS
showsPrec :: Int -> ObservedReadBoundExceeded -> ShowS
$cshow :: ObservedReadBoundExceeded -> String
show :: ObservedReadBoundExceeded -> String
$cshowList :: [ObservedReadBoundExceeded] -> ShowS
showList :: [ObservedReadBoundExceeded] -> ShowS
Show)

instance Exception ObservedReadBoundExceeded

-- | One origin's contribution, access refusal, identity mismatch, or absence.
data OriginResult
    = -- | The request-wide observed read bound was crossed.
      OriginBudgetExceeded LimitError
    | -- | A packument that decoded and whose self-reported name matched the request.
      OriginResolved Manifest
    | {- | An explicit upstream access refusal, retaining its 401 or 403. A public one refuses a listing
      with nothing admitted as an invalid reply, because the public read is anonymous.
      -}
      OriginAuthorisationFailure Int
    | {- | An invalid package identity. A public one contributes a 502 when no valid origin remains,
      and a private one refuses the request.
      -}
      OriginNameMismatch
    | -- | The origin answered 404, so it holds no such package. It degrades to no contribution.
      OriginNotFound
    | {- | The origin was not read, for this cause. A private one refuses the request. A public one
      contributes nothing, and when no version survives its cause refuses the listing as a private
      one would.
      -}
      OriginUnresolved ReadFailure
    | -- | Public reads are suspended until the authority cooldown expires.
      OriginCooling RetryAfter
    | -- | An unconfigured origin contributes neither metadata nor an availability failure.
      OriginAbsent

-- | Distinguish a settled absence from an unread origin that a retry may resolve.
data OriginMiss
    = -- | The origin holds no such package, or the mount configures no such origin.
      MissAbsent
    | -- | The origin was not read, so nothing yet says whether it holds the package.
      MissUnresolved ReadFailure
    deriving stock (OriginMiss -> OriginMiss -> Bool
(OriginMiss -> OriginMiss -> Bool)
-> (OriginMiss -> OriginMiss -> Bool) -> Eq OriginMiss
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: OriginMiss -> OriginMiss -> Bool
== :: OriginMiss -> OriginMiss -> Bool
$c/= :: OriginMiss -> OriginMiss -> Bool
/= :: OriginMiss -> OriginMiss -> Bool
Eq, Int -> OriginMiss -> ShowS
[OriginMiss] -> ShowS
OriginMiss -> String
(Int -> OriginMiss -> ShowS)
-> (OriginMiss -> String)
-> ([OriginMiss] -> ShowS)
-> Show OriginMiss
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> OriginMiss -> ShowS
showsPrec :: Int -> OriginMiss -> ShowS
$cshow :: OriginMiss -> String
show :: OriginMiss -> String
$cshowList :: [OriginMiss] -> ShowS
showList :: [OriginMiss] -> ShowS
Show)

-- | Why a read failed, which sets the status of a refusal it causes.
data ReadFailure
    = -- | @503@: a transport failure, a timeout, a @5xx@ or a @429@, with any delay the origin asked for.
      ReadUnavailable (Maybe RetryAfter)
    | -- | @502@: an unfollowed @3xx@, another unexpected status, a name mismatch, an undecodable document or a bound breach.
      ReadInvalid
    deriving stock (ReadFailure -> ReadFailure -> Bool
(ReadFailure -> ReadFailure -> Bool)
-> (ReadFailure -> ReadFailure -> Bool) -> Eq ReadFailure
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ReadFailure -> ReadFailure -> Bool
== :: ReadFailure -> ReadFailure -> Bool
$c/= :: ReadFailure -> ReadFailure -> Bool
/= :: ReadFailure -> ReadFailure -> Bool
Eq, Int -> ReadFailure -> ShowS
[ReadFailure] -> ShowS
ReadFailure -> String
(Int -> ReadFailure -> ShowS)
-> (ReadFailure -> String)
-> ([ReadFailure] -> ShowS)
-> Show ReadFailure
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ReadFailure -> ShowS
showsPrec :: Int -> ReadFailure -> ShowS
$cshow :: ReadFailure -> String
show :: ReadFailure -> String
$cshowList :: [ReadFailure] -> ShowS
showList :: [ReadFailure] -> ShowS
Show)

-- | Classify an unsuccessful status that is neither a 404 nor an access refusal.
statusReadFailure :: Int -> Maybe RetryAfter -> ReadFailure
statusReadFailure :: Int -> Maybe RetryAfter -> ReadFailure
statusReadFailure Int
code Maybe RetryAfter
retry
    | Int
code Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
429 Bool -> Bool -> Bool
|| (Int
code Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
500 Bool -> Bool -> Bool
&& Int
code Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
600) = Maybe RetryAfter -> ReadFailure
ReadUnavailable Maybe RetryAfter
retry
    | Bool
otherwise = ReadFailure
ReadInvalid

-- | The cause of a failed metadata read, or 'Nothing' for an access refusal or a settled absence.
metadataReadFailure :: MetadataError -> Maybe ReadFailure
metadataReadFailure :: MetadataError -> Maybe ReadFailure
metadataReadFailure = \case
    MetadataAuthorisationFailure Int
_ -> Maybe ReadFailure
forall a. Maybe a
Nothing
    MetadataError
MetadataAbsent -> Maybe ReadFailure
forall a. Maybe a
Nothing
    MetadataHttpFailure Int
code Maybe RetryAfter
retry -> ReadFailure -> Maybe ReadFailure
forall a. a -> Maybe a
Just (Int -> Maybe RetryAfter -> ReadFailure
statusReadFailure Int
code Maybe RetryAfter
retry)
    MetadataFetch FetchFault
fault -> ReadFailure -> Maybe ReadFailure
forall a. a -> Maybe a
Just (FetchFault -> ReadFailure
fetchReadFailure FetchFault
fault)
    MetadataBoundExceeded LimitError
_ -> ReadFailure -> Maybe ReadFailure
forall a. a -> Maybe a
Just ReadFailure
ReadInvalid
    MetadataError
MetadataUndecodable -> ReadFailure -> Maybe ReadFailure
forall a. a -> Maybe a
Just ReadFailure
ReadInvalid
    MetadataNameMismatch Text
_ -> ReadFailure -> Maybe ReadFailure
forall a. a -> Maybe a
Just ReadFailure
ReadInvalid

{- | The refusal a failed read of the named upstream renders: @503@ under the given words with its
delay, or @502@ naming that upstream for an invalid reply.
-}
readFailureRefusal :: Text -> Text -> ReadFailure -> ServeDecision
readFailureRefusal :: Text -> Text -> ReadFailure -> ServeDecision
readFailureRefusal Text
upstream Text
unavailable = \case
    ReadUnavailable Maybe RetryAfter
retry -> Transience -> Text -> ServeDecision
rejectUnavailable (Maybe RetryAfter -> Transience
WillResolve Maybe RetryAfter
retry) Text
unavailable
    ReadFailure
ReadInvalid -> Rejection -> ServeDecision
Reject (RejectReason -> Text -> Rejection
Rejection RejectReason
UpstreamInvalid (Text
upstream Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" returned an invalid or unusable reply"))

-- | Classify a failed exchange. An unformable URL sent no request, and keeps the unavailable refusal.
fetchReadFailure :: FetchFault -> ReadFailure
fetchReadFailure :: FetchFault -> ReadFailure
fetchReadFailure = \case
    FetchBoundExceeded LimitError
_ -> ReadFailure
ReadInvalid
    FetchTransport TransportFault
_ -> Maybe RetryAfter -> ReadFailure
ReadUnavailable Maybe RetryAfter
forall a. Maybe a
Nothing
    FetchCooldown RetryAfter
retry -> Maybe RetryAfter -> ReadFailure
ReadUnavailable (RetryAfter -> Maybe RetryAfter
forall a. a -> Maybe a
Just RetryAfter
retry)
    FetchUrlUnformable UrlFormationError
_ -> Maybe RetryAfter -> ReadFailure
ReadUnavailable Maybe RetryAfter
forall a. Maybe a
Nothing

-- | The resolved manifest an origin contributed, if any.
originManifest :: OriginResult -> Maybe Manifest
originManifest :: OriginResult -> Maybe Manifest
originManifest = \case
    OriginBudgetExceeded LimitError
_ -> Maybe Manifest
forall a. Maybe a
Nothing
    OriginAuthorisationFailure Int
_ -> Maybe Manifest
forall a. Maybe a
Nothing
    OriginResolved Manifest
manifest -> Manifest -> Maybe Manifest
forall a. a -> Maybe a
Just Manifest
manifest
    OriginResult
OriginNameMismatch -> Maybe Manifest
forall a. Maybe a
Nothing
    OriginResult
OriginNotFound -> Maybe Manifest
forall a. Maybe a
Nothing
    OriginUnresolved ReadFailure
_ -> Maybe Manifest
forall a. Maybe a
Nothing
    OriginCooling RetryAfter
_ -> Maybe Manifest
forall a. Maybe a
Nothing
    OriginResult
OriginAbsent -> Maybe Manifest
forall a. Maybe a
Nothing

-- | The miss an origin yielded, or 'Nothing' when it contributed a document or an access refusal.
originMiss :: OriginResult -> Maybe OriginMiss
originMiss :: OriginResult -> Maybe OriginMiss
originMiss = \case
    OriginBudgetExceeded LimitError
_ -> OriginMiss -> Maybe OriginMiss
forall a. a -> Maybe a
Just (ReadFailure -> OriginMiss
MissUnresolved ReadFailure
ReadInvalid)
    OriginAuthorisationFailure Int
_ -> Maybe OriginMiss
forall a. Maybe a
Nothing
    OriginResolved{} -> Maybe OriginMiss
forall a. Maybe a
Nothing
    OriginResult
OriginNameMismatch -> OriginMiss -> Maybe OriginMiss
forall a. a -> Maybe a
Just (ReadFailure -> OriginMiss
MissUnresolved ReadFailure
ReadInvalid)
    OriginResult
OriginNotFound -> OriginMiss -> Maybe OriginMiss
forall a. a -> Maybe a
Just OriginMiss
MissAbsent
    OriginUnresolved ReadFailure
failure -> OriginMiss -> Maybe OriginMiss
forall a. a -> Maybe a
Just (ReadFailure -> OriginMiss
MissUnresolved ReadFailure
failure)
    OriginCooling RetryAfter
retry -> OriginMiss -> Maybe OriginMiss
forall a. a -> Maybe a
Just (ReadFailure -> OriginMiss
MissUnresolved (Maybe RetryAfter -> ReadFailure
ReadUnavailable (RetryAfter -> Maybe RetryAfter
forall a. a -> Maybe a
Just RetryAfter
retry)))
    OriginResult
OriginAbsent -> OriginMiss -> Maybe OriginMiss
forall a. a -> Maybe a
Just OriginMiss
MissAbsent

originResultOf :: Either SomeException (Either MetadataError Manifest) -> OriginResult
originResultOf :: Either SomeException (Either MetadataError Manifest)
-> OriginResult
originResultOf = \case
    Left SomeException
fault | Just (ObservedReadBoundExceeded LimitError
bound) <- SomeException -> Maybe ObservedReadBoundExceeded
forall e. Exception e => SomeException -> Maybe e
fromException SomeException
fault -> LimitError -> OriginResult
OriginBudgetExceeded LimitError
bound
    Left SomeException
_ -> ReadFailure -> OriginResult
OriginUnresolved (Maybe RetryAfter -> ReadFailure
ReadUnavailable Maybe RetryAfter
forall a. Maybe a
Nothing)
    Right (Left MetadataError
err) -> case MetadataError
err of
        MetadataAuthorisationFailure Int
code -> Int -> OriginResult
OriginAuthorisationFailure Int
code
        MetadataNameMismatch Text
_ -> OriginResult
OriginNameMismatch
        MetadataError
MetadataAbsent -> OriginResult
OriginNotFound
        MetadataFetch (FetchCooldown RetryAfter
retry) -> RetryAfter -> OriginResult
OriginCooling RetryAfter
retry
        MetadataHttpFailure Int
code Maybe RetryAfter
retry -> ReadFailure -> OriginResult
OriginUnresolved (Int -> Maybe RetryAfter -> ReadFailure
statusReadFailure Int
code Maybe RetryAfter
retry)
        MetadataFetch FetchFault
fault -> ReadFailure -> OriginResult
OriginUnresolved (FetchFault -> ReadFailure
fetchReadFailure FetchFault
fault)
        MetadataBoundExceeded LimitError
_ -> ReadFailure -> OriginResult
OriginUnresolved ReadFailure
ReadInvalid
        MetadataError
MetadataUndecodable -> ReadFailure -> OriginResult
OriginUnresolved ReadFailure
ReadInvalid
    Right (Right Manifest
manifest) -> Manifest -> OriginResult
OriginResolved Manifest
manifest

-- | Read caller-authorised private metadata without caching and observe every incoming full-read chunk.
fetchPrivateOriginObserved :: (Int -> IO ()) -> PackumentDeps -> ServeRuntime -> ChargePart -> Maybe ClientCredential -> PackageName -> Handler OriginResult
fetchPrivateOriginObserved :: (Int -> IO ())
-> PackumentDeps
-> ServeRuntime
-> ChargePart
-> Maybe ClientCredential
-> PackageName
-> Handler OriginResult
fetchPrivateOriginObserved Int -> IO ()
observe PackumentDeps
deps ServeRuntime
rt ChargePart
part Maybe ClientCredential
token PackageName
name = case PackumentDeps -> Maybe BaseRequest
pdPrivateBase PackumentDeps
deps of
    Maybe BaseRequest
Nothing -> OriginResult -> Handler OriginResult
forall a. a -> Handler a
forall (f :: * -> *) a. Applicative f => a -> f a
pure OriginResult
OriginAbsent
    Just BaseRequest
privateBase -> do
        Severity -> LogStr -> Handler ()
forall (m :: * -> *).
(Applicative m, KatipContext m) =>
Severity -> LogStr -> m ()
logFM Severity
DebugS (Text -> LogStr
forall a. StringConv a Text => a -> LogStr
ls (Text
"fetching private origin for " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> PackageName -> Text
renderPackageName PackageName
name))
        origin <- IO (OriginFor Private) -> Handler (OriginFor Private)
forall a. IO a -> Handler a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO ((Int -> IO ())
-> PackumentDeps
-> ChargePart
-> OriginFor Private
-> IO (OriginFor Private)
forall posture.
(Int -> IO ())
-> PackumentDeps
-> ChargePart
-> OriginFor posture
-> IO (OriginFor posture)
fullReadOrigin Int -> IO ()
observe PackumentDeps
deps ChargePart
part (ServeRuntime
-> PackumentDeps
-> BaseRequest
-> Maybe ClientCredential
-> OriginFor Private
privateOrigin ServeRuntime
rt PackumentDeps
deps BaseRequest
privateBase Maybe ClientCredential
token))
        keepingRead deps part . originResultOf =<< tryAny (withMetadataClient rt deps privateMetadataClient origin (`fetchFullManifest` name))

-- | Cache anonymous public metadata and observe shared-read leader chunks. Cache consumers retain their own document accounting.
fetchPublicOriginObserved :: (Int -> IO ()) -> PackumentDeps -> ServeRuntime -> MemoryTicket -> ChargePart -> PackageName -> Handler OriginResult
fetchPublicOriginObserved :: (Int -> IO ())
-> PackumentDeps
-> ServeRuntime
-> MemoryTicket
-> ChargePart
-> PackageName
-> Handler OriginResult
fetchPublicOriginObserved Int -> IO ()
observe PackumentDeps
deps ServeRuntime
rt MemoryTicket
ticket ChargePart
part PackageName
name = do
    Severity -> LogStr -> Handler ()
forall (m :: * -> *).
(Applicative m, KatipContext m) =>
Severity -> LogStr -> m ()
logFM Severity
DebugS (Text -> LogStr
forall a. StringConv a Text => a -> LogStr
ls (Text
"fetching public origin for " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> PackageName -> Text
renderPackageName PackageName
name))
    -- Every request that shares this read waits on it, so whichever request leads it pays with their priority.
    let flight :: FlightKey
flight = CacheKey -> FlightKey
flightOf (Source -> PackageName -> CacheKey
fullKey (PackumentDeps -> Source
publicSource PackumentDeps
deps) PackageName
name)
        sharedPart :: ChargePart
sharedPart = FlightKey -> ChargePart -> ChargePart
servingPart FlightKey
flight ChargePart
part
    origin <- IO (OriginFor Public) -> Handler (OriginFor Public)
forall a. IO a -> Handler a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO ((Int -> IO ())
-> PackumentDeps
-> ChargePart
-> OriginFor Public
-> IO (OriginFor Public)
forall posture.
(Int -> IO ())
-> PackumentDeps
-> ChargePart
-> OriginFor posture
-> IO (OriginFor posture)
fullReadOrigin Int -> IO ()
observe PackumentDeps
deps ChargePart
sharedPart (ServeRuntime -> PackumentDeps -> OriginFor Public
publicOrigin ServeRuntime
rt PackumentDeps
deps))
    keepingRead deps part . originResultOf =<< tryAny (awaitingFlight ticket flight (withMetadataClient rt deps (publicMetadataClient (srMetadataCache rt) (publicSource deps)) origin (`fetchFullManifest` name)))

-- Once its result is evaluated a read holds only its manifest, so its part steps down to the kept charge.
keepingRead :: PackumentDeps -> ChargePart -> OriginResult -> Handler OriginResult
keepingRead :: PackumentDeps -> ChargePart -> OriginResult -> Handler OriginResult
keepingRead PackumentDeps
deps ChargePart
part OriginResult
result = do
    settled <- OriginResult -> Handler OriginResult
forall (m :: * -> *) a. MonadIO m => a -> m a
evaluateWHNF OriginResult
result
    liftIO (stepDown part (keptCharge deps settled))
    pure settled

-- A manifest's bytes at the kept charge, or the floors for the entries its finished document keeps when that is larger.
keptCharge :: PackumentDeps -> OriginResult -> Int
keptCharge :: PackumentDeps -> OriginResult -> Int
keptCharge PackumentDeps
deps = \case
    OriginResolved Manifest
manifest -> Int -> Int -> Int
forall a. Ord a => a -> a -> a
max (Int -> Int -> Int
scaleCharge (ChargeFactors -> Int
cfKeptPermille (PackumentDeps -> ChargeFactors
chargeFactors PackumentDeps
deps)) (Manifest -> Int
manifestBodyBytes Manifest
manifest)) (RetainedEntries -> Int
entryFloorCharge (Manifest -> RetainedEntries
manifestRetainedEntries Manifest
manifest))
    OriginResult
_ -> Int
0

-- Origin logs use the request's logging context and its selected upstream.
withMetadataClient ::
    ServeRuntime ->
    PackumentDeps ->
    (MetadataReads posture -> client) ->
    OriginFor posture ->
    (client -> IO a) ->
    Handler a
withMetadataClient :: forall posture client a.
ServeRuntime
-> PackumentDeps
-> (MetadataReads posture -> client)
-> OriginFor posture
-> (client -> IO a)
-> Handler a
withMetadataClient ServeRuntime
rt PackumentDeps
deps MetadataReads posture -> client
settle OriginFor posture
origin client -> IO a
k =
    ((forall a. Handler a -> IO a) -> IO a) -> Handler a
forall b. ((forall a. Handler a -> IO a) -> IO b) -> Handler b
forall (m :: * -> *) b.
MonadUnliftIO m =>
((forall a. m a -> IO a) -> IO b) -> m b
withRunInIO (((forall a. Handler a -> IO a) -> IO a) -> Handler a)
-> ((forall a. Handler a -> IO a) -> IO a) -> Handler a
forall a b. (a -> b) -> a -> b
$ \forall a. Handler a -> IO a
runInIO ->
        client -> IO a
k (client -> IO a)
-> (MetadataReads posture -> client)
-> MetadataReads posture
-> IO a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MetadataReads posture -> client
settle (MetadataReads posture -> client)
-> (MetadataReads posture -> MetadataReads posture)
-> MetadataReads posture
-> client
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Deadlines
-> ProgressFloor -> MetadataReads posture -> MetadataReads posture
forall posture.
Deadlines
-> ProgressFloor -> MetadataReads posture -> MetadataReads posture
withinRequestCap (ExchangeClient -> Deadlines
ecDeadlines (OriginClient -> ExchangeClient
ocClient (OriginFor posture -> OriginClient
forall posture. OriginFor posture -> OriginClient
originClientOf OriginFor posture
origin))) (Limits -> ProgressFloor
progressFloor (PackumentDeps -> Limits
pdLimits PackumentDeps
deps)) (MetadataReads posture -> IO a) -> MetadataReads posture -> IO a
forall a b. (a -> b) -> a -> b
$
            PrepareRead
-> TracingPort
-> MetricsPort
-> (PackageName -> MetadataError -> IO ())
-> (PackageName -> PackageInfo -> IO ())
-> (PackageName -> IO ())
-> OriginFor posture
-> MetadataReads posture
forall posture.
PrepareRead
-> TracingPort
-> MetricsPort
-> (PackageName -> MetadataError -> IO ())
-> (PackageName -> PackageInfo -> IO ())
-> (PackageName -> IO ())
-> OriginFor posture
-> MetadataReads posture
ecosystemMetadataReads
                (AdapterMetadata -> PrepareRead
metadataRead (PackumentDeps -> AdapterMetadata
pdMetadata PackumentDeps
deps))
                (ServeRuntime -> TracingPort
srTracing ServeRuntime
rt)
                (ServeRuntime -> MetricsPort
srMetrics ServeRuntime
rt)
                (\PackageName
nm MetadataError
err -> Handler () -> IO ()
forall a. Handler a -> IO a
runInIO (PackageName -> Text -> MetadataError -> Handler ()
forall (m :: * -> *).
KatipContext m =>
PackageName -> Text -> MetadataError -> m ()
logMetadataFailure PackageName
nm Text
baseUrl MetadataError
err))
                (\PackageName
nm PackageInfo
info -> Handler () -> IO ()
forall a. Handler a -> IO a
runInIO (PackageName -> Text -> PackageInfo -> Handler ()
forall (m :: * -> *).
KatipContext m =>
PackageName -> Text -> PackageInfo -> m ()
logPackumentDrops PackageName
nm Text
baseUrl PackageInfo
info))
                (\PackageName
nm -> Handler () -> IO ()
forall a. Handler a -> IO a
runInIO (Severity -> LogStr -> Handler ()
forall (m :: * -> *).
(Applicative m, KatipContext m) =>
Severity -> LogStr -> m ()
logFM Severity
DebugS (Text -> LogStr
forall a. StringConv a Text => a -> LogStr
ls (Text
"fetching packument from origin for " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> PackageName -> Text
renderPackageName PackageName
nm))))
                OriginFor posture
origin
  where
    baseUrl :: Text
baseUrl = OriginClient -> Text
originBaseUrl (OriginFor posture -> OriginClient
forall posture. OriginFor posture -> OriginClient
originClientOf OriginFor posture
origin)

fullReadOrigin :: (Int -> IO ()) -> PackumentDeps -> ChargePart -> OriginFor posture -> IO (OriginFor posture)
fullReadOrigin :: forall posture.
(Int -> IO ())
-> PackumentDeps
-> ChargePart
-> OriginFor posture
-> IO (OriginFor posture)
fullReadOrigin Int -> IO ()
observe PackumentDeps
deps ChargePart
part OriginFor posture
origin
    | PrepareRead -> Bool
canChargeFullReadWorkspace (AdapterMetadata -> PrepareRead
metadataRead (PackumentDeps -> AdapterMetadata
pdMetadata PackumentDeps
deps)) = do
        workspace <- (Int -> IO ()) -> IO (Int -> IO ())
newChargeRegion (ChargePart -> Int -> IO ()
chargePart ChargePart
part)
        pure (chargingFullReadWorkspace workspace charged)
    | Bool
otherwise = OriginFor posture -> IO (OriginFor posture)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure OriginFor posture
charged
  where
    charged :: OriginFor posture
charged = (Int -> IO ()) -> OriginFor posture -> OriginFor posture
forall posture.
(Int -> IO ()) -> OriginFor posture -> OriginFor posture
chargingFullReads (\Int
bytes -> Int -> IO ()
observe Int
bytes IO () -> IO () -> IO ()
forall a b. IO a -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> PackumentDeps -> ChargePart -> Int -> IO ()
fullReadCharge PackumentDeps
deps ChargePart
part Int
bytes) OriginFor posture
origin

fullReadCharge :: PackumentDeps -> ChargePart -> Int -> IO ()
fullReadCharge :: PackumentDeps -> ChargePart -> Int -> IO ()
fullReadCharge PackumentDeps
deps ChargePart
part = ChargePart -> Int -> IO ()
chargePart ChargePart
part (Int -> IO ()) -> (Int -> Int) -> Int -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Int -> Int
scaleCharge (ChargeFactors -> Int
cfFullReadPermille (PackumentDeps -> ChargeFactors
chargeFactors PackumentDeps
deps))

chargeFactors :: PackumentDeps -> ChargeFactors
chargeFactors :: PackumentDeps -> ChargeFactors
chargeFactors = AdapterMetadata -> ChargeFactors
metadataChargeFactors (AdapterMetadata -> ChargeFactors)
-> (PackumentDeps -> AdapterMetadata)
-> PackumentDeps
-> ChargeFactors
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PackumentDeps -> AdapterMetadata
pdMetadata

{- | Bypass shared caching so the private upstream authorises each caller's credential.
Static lookups can omit the ticket. Native workspace admission requires one.
-}
withPrivateMetadataClient :: ServeRuntime -> PackumentDeps -> Maybe MemoryTicket -> BaseRequest -> Maybe ClientCredential -> (MetadataClient -> IO a) -> Handler a
withPrivateMetadataClient :: forall a.
ServeRuntime
-> PackumentDeps
-> Maybe MemoryTicket
-> BaseRequest
-> Maybe ClientCredential
-> (MetadataClient -> IO a)
-> Handler a
withPrivateMetadataClient ServeRuntime
rt PackumentDeps
deps Maybe MemoryTicket
ticket BaseRequest
base Maybe ClientCredential
token MetadataClient -> IO a
action = do
    let selected :: OriginFor Private
selected = ServeRuntime
-> PackumentDeps
-> BaseRequest
-> Maybe ClientCredential
-> OriginFor Private
privateOrigin ServeRuntime
rt PackumentDeps
deps BaseRequest
base Maybe ClientCredential
token
    origin <- IO (OriginFor Private) -> Handler (OriginFor Private)
forall a. IO a -> Handler a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (OriginFor Private)
-> (MemoryTicket -> IO (OriginFor Private))
-> Maybe MemoryTicket
-> IO (OriginFor Private)
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (OriginFor Private -> IO (OriginFor Private)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure OriginFor Private
selected) (\MemoryTicket
held -> (Int -> IO ()) -> IO (Int -> IO ())
newChargeRegion (MemoryTicket -> Int -> IO ()
charge MemoryTicket
held) IO (Int -> IO ())
-> ((Int -> IO ()) -> IO (OriginFor Private))
-> IO (OriginFor Private)
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Int -> IO ()
region -> ChargeFactors
-> (Int -> IO ()) -> OriginFor Private -> IO (OriginFor Private)
forall posture.
ChargeFactors
-> (Int -> IO ()) -> OriginFor posture -> IO (OriginFor posture)
chargingNativeReads (PackumentDeps -> ChargeFactors
chargeFactors PackumentDeps
deps) Int -> IO ()
region OriginFor Private
selected) Maybe MemoryTicket
ticket)
    withMetadataClient rt deps privateMetadataClient origin action

-- | Reserve each consumer's selected facts. Serving consumers never retain the cached source row.
withPublicMetadata :: ServeRuntime -> PackumentDeps -> MemoryTicket -> PackageName -> Version -> (IO (Either MetadataError VersionRead) -> Handler a) -> Handler a
withPublicMetadata :: forall a.
ServeRuntime
-> PackumentDeps
-> MemoryTicket
-> PackageName
-> Version
-> (IO (Either MetadataError VersionRead) -> Handler a)
-> Handler a
withPublicMetadata ServeRuntime
rt PackumentDeps
deps MemoryTicket
ticket PackageName
name Version
version IO (Either MetadataError VersionRead) -> Handler a
action = do
    let flight :: FlightKey
flight = CacheKey -> FlightKey
flightOf (Source -> PackageName -> Version -> CacheKey
versionKey (PackumentDeps -> Source
publicSource PackumentDeps
deps) PackageName
name Version
version)
    workspace <- IO (Int -> IO ()) -> Handler (Int -> IO ())
forall a. IO a -> Handler a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO ((Int -> IO ()) -> IO (Int -> IO ())
newChargeRegion (MemoryTicket -> Int -> IO ()
charge (FlightKey -> MemoryTicket -> MemoryTicket
servingFlight FlightKey
flight MemoryTicket
ticket)))
    origin <- liftIO (chargingNativeReads (chargeFactors deps) workspace (publicOrigin rt deps))
    retained <- liftIO (newChargeRegion (charge ticket))
    readSelected <- withMetadataClient rt deps (preparePublicVersion (srMetadataCache rt) (publicSource deps)) origin $ \PackageName
-> Version -> IO (PreparedStore MetadataError VersionRead)
prepare ->
        IO (Either MetadataError VersionRead)
-> IO (IO (Either MetadataError VersionRead))
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (MemoryTicket
-> FlightKey
-> IO (Either MetadataError VersionRead)
-> IO (Either MetadataError VersionRead)
forall (m :: * -> *) a.
MonadUnliftIO m =>
MemoryTicket -> FlightKey -> m a -> m a
awaitingFlight MemoryTicket
ticket FlightKey
flight (PackageName
-> Version -> IO (PreparedStore MetadataError VersionRead)
prepare PackageName
name Version
version IO (PreparedStore MetadataError VersionRead)
-> (PreparedStore MetadataError VersionRead
    -> IO (Either MetadataError VersionRead))
-> IO (Either MetadataError VersionRead)
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= PreparedStore MetadataError VersionRead
-> IO (Either MetadataError VersionRead)
forall e v. PreparedStore e v -> IO (Either e v)
executePrepared))
    action $
        readSelected
            >>= traverse
                ( \VersionRead
result -> do
                    let !served :: VersionRead
served = VersionRead -> VersionRead
servingVersion VersionRead
result
                    Int -> IO ()
retained (VersionRead -> Int
weighVersion VersionRead
served)
                    VersionRead -> IO VersionRead
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure VersionRead
served
                )

servingVersion :: VersionRead -> VersionRead
servingVersion :: VersionRead -> VersionRead
servingVersion VersionRead
result = case VersionRead -> Maybe VersionDoc
vrVersion VersionRead
result of
    Maybe VersionDoc
Nothing -> VersionRead
result
    Just VersionDoc
document ->
        let !details :: PackageDetails
details = VersionDoc -> PackageDetails
vdDetails VersionDoc
document
            !selected :: VersionDoc
selected = PackageDetails -> Maybe CachedDoc -> VersionDoc
VersionDoc PackageDetails
details Maybe CachedDoc
forall a. Maybe a
Nothing
         in Maybe VersionDoc -> Int -> Maybe Version -> VersionRead
VersionRead (VersionDoc -> Maybe VersionDoc
forall a. a -> Maybe a
Just VersionDoc
selected) (VersionRead -> Int
vrBodyBytes VersionRead
result) (VersionRead -> Maybe Version
vrUpstreamLatest VersionRead
result)

privateOrigin :: ServeRuntime -> PackumentDeps -> BaseRequest -> Maybe ClientCredential -> OriginFor Private
privateOrigin :: ServeRuntime
-> PackumentDeps
-> BaseRequest
-> Maybe ClientCredential
-> OriginFor Private
privateOrigin ServeRuntime
rt PackumentDeps
deps = Limits
-> ExchangeClient
-> BaseRequest
-> Maybe ClientCredential
-> OriginFor Private
perCallerOrigin (PackumentDeps -> Limits
pdLimits PackumentDeps
deps) (ServeRuntime -> ExchangeClient
srPrivateClient ServeRuntime
rt)

publicOrigin :: ServeRuntime -> PackumentDeps -> OriginFor Public
publicOrigin :: ServeRuntime -> PackumentDeps -> OriginFor Public
publicOrigin ServeRuntime
rt PackumentDeps
deps = PublicControl
-> Maybe PublicReadKind -> OriginFor Public -> OriginFor Public
controlPublicOrigin (ServeRuntime -> PublicControl
srPublicControl ServeRuntime
rt) Maybe PublicReadKind
forall a. Maybe a
Nothing (Limits -> ExchangeClient -> BaseRequest -> OriginFor Public
anonymousOrigin (PackumentDeps -> Limits
pdLimits PackumentDeps
deps) (ServeRuntime -> ExchangeClient
srPublicClient ServeRuntime
rt) (PackumentDeps -> BaseRequest
pdPublicBase PackumentDeps
deps))

-- | The public origin's cache source, keyed by its base URL.
publicSource :: PackumentDeps -> Source
publicSource :: PackumentDeps -> Source
publicSource PackumentDeps
deps = Text -> Source
Source (RegistryUrl -> Text
registryUrlText (PackumentDeps -> RegistryUrl
pdPublicBaseUrl PackumentDeps
deps))

-- | Build an origin with the mount's response bound and the caller-selected transport and credential.
mountOrigin :: PackumentDeps -> ExchangeClient -> BaseRequest -> Maybe ClientCredential -> OriginClient
mountOrigin :: PackumentDeps
-> ExchangeClient
-> BaseRequest
-> Maybe ClientCredential
-> OriginClient
mountOrigin PackumentDeps
deps ExchangeClient
client BaseRequest
base = OriginFor Private -> OriginClient
forall posture. OriginFor posture -> OriginClient
originClientOf (OriginFor Private -> OriginClient)
-> (Maybe ClientCredential -> OriginFor Private)
-> Maybe ClientCredential
-> OriginClient
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Limits
-> ExchangeClient
-> BaseRequest
-> Maybe ClientCredential
-> OriginFor Private
perCallerOrigin (PackumentDeps -> Limits
pdLimits PackumentDeps
deps) ExchangeClient
client BaseRequest
base