arbiter-core-0.1.0.0
arbiter-core
Safe HaskellNone
LanguageGHC2024

Arbiter.Core.RateLimit.Spec

Description

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

Source #data Durability

Whether the rate-limit bucket table is WAL-logged. Set at migration time.

Constructors

Durable 
Unlogged 

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

Source #data RateLimitKey

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

Constructors

RateLimitKey 

Fields

Source #rateLimitKeyText :: RateLimitKey -> Text

The bucket key as stored, prefix and suffix joined.

Source #data Policy

A token-bucket policy. Burst policyMax, refilling policyRefill every policyInterval. A policyRefill of 0 is a manually-refilled bucket.

Instances

Instances details
AdmissionPolicy Policy Source # 
Instance details

Defined in Arbiter.Core.RateLimit.Spec

Eq Policy Source # 
Instance details

Defined in Arbiter.Core.RateLimit.Spec

Methods

#(==) :: Policy -> Policy -> Bool

#(/=) :: Policy -> Policy -> Bool

Ord Policy Source # 
Instance details

Defined in Arbiter.Core.RateLimit.Spec

Show Policy Source # 
Instance details

Defined in Arbiter.Core.RateLimit.Spec

HasRateLimit payload => CollectFor (payload :: Type) Policy Source # 
Instance details

Defined in Arbiter.Core.RateLimit.Spec

Source #tokenBucket :: Text -> Double -> NominalDiffTime -> Policy

"N per period" with burst N (max = refill = n). 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

Source #class HasRateLimit payload where

A payload's per-job key selection. Defaults to unlimited. Only limited payloads need an instance.

Minimal complete definition

Nothing

Methods

Source #rateLimitFor :: RateLimitFor payload

The selector deciding which policy (if any) limits a given job.

Source #rateLimitCost :: payload -> Double

How many tokens this job spends. Defaults to 1.

Instances

Instances details
HasRateLimit payload Source # 
Instance details

Defined in Arbiter.Core.RateLimit.Spec

Source #type RateLimitFor payload = Selector Policy payload (Maybe RateLimitKey)

A selective description of the rate-limit key for a payload. Evaluation returns the job key. Static inspection returns the reachable policies.

Source #noLimit :: RateLimitFor payload

This payload is unlimited.

Source #limitBy :: Policy -> (payload -> Text) -> RateLimitFor payload

Limit by a fixed policy, keyed by a per-job suffix (e.g. a tenant id).

Source #globalLimit :: Policy -> Text -> RateLimitFor payload

Limit by a fixed policy under one shared key (a single global bucket).

Source #chooseWhen :: (payload -> Bool) -> Selector policy payload a -> Selector policy payload a -> Selector policy payload a

Select between two selectors with a job predicate. Policy collection inspects both branches.

Source #limitByCase :: (Bounded k, Enum k, Eq k) => (payload -> k) -> (k -> RateLimitFor payload) -> RateLimitFor payload

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.

Source #runRateLimitFor :: payload -> RateLimitFor payload -> Maybe RateLimitKey

Run a selector against a concrete job to get its key.

Source #collectPolicies :: Ord policy => Selector policy payload a -> Set policy

All policies that a selector can reach across all branches.

Registry reflection

Source #type RegistryRateLimitPolicies (registry :: JobPayloadRegistry) = RegistryPolicies registry Policy

Collect every policy declared across a registry's payloads, by statically inspecting each payload's rateLimitFor. The migration seeds these.

Source #registryRateLimitPolicies :: forall (registry :: JobPayloadRegistry). RegistryRateLimitPolicies registry => Set Policy

Every distinct policy declared across the registry's payloads.

Source #registryRateLimitTables :: forall (registry :: JobPayloadRegistry). RegistryRateLimitPolicies registry => [(Text, Bool)]

Each registry table paired with whether its payload declares any policy.