ratelimit

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Jul 26, 2021 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LimitedCommand

type LimitedCommand interface {

	// GetLimiterBurst returns the maximum ammount
	// of tokens which can be available at a time.
	GetLimiterBurst() int

	// GetLimiterRestoration returns the duration
	// between new tokens are generated.
	GetLimiterRestoration() time.Duration

	// IsLimiterGlobal returns true if the limit
	// shall be handled globally across all guilds.
	// Otherwise, a limiter is created for each
	// guild the user executes the command on.
	IsLimiterGlobal() bool
}

LimitedCommand specifies the structure of a rate limitable command.

type Limiter

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

Limiter handles and calculates the rate limit tokens using a simple token bucket system.

func NewLimiter

func NewLimiter(burst int, restoration time.Duration) *Limiter

NewLimiter initializes a new limiter with the given burst and restoration values.

func (*Limiter) Take

func (l *Limiter) Take() (ok bool, next time.Duration)

Take returns true when a token was available to be taken. Otherwise, false is returned as well as a duration until a next token will be available.

type Manager

type Manager interface {
	// GetLimiter returns a limiter instance from the given
	// cmd instance, userID and guildID.
	//
	// cmd is guaranteed to also implement the LimitedCommand
	// interface when calling this function.
	GetLimiter(cmd shireikan.Command, userID, guildID string) *Limiter
}

Manager provides limiter instances by command instance, userID and guildID.

type Middleware

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

Middleware command implements the ratelimit middleware.

func New

func New(manager ...Manager) *Middleware

New returns a new instance of Middleware.

Optionally, you can pass a custom Manager instance if you want to handle limiters differently than the standard Manager implementation.

func (*Middleware) GetLayer

func (m *Middleware) GetLayer() shireikan.MiddlewareLayer

func (*Middleware) Handle

Jump to

Keyboard shortcuts

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