Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
Functions ¶
This section is empty.
Types ¶
type Bucket ¶
type Bucket struct {
// contains filtered or unexported fields
}
func (*Bucket) AvailableTokens ¶
type BucketOpt ¶
type BucketOpt func(b *Bucket)
func WithConsumeStrategy ¶
func WithConsumeStrategy(strategy ConsumeStrategy) BucketOpt
func WithRefiller ¶
func WithStorage ¶
func WithStorage(storage TokensStorage) BucketOpt
type ConsumeExpression ¶
type ConsumeStrategy ¶
ConsumeStrategy is a function that determines how many tokens should be consumed.
func NewDynamicConsume ¶
func NewDynamicConsume() ConsumeStrategy
NewDynamicConsume returns the same number it receives as argument, if the argument is not an int64 it'll return an error.
func NewExpressionsConsume ¶
func NewExpressionsConsume(consumeExpressions []ConsumeExpression) (ConsumeStrategy, error)
NewExpressionsConsume allows you to dynamically consume tokens based on expressions, the expressions can relly on the arguments passed to the strategy, and they always must return a boolean value. Expressions are evaluated in the same order from the list, from the first to the last.
expressions := []ConsumeExpression{ {Expression: "my_argument == 1", Tokens: 50}, {Expression: `my_second_argument == "a"`, Tokens: 40}, {Expression: "true", Tokens: 100}, // This can be considered a default case since it'll always return true } consumeStrategy, err := NewExpressionsConsume(expressions) if err != nil { panic(err) }
For more information on how to use/build the expressions see https://expr.medv.io
func NewStaticConsume ¶
func NewStaticConsume(tokens int64) (ConsumeStrategy, error)
NewStaticConsume returns a static consume strategy which means the same token will always be returned. For example, NewStaticConsume(1) will always return 1 (one) when called to get the tokens to consume.
type InMemoryTokenStorage ¶
type InMemoryTokenStorage struct {
// contains filtered or unexported fields
}
func NewInMemoryTokenStorage ¶
func NewInMemoryTokenStorage(initialTokens int64) *InMemoryTokenStorage
func (*InMemoryTokenStorage) GetTokens ¶
func (s *InMemoryTokenStorage) GetTokens() (int64, error)
func (*InMemoryTokenStorage) SetTokens ¶
func (s *InMemoryTokenStorage) SetTokens(tokens int64) error
type Refiller ¶
type Refiller interface { StartRefiller(ctx context.Context, tokensChannel chan<- int64) error StopRefiller() error }
Refiller is likely a background job to refill a bucket automatically.
type TimeRefiller ¶
type TimeRefiller struct {
// contains filtered or unexported fields
}
func NewTimeRefiller ¶
func NewTimeRefiller(refillRate int64, refillRateTime time.Duration) *TimeRefiller
func (*TimeRefiller) StartRefiller ¶
func (r *TimeRefiller) StartRefiller(ctx context.Context, tokensChannel chan<- int64) error
StartRefiller starts a background job and for each time waited iteration it sends the refill number to the tokens channel.
func (*TimeRefiller) StopRefiller ¶
func (r *TimeRefiller) StopRefiller() error
StopRefiller stops the background job. CAUTION: Just call this method once after starting otherwise it'll block your program.