-- Hoogle documentation, generated by Haddock
-- See Hoogle, http://www.haskell.org/hoogle/
-- | Fast rate limiting using the token bucket algorithm (BSD)
--
-- Fast rate limiting using the token bucket algorithm. BSD-licensed.
@package token-limiter
@version 0.2.0.2
-- | Fast rate-limiting via token bucket algorithm. Uses lock-free
-- compare-and-swap operations on the fast path when debiting tokens.
module Control.Concurrent.TokenLimiter
type Count = Int
data LimitConfig
LimitConfig :: {-# UNPACK #-} !Count -> {-# UNPACK #-} !Count -> {-# UNPACK #-} !Count -> IO TimeSpec -> (TimeSpec -> IO ()) -> LimitConfig
-- | maximum number of tokens the bucket can hold at any one time.
[maxBucketTokens] :: LimitConfig -> {-# UNPACK #-} !Count
-- | how many tokens should be in the bucket when it's created.
[initialBucketTokens] :: LimitConfig -> {-# UNPACK #-} !Count
-- | how many tokens should replenish the bucket per second.
[bucketRefillTokensPerSecond] :: LimitConfig -> {-# UNPACK #-} !Count
-- | clock action, defaultLimitConfig uses the monotonic system
-- clock. Mostly provided for mocking in the testsuite.
[clockAction] :: LimitConfig -> IO TimeSpec
-- | action to delay for the given time interval. defaultLimitConfig
-- forwards to threadDelay. Provided for mocking.
[delayAction] :: LimitConfig -> TimeSpec -> IO ()
data RateLimiter
newRateLimiter :: LimitConfig -> IO RateLimiter
-- | Attempt to pull the given number of tokens from the bucket. Returns
-- True if the tokens were successfully debited.
tryDebit :: LimitConfig -> RateLimiter -> Count -> IO Bool
-- | Unconditionally debit this amount of tokens from the rate limiter,
-- driving it negative if necessary. Returns the new bucket balance.
--
-- Since: 0.2
penalize :: RateLimiter -> Count -> IO Count
-- | 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).
waitDebit :: LimitConfig -> RateLimiter -> Count -> IO ()
defaultLimitConfig :: LimitConfig
instance GHC.Generics.Generic Control.Concurrent.TokenLimiter.LimitConfig