ratelimit

package
v0.0.0-...-9742f5a Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2020 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrTooMuchData = errors.New("too much data sent; please retry later")

ErrTooMuchData indicates when too much data has been sent.

Functions

This section is empty.

Types

type Config

type Config struct {
	// Logger for debug and info messages related to rate limiting.
	Logger *logrus.Logger
	// Maximum burst rate
	Cap int64
	// Refresh burst tokens over this interval
	RefreshInterval time.Duration
	// Refresh this amount / interval
	RefreshAmount int64
}

Config is the configuration passed to the rate limiter.

type RateLimiter

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

RateLimiter is a simple API to rate limiting with the k/v library.

func NewRateLimiter

func NewRateLimiter(c Config, kvClient *kv.Client) *RateLimiter

NewRateLimiter constructs a new RateLimiter

func (*RateLimiter) CASUpdate

func (rl *RateLimiter) CASUpdate(ctx context.Context, dataRateKey string, nonce, byt, newByt []byte) ([]byte, error)

CASUpdate is a safe method to slam in data. Will return kv.ErrNotEqual if the values do not line up after a few attempts. If unset, it will set the initial value instead of doing the update.

func (*RateLimiter) CreateInitialLimit

func (rl *RateLimiter) CreateInitialLimit(ctx context.Context, dataRateKey string) ([]byte, error)

CreateInitialLimit creates an initial limit based off the Cap value.

func (*RateLimiter) RateLimit

func (rl *RateLimiter) RateLimit(ctx context.Context, dataRateKey string, size int64) error

RateLimit rate limits stuff, provided a dataRateKey and a value. Returns ErrTooMuchData if too much data has tripped the limit. Returns other errors on systemic issues.

Jump to

Keyboard shortcuts

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