| Safe Haskell | None |
|---|---|
| Language | GHC2024 |
Arbiter.Core.RateLimit.Spec
Description
Internal to the arbiter packages. Not covered by the PVP.
Per-job token-bucket rate limits. A payload's rateLimitFor describes how
to select its policy and key. Static inspection finds all policies that the
migration must initialize.
Synopsis
- data Durability
- data RateLimitKey = RateLimitKey {}
- toRateLimitKey :: Text -> Text -> RateLimitKey
- rateLimitKeyText :: RateLimitKey -> Text
- data RateLimitPolicy = RateLimitPolicy {}
- tokenBucket :: Text -> Double -> NominalDiffTime -> RateLimitPolicy
- class HasRateLimit payload where
- rateLimitFor :: RateLimitFor payload
- rateLimitCost :: payload -> Double
- type RateLimitFor payload = Selector RateLimitPolicy payload (Maybe RateLimitKey)
- noLimit :: RateLimitFor payload
- limitBy :: RateLimitPolicy -> (payload -> Text) -> RateLimitFor payload
- globalLimit :: RateLimitPolicy -> Text -> RateLimitFor payload
- chooseWhen :: (payload -> Bool) -> Selector policy payload a -> Selector policy payload a -> Selector policy payload a
- limitByCase :: (Bounded k, Enum k, Eq k) => (payload -> k) -> (k -> RateLimitFor payload) -> RateLimitFor payload
- collectPolicies :: Ord policy => Selector policy payload a -> Set policy
- type RegistryRateLimitPolicies (registry :: JobPayloadRegistry) = RegistryPolicies registry RateLimitPolicy
- registryRateLimitPolicies :: forall (registry :: JobPayloadRegistry). RegistryRateLimitPolicies registry => Set RateLimitPolicy
- registryRateLimitTables :: forall (registry :: JobPayloadRegistry). RegistryRateLimitPolicies registry => [(Text, Bool)]
Core types
data DurabilitySource #
Whether the rate-limit bucket table is WAL-logged. Set by the migration config.
Constructors
| Durable | WAL-logged. Buckets survive a crash. |
| Unlogged | Not WAL-logged. A crash resets the buckets. The migrations use this by default. |
Instances
| Eq Durability Source # | |
Defined in Arbiter.Core.RateLimit.Spec | |
| Show Durability Source # | |
Defined in Arbiter.Core.RateLimit.Spec Methods showsPrec :: Int -> Durability -> ShowS# show :: Durability -> String# showList :: [Durability] -> ShowS# | |
data RateLimitKeySource #
A resolved key with a prefix and per-key suffix. The stored form is
prefix:suffix.
Constructors
| RateLimitKey | |
Instances
| FromJSON RateLimitKey Source # | |
Defined in Arbiter.Core.RateLimit.Spec | |
| ToJSON RateLimitKey Source # | |
Defined in Arbiter.Core.RateLimit.Spec Methods toJSON :: RateLimitKey -> Value# toEncoding :: RateLimitKey -> Encoding# toJSONList :: [RateLimitKey] -> Value# toEncodingList :: [RateLimitKey] -> Encoding# omitField :: RateLimitKey -> Bool# | |
| Eq RateLimitKey Source # | |
Defined in Arbiter.Core.RateLimit.Spec | |
| Show RateLimitKey Source # | |
Defined in Arbiter.Core.RateLimit.Spec Methods showsPrec :: Int -> RateLimitKey -> ShowS# show :: RateLimitKey -> String# showList :: [RateLimitKey] -> ShowS# | |
toRateLimitKey :: Text -> Text -> RateLimitKeySource #
A rate-limit key from its prefix and suffix.
rateLimitKeyText :: RateLimitKey -> TextSource #
The stored key text, prefix:suffix.
data RateLimitPolicySource #
A token-bucket policy. Burst policyMax, refilling continuously at policyRefill
per policyInterval. A policyRefill of 0 is a manually-refilled bucket. Fields are
non-negative and the interval is positive. The migration rejects other values.
Constructors
| RateLimitPolicy | |
Fields
| |
Instances
tokenBucket :: Text -> Double -> NominalDiffTime -> RateLimitPolicySource #
count per period, with a burst of count. The period is floored to a
tiny positive value. The prefix must not contain :, the key separator. The
migration enforces this.
Selecting a policy per job
class HasRateLimit payload whereSource #
A payload's per-job policy selection. Defaults to unlimited. Only limited payloads need an instance.
Minimal complete definition
Nothing
Methods
rateLimitFor :: RateLimitFor payloadSource #
The selector deciding which policy (if any) limits a given job.
rateLimitCost :: payload -> DoubleSource #
How many tokens this job spends, clamped to the bucket's range. Defaults to 1.
Instances
| HasRateLimit payload Source # | |
Defined in Arbiter.Core.RateLimit.Spec | |
type RateLimitFor payload = Selector RateLimitPolicy payload (Maybe RateLimitKey)Source #
A selective description of the rate-limit key for a payload. Evaluation returns the job key. Static inspection returns the reachable policies.
noLimit :: RateLimitFor payloadSource #
This payload is unlimited.
limitBy :: RateLimitPolicy -> (payload -> Text) -> RateLimitFor payloadSource #
Limit by a fixed policy, keyed by a per-job suffix, such as a tenant id.
globalLimit :: RateLimitPolicy -> Text -> RateLimitFor payloadSource #
Limit by a fixed policy under one shared key (a single global bucket).
chooseWhen :: (payload -> Bool) -> Selector policy payload a -> Selector policy payload a -> Selector policy payload aSource #
The first selector when the predicate holds, else the second. Policy collection inspects both branches.
limitByCase :: (Bounded k, Enum k, Eq k) => (payload -> k) -> (k -> RateLimitFor payload) -> RateLimitFor payloadSource #
N-way chooseWhen. Maps the job to a finite tag, then each tag to its selector.
Policy collection evaluates every tag in [minBound..maxBound]. The tag's
Bounded/Enum and the selector must be total over k.
collectPolicies :: Ord policy => Selector policy payload a -> Set policySource #
All policies that a selector can reach across all branches.
Registry reflection
type RegistryRateLimitPolicies (registry :: JobPayloadRegistry) = RegistryPolicies registry RateLimitPolicySource #
The constraint that lets the migration collect a registry's declared policies.
registryRateLimitPolicies :: forall (registry :: JobPayloadRegistry). RegistryRateLimitPolicies registry => Set RateLimitPolicySource #
Every distinct policy declared across the registry's payloads.
registryRateLimitTables :: forall (registry :: JobPayloadRegistry). RegistryRateLimitPolicies registry => [(Text, Bool)]Source #
Each registry table paired with whether its payload declares any policy.