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
- tryDebit :: TokenLimiter -> Word64 -> IO Bool
- waitDebit :: TokenLimiter -> Word64 -> IO (Maybe MonotonicDiffNanos)
- newtype MonotonicDiffNanos = MonotonicDiffNanos {}
- computeMicrosecondsToWait :: Count -> Word64 -> Maybe Int
- computeCurrentCount :: TokenLimitConfig -> MonotonicTime -> Count -> MonotonicTime -> Count
Create
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
Generic TokenLimiter Source # | |||||
Defined in Control.Concurrent.TokenLimiter.Concurrent Associated Types
| |||||
Eq TokenLimiter Source # | |||||
Defined in Control.Concurrent.TokenLimiter.Concurrent | |||||
type Rep TokenLimiter Source # | |||||
Defined in Control.Concurrent.TokenLimiter.Concurrent type Rep TokenLimiter = D1 ('MetaData "TokenLimiter" "Control.Concurrent.TokenLimiter.Concurrent" "token-limiter-concurrent-0.2.0.1-GRln7kwjj9WE7z0N0MaSrW" '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 #
Constructors
MonotonicDiffNanos | |
Fields |
Instances
Generic MonotonicDiffNanos Source # | |||||
Defined in Control.Concurrent.TokenLimiter.Concurrent Associated Types
Methods from :: MonotonicDiffNanos -> Rep MonotonicDiffNanos x # to :: Rep MonotonicDiffNanos x -> MonotonicDiffNanos # | |||||
Show MonotonicDiffNanos Source # | |||||
Defined in Control.Concurrent.TokenLimiter.Concurrent Methods showsPrec :: Int -> MonotonicDiffNanos -> ShowS # show :: MonotonicDiffNanos -> String # showList :: [MonotonicDiffNanos] -> ShowS # | |||||
Eq MonotonicDiffNanos Source # | |||||
Defined in Control.Concurrent.TokenLimiter.Concurrent Methods (==) :: MonotonicDiffNanos -> MonotonicDiffNanos -> Bool # (/=) :: MonotonicDiffNanos -> MonotonicDiffNanos -> Bool # | |||||
Ord MonotonicDiffNanos Source # | |||||
Defined in Control.Concurrent.TokenLimiter.Concurrent Methods compare :: MonotonicDiffNanos -> MonotonicDiffNanos -> Ordering # (<) :: MonotonicDiffNanos -> MonotonicDiffNanos -> Bool # (<=) :: MonotonicDiffNanos -> MonotonicDiffNanos -> Bool # (>) :: MonotonicDiffNanos -> MonotonicDiffNanos -> Bool # (>=) :: MonotonicDiffNanos -> MonotonicDiffNanos -> Bool # max :: MonotonicDiffNanos -> MonotonicDiffNanos -> MonotonicDiffNanos # min :: MonotonicDiffNanos -> MonotonicDiffNanos -> MonotonicDiffNanos # | |||||
type Rep MonotonicDiffNanos Source # | |||||
Defined in Control.Concurrent.TokenLimiter.Concurrent type Rep MonotonicDiffNanos = D1 ('MetaData "MonotonicDiffNanos" "Control.Concurrent.TokenLimiter.Concurrent" "token-limiter-concurrent-0.2.0.1-GRln7kwjj9WE7z0N0MaSrW" 'True) (C1 ('MetaCons "MonotonicDiffNanos" 'PrefixI 'True) (S1 ('MetaSel ('Just "unMonotonicDiffNanos") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Word64))) |
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.