ratelimit

package
v0.0.0-...-99e36ab Latest Latest
Warning

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

Go to latest
Published: May 14, 2021 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package ratelimit provides a basic token-bucket limiter for rosetta router. This can be used to prevent users from spamming the bot and overload discord API.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bucket

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

Bucket implements a simple bucket containing multiple tokens.

func NewBucket

func NewBucket(burst int, restore time.Duration) *Bucket

NewBucket creates a new limiter with given burst and restoration value.

func (*Bucket) Take

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

Take returns true when token is available to be taken, else false as well as duration til next token is available.

type Manager

type Manager interface {

	// GetBucket returns a token-bucket rate limiter from given context.
	GetBucket(cmd rosetta.Command, uid, gid string) *Bucket

	// GetExecutions returns our internal execution mapping.
	GetExecutions() *timedmap.TimedMap
}

Manager holds multiple buckets, act as an interface for rate limiter.

type RateLimiter

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

RateLimiter implements a managers of rate limiters. This can also be parsed a custom Manager instance if you want to handle limiters differently.

func New

func New(m ...Manager) *RateLimiter

New returns a new instance of Rate Limiter.

func (*RateLimiter) GetLayer

func (r *RateLimiter) GetLayer() rosetta.MiddlewareLayer

func (*RateLimiter) Handle

func (r *RateLimiter) Handle(cmd rosetta.Command, ctx rosetta.Context, layer rosetta.MiddlewareLayer) (bool, error)

Jump to

Keyboard shortcuts

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