Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
Control.Concurrent.TokenLimiter.Concurrent
Contents
Synopsis
- type Count = Word64
- data TokenLimitConfig = TokenLimitConfig {}
- type MonotonicTime = Word64
- data TokenLimiter = TokenLimiter {}
- makeTokenLimiter :: TokenLimitConfig -> IO TokenLimiter
- canDebit :: TokenLimiter -> Word64 -> IO Bool
- tryDebit :: TokenLimiter -> Word64 -> IO Bool
- waitDebit :: TokenLimiter -> Word64 -> IO ()
- computeCurrentCount :: TokenLimitConfig -> MonotonicTime -> Count -> MonotonicTime -> Count
Documentation
data TokenLimitConfig Source #
A configuration for TokenLimiter
Constructors
TokenLimitConfig | |
Fields
|
Instances
type MonotonicTime = Word64 Source #
data TokenLimiter Source #
A token bucket-based rate limiter
This token limiter is thread-safe and guarantees that:
Constructors
TokenLimiter | |
Fields
|
Instances
Eq TokenLimiter Source # | |
Defined in Control.Concurrent.TokenLimiter.Concurrent | |
Generic TokenLimiter Source # | |
Defined in Control.Concurrent.TokenLimiter.Concurrent Associated Types type Rep TokenLimiter :: Type -> Type # | |
type Rep TokenLimiter Source # | |
Defined in Control.Concurrent.TokenLimiter.Concurrent type Rep TokenLimiter = D1 ('MetaData "TokenLimiter" "Control.Concurrent.TokenLimiter.Concurrent" "token-limiter-concurrent-0.0.0.0-4L9nyvrvvfa3qKmi95ra6r" 'False) (C1 ('MetaCons "TokenLimiter" 'PrefixI 'True) (S1 ('MetaSel ('Just "tokenLimiterConfig") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 TokenLimitConfig) :*: S1 ('MetaSel ('Just "tokenLimiterLastServiced") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 (MVar (MonotonicTime, Count))))) |
makeTokenLimiter :: TokenLimitConfig -> IO TokenLimiter Source #
Make a token limiter
The initial number of tokens will be the minimum of the tokenLimitConfigInitialTokens
and the tokenLimitConfigMaxTokens
,
canDebit :: TokenLimiter -> Word64 -> IO Bool Source #
Ask if we could debit a number of tokens, without actually doing it.
Note that this information can become stale _very_ quickly.
If you want to also actually debit a number of tokens, use tryDebit
instead.
tryDebit :: TokenLimiter -> Word64 -> IO Bool Source #
Check if we can debit a number of tokens, and do it if possible.
The returned boolean represents whether the tokens were debited.
waitDebit :: TokenLimiter -> Word64 -> IO () Source #
Wait until the given number of tokens can be debited
Helper functions
computeCurrentCount :: TokenLimitConfig -> MonotonicTime -> Count -> MonotonicTime -> Count Source #
Compute the current number of tokens in a bucket purely.
You should not need this function.