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
- waitDebit :: LimitConfig -> RateLimiter -> Count -> IO ()
- defaultLimitConfig :: LimitConfig
Documentation
data LimitConfig Source #
LimitConfig | |
|
Instances
Generic LimitConfig Source # | |
Defined in Control.Concurrent.TokenLimiter type Rep LimitConfig :: * -> * # 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.1.0.0-AhVOZ6w5bKCLtP51fEVAmu" 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.
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 definitely get "thundering herd" on wakeup if a number of threads are contending for fresh tokens).