arbiter-core-0.1.0.0
arbiter-core
Safe HaskellNone
LanguageGHC2024

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

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

Instances details
Eq Durability Source # 
Instance details

Defined in Arbiter.Core.RateLimit.Spec

Show Durability Source # 
Instance details

Defined in Arbiter.Core.RateLimit.Spec

data RateLimitKeySource #

A resolved key with a prefix and per-key suffix. The stored form is prefix:suffix.

Constructors

RateLimitKey 

Fields

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

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

Instances details
HasRateLimit payload Source # 
Instance details

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.