rate

package
v1.1.10 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 11, 2024 License: Apache-2.0, MIT Imports: 7 Imported by: 0

Documentation

Overview

Package rate contains rate limiting strategies for asynq.Handler(s).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Semaphore

type Semaphore struct {
	// contains filtered or unexported fields
}

Semaphore is a distributed counting semaphore which can be used to set maxTokens across multiple asynq servers.

func NewSemaphore

func NewSemaphore(rco asynq.RedisConnOpt, scope string, maxTokens int) *Semaphore

NewSemaphore creates a counting Semaphore for the given scope with the given number of tokens.

func (*Semaphore) Acquire

func (s *Semaphore) Acquire(ctx context.Context) (bool, error)

Acquire attempts to acquire a token from the semaphore. - Returns (true, nil), iff semaphore key exists and current value is less than maxTokens - Returns (false, nil) when token cannot be acquired - Returns (false, error) otherwise

The context.Context passed to Acquire must have a deadline set, this ensures that token is released if the job goroutine crashes and does not call Release.

func (*Semaphore) Close

func (s *Semaphore) Close() error

Close closes the connection to redis.

func (*Semaphore) Release

func (s *Semaphore) Release(ctx context.Context) error

Release will release the token on the counting semaphore.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL