ratelimit

package
v0.20.1 Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2024 License: MIT Imports: 6 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Skip

func Skip(ctx *ken.Ctx)

Types

type Config

type Config struct {
	Manager Manager
	Force   bool
}

type LimitedCommand

type LimitedCommand interface {

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

	// GetLimiterRestoration returns the duration
	// between new tokens are generated.
	LimiterRestoration() 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) Restore

func (l *Limiter) Restore()

Restore restores one single token to the limiter.

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 ken.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(cfg ...Config) *Middleware

New returns a new instance of Middleware.

func (*Middleware) After

func (m *Middleware) After(ctx *ken.Ctx, cmdError error) (err error)

func (*Middleware) Before

func (m *Middleware) Before(ctx *ken.Ctx) (next bool, err error)

Jump to

Keyboard shortcuts

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