token-limiter-0.1.0.0: Fast rate limiting using the token bucket algorithm (BSD)

Safe HaskellNone
LanguageHaskell2010

Control.Concurrent.TokenLimiter

Description

Fast rate-limiting via token bucket algorithm. Uses lock-free compare-and-swap operations on the fast path when debiting tokens.

Synopsis

Documentation

type Count = Int Source #

data LimitConfig Source #

Constructors

LimitConfig 

Fields

Instances
Generic LimitConfig Source # 
Instance details

Defined in Control.Concurrent.TokenLimiter

Associated Types

type Rep LimitConfig :: * -> * #

type Rep LimitConfig Source # 
Instance details

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 ()))))))

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).