token-limiter-concurrent-0.0.0.0: A thread-safe concurrent token-bucket rate limiter that guarantees fairness
Safe HaskellSafe-Inferred
LanguageHaskell2010

Control.Concurrent.TokenLimiter.Concurrent

Synopsis

Documentation

type Count = Word64 Source #

An amount of tokens

data TokenLimitConfig Source #

A configuration for TokenLimiter

Constructors

TokenLimitConfig 

Fields

Instances

Instances details
Eq TokenLimitConfig Source # 
Instance details

Defined in Control.Concurrent.TokenLimiter.Concurrent

Show TokenLimitConfig Source # 
Instance details

Defined in Control.Concurrent.TokenLimiter.Concurrent

Generic TokenLimitConfig Source # 
Instance details

Defined in Control.Concurrent.TokenLimiter.Concurrent

Associated Types

type Rep TokenLimitConfig :: Type -> Type #

type Rep TokenLimitConfig Source # 
Instance details

Defined in Control.Concurrent.TokenLimiter.Concurrent

type Rep TokenLimitConfig = D1 ('MetaData "TokenLimitConfig" "Control.Concurrent.TokenLimiter.Concurrent" "token-limiter-concurrent-0.0.0.0-4L9nyvrvvfa3qKmi95ra6r" 'False) (C1 ('MetaCons "TokenLimitConfig" 'PrefixI 'True) (S1 ('MetaSel ('Just "tokenLimitConfigInitialTokens") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Count) :*: (S1 ('MetaSel ('Just "tokenLimitConfigMaxTokens") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Count) :*: S1 ('MetaSel ('Just "tokenLimitConfigTokensPerSecond") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Count))))

type MonotonicTime = Word64 Source #

A type synonym for a number of "monotonic time" nanoseconds.

This only exists because it is also a Word64 and would be too easy to confuse with a Count.

data TokenLimiter Source #

A token bucket-based rate limiter

This token limiter is thread-safe and guarantees that:

Constructors

TokenLimiter 

Fields

Instances

Instances details
Eq TokenLimiter Source # 
Instance details

Defined in Control.Concurrent.TokenLimiter.Concurrent

Generic TokenLimiter Source # 
Instance details

Defined in Control.Concurrent.TokenLimiter.Concurrent

Associated Types

type Rep TokenLimiter :: Type -> Type #

type Rep TokenLimiter Source # 
Instance details

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.