Safe Haskell | None |
---|---|
Language | Haskell2010 |
Fast rate-limiting via token bucket algorithm. Uses lock-free compare-and-swap operations on the fast path when debiting tokens.
Synopsis
- type Count = Int
- data LimitConfig = LimitConfig {
- maxBucketTokens :: !Count
- initialBucketTokens :: !Count
- bucketRefillTokensPerSecond :: !Count
- clockAction :: IO TimeSpec
- delayAction :: TimeSpec -> IO ()
- data RateLimiter
- newRateLimiter :: LimitConfig -> IO RateLimiter
- tryDebit :: LimitConfig -> RateLimiter -> Count -> IO Bool
- penalize :: RateLimiter -> Count -> IO Count
- waitDebit :: LimitConfig -> RateLimiter -> Count -> IO ()
- defaultLimitConfig :: LimitConfig
Documentation
data LimitConfig Source #
LimitConfig | |
|
Instances
Generic LimitConfig Source # | |
Defined in Control.Concurrent.TokenLimiter type Rep LimitConfig :: Type -> Type # from :: LimitConfig -> Rep LimitConfig x # to :: Rep LimitConfig x -> LimitConfig # | |
type Rep LimitConfig Source # | |
Defined in Control.Concurrent.TokenLimiter type Rep LimitConfig = D1 (MetaData "LimitConfig" "Control.Concurrent.TokenLimiter" "token-limiter-0.2.0.1-3ZrEfwWf5VAHyIMU4eP68L" False) (C1 (MetaCons "LimitConfig" PrefixI True) ((S1 (MetaSel (Just "maxBucketTokens") SourceUnpack SourceStrict DecidedStrict) (Rec0 Count) :*: S1 (MetaSel (Just "initialBucketTokens") SourceUnpack SourceStrict DecidedStrict) (Rec0 Count)) :*: (S1 (MetaSel (Just "bucketRefillTokensPerSecond") SourceUnpack SourceStrict DecidedStrict) (Rec0 Count) :*: (S1 (MetaSel (Just "clockAction") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 (IO TimeSpec)) :*: S1 (MetaSel (Just "delayAction") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 (TimeSpec -> IO ())))))) |
data RateLimiter Source #
tryDebit :: LimitConfig -> RateLimiter -> Count -> IO Bool Source #
Attempt to pull the given number of tokens from the bucket. Returns True
if the tokens were successfully debited.
penalize :: RateLimiter -> Count -> IO Count Source #
Unconditionally debit this amount of tokens from the rate limiter, driving it negative if necessary. Returns the new bucket balance.
Since: 0.2
waitDebit :: LimitConfig -> RateLimiter -> Count -> IO () Source #
Attempt to pull k tokens from the bucket, sleeping in a loop until they become available. Will not partially fulfill token requests (i.e. it loops until the entire allotment is available in one swoop), and makes no attempt at fairness or queueing (i.e. you will probably get "thundering herd" on wakeup if a number of threads are contending for fresh tokens).