module Ecluse.Composition.Sizing (
resolveSized,
renderSized,
newPooledManager,
connectionPoolSettings,
resolveServeAdmission,
resolvePrivateConnections,
resolvePublicConnections,
openFileSoftLimit,
mirrorEnqueueBufferDepth,
mirrorEnqueueReportInterval,
) where
import Data.Ord (clamp)
import Network.HTTP.Client (Manager, ManagerSettings (managerConnCount), newManager)
import System.Posix.Resource (Resource (ResourceOpenFiles), ResourceLimit (ResourceLimit, ResourceLimitInfinity, ResourceLimitUnknown), ResourceLimits (softLimit), getResourceLimit)
resolveSized :: (Show a) => Text -> Maybe a -> a -> Text -> (a, Text)
resolveSized :: forall a. Show a => Text -> Maybe a -> a -> Text -> (a, Text)
resolveSized Text
subject Maybe a
explicit a
computed Text
computedClause =
(a
value, Text -> a -> Maybe a -> Text -> Text
forall a. Show a => Text -> a -> Maybe a -> Text -> Text
renderSized Text
subject a
value Maybe a
explicit Text
computedClause)
where
value :: a
value = a -> Maybe a -> a
forall a. a -> Maybe a -> a
fromMaybe a
computed Maybe a
explicit
renderSized :: (Show a) => Text -> a -> Maybe a -> Text -> Text
renderSized :: forall a. Show a => Text -> a -> Maybe a -> Text -> Text
renderSized Text
subject a
value Maybe a
explicit Text
computedClause =
Text
subject Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> a -> Text
forall b a. (Show a, IsString b) => a -> b
show a
value Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" (" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
provenance Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
")"
where
provenance :: Text
provenance = if Maybe a -> Bool
forall a. Maybe a -> Bool
isJust Maybe a
explicit then Text
"from config" else Text
computedClause
newPooledManager :: Int -> ManagerSettings -> IO Manager
newPooledManager :: Int -> ManagerSettings -> IO Manager
newPooledManager Int
connections = ManagerSettings -> IO Manager
newManager (ManagerSettings -> IO Manager)
-> (ManagerSettings -> ManagerSettings)
-> ManagerSettings
-> IO Manager
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> ManagerSettings -> ManagerSettings
connectionPoolSettings Int
connections
connectionPoolSettings :: Int -> ManagerSettings -> ManagerSettings
connectionPoolSettings :: Int -> ManagerSettings -> ManagerSettings
connectionPoolSettings Int
connections ManagerSettings
settings = ManagerSettings
settings{managerConnCount = connections}
resolveServeAdmission :: Maybe Int -> Int -> (Int, Text)
resolveServeAdmission :: Maybe Int -> Int -> (Int, Text)
resolveServeAdmission Maybe Int
explicit Int
capabilities =
Text -> Maybe Int -> Int -> Text -> (Int, Text)
forall a. Show a => Text -> Maybe a -> a -> Text -> (a, Text)
resolveSized
Text
"runtime: serve admission"
Maybe Int
explicit
(Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
serveAdmissionFloor (Int
serveAdmissionPerCapability Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
capabilities))
(Text
"computed from " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Int -> Text
forall b a. (Show a, IsString b) => a -> b
show Int
capabilities Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" capabilities")
serveAdmissionPerCapability :: Int
serveAdmissionPerCapability :: Int
serveAdmissionPerCapability = Int
10
serveAdmissionFloor :: Int
serveAdmissionFloor :: Int
serveAdmissionFloor = Int
8
resolvePrivateConnections :: Maybe Int -> Int -> (Int, Text)
resolvePrivateConnections :: Maybe Int -> Int -> (Int, Text)
resolvePrivateConnections Maybe Int
explicit Int
fdLimit =
Text -> Maybe Int -> Int -> Text -> (Int, Text)
forall a. Show a => Text -> Maybe a -> a -> Text -> (a, Text)
resolveSized
Text
"runtime: private connection pool"
Maybe Int
explicit
(Int -> Int
clampPrivateConnections (Int
fdLimit Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
privateConnectionsFdShare))
(Int -> Text
fdLimitClause Int
fdLimit)
clampPrivateConnections :: Int -> Int
clampPrivateConnections :: Int -> Int
clampPrivateConnections = (Int, Int) -> Int -> Int
forall a. Ord a => (a, a) -> a -> a
clamp (Int
privateConnectionsFloor, Int
privateConnectionsCap)
privateConnectionsFdShare :: Int
privateConnectionsFdShare :: Int
privateConnectionsFdShare = Int
4
privateConnectionsFloor :: Int
privateConnectionsFloor :: Int
privateConnectionsFloor = Int
64
privateConnectionsCap :: Int
privateConnectionsCap :: Int
privateConnectionsCap = Int
4096
resolvePublicConnections :: Maybe Int -> Int -> (Int, Text)
resolvePublicConnections :: Maybe Int -> Int -> (Int, Text)
resolvePublicConnections Maybe Int
explicit Int
fdLimit =
Text -> Maybe Int -> Int -> Text -> (Int, Text)
forall a. Show a => Text -> Maybe a -> a -> Text -> (a, Text)
resolveSized
Text
"runtime: public connection pool"
Maybe Int
explicit
(Int -> Int
clampPublicConnections (Int
fdLimit Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
publicConnectionsFdShare))
(Int -> Text
fdLimitClause Int
fdLimit)
fdLimitClause :: Int -> Text
fdLimitClause :: Int -> Text
fdLimitClause Int
fdLimit = Text
"computed from file-descriptor limit " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Int -> Text
forall b a. (Show a, IsString b) => a -> b
show Int
fdLimit
clampPublicConnections :: Int -> Int
clampPublicConnections :: Int -> Int
clampPublicConnections = (Int, Int) -> Int -> Int
forall a. Ord a => (a, a) -> a -> a
clamp (Int
publicConnectionsFloor, Int
publicConnectionsCap)
publicConnectionsFdShare :: Int
publicConnectionsFdShare :: Int
publicConnectionsFdShare = Int
8
publicConnectionsFloor :: Int
publicConnectionsFloor :: Int
publicConnectionsFloor = Int
32
publicConnectionsCap :: Int
publicConnectionsCap :: Int
publicConnectionsCap = Int
1024
mirrorEnqueueBufferDepth :: Int
mirrorEnqueueBufferDepth :: Int
mirrorEnqueueBufferDepth = Int
1024
mirrorEnqueueReportInterval :: Int
mirrorEnqueueReportInterval :: Int
mirrorEnqueueReportInterval = Int
100
openFileSoftLimit :: IO Int
openFileSoftLimit :: IO Int
openFileSoftLimit = do
limits <- Resource -> IO ResourceLimits
getResourceLimit Resource
ResourceOpenFiles
pure $ case softLimit limits of
ResourceLimit Integer
n -> Integer -> Int
forall a. Num a => Integer -> a
fromInteger Integer
n
ResourceLimit
ResourceLimitInfinity -> Int
privateConnectionsCap Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
privateConnectionsFdShare
ResourceLimit
ResourceLimitUnknown -> Int
privateConnectionsCap Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
privateConnectionsFdShare