Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
Synopsis
- type Count = Word64
- data TokenLimitConfig = TokenLimitConfig {}
- type MonotonicTime = Word64
- data TokenLimiter = TokenLimiter {}
- makeTokenLimiter :: TokenLimitConfig -> IO TokenLimiter
- tryDebit :: TokenLimiter -> Word64 -> IO Bool
- waitDebit :: TokenLimiter -> Word64 -> IO (Maybe MonotonicDiffNanos)
- newtype MonotonicDiffNanos = MonotonicDiffNanos {}
- computeCurrentCount :: TokenLimitConfig -> MonotonicTime -> Count -> MonotonicTime -> Count
Create
data TokenLimitConfig Source #
A configuration for TokenLimiter
TokenLimitConfig | |
|
Instances
type MonotonicTime = Word64 Source #
data TokenLimiter Source #
A token bucket-based rate limiter
This token limiter is thread-safe and guarantees that:
TokenLimiter | |
|
Instances
Generic TokenLimiter Source # | |
Defined in Control.Concurrent.TokenLimiter.Concurrent type Rep TokenLimiter :: Type -> Type # from :: TokenLimiter -> Rep TokenLimiter x # to :: Rep TokenLimiter x -> TokenLimiter # | |
Eq TokenLimiter Source # | |
Defined in Control.Concurrent.TokenLimiter.Concurrent (==) :: TokenLimiter -> TokenLimiter -> Bool # (/=) :: TokenLimiter -> TokenLimiter -> Bool # | |
type Rep TokenLimiter Source # | |
Defined in Control.Concurrent.TokenLimiter.Concurrent type Rep TokenLimiter = D1 ('MetaData "TokenLimiter" "Control.Concurrent.TokenLimiter.Concurrent" "token-limiter-concurrent-0.1.0.0-DcHrU4XMkb37dFhrj1OAuv" '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
,
Use
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.
Note that there is a small race-condition in which tryDebit
sometimes
returns False
eventhough it could (maybe) have debited because another
thread was currently waitDebit
-ing without actually waiting (because it
didn't need to wait).
waitDebit :: TokenLimiter -> Word64 -> IO (Maybe MonotonicDiffNanos) Source #
Wait until the given number of tokens can be debited.
Returns the time waited, for stats recording purposes.
Note: only reports the time waited due to rate-limiting this specific action, not the wall-clock time waited (that might include waiting for previous actions limited by this rate limiter to finish).
Note: debitor threads are serviced in FIFO order, so a request for a small (and currently satisfiable) number of tokens can still be delayed by a debit request for a larger amount of tokens.
Note: the wait time reported can be inflated due to scheduling inaccuracy. See https://gitlab.haskell.org/ghc/ghc/-/issues/16601.
newtype MonotonicDiffNanos Source #
Instances
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.