limiter

package
v0.0.0-...-c18a219 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2023 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Limiter

type Limiter struct {
	Store
	*Rate
}

Limiter controls how frequently events are allowed to happen.

func New

func New(store Store, rate *Rate) *Limiter

New creates an instance of Limiter.

func (*Limiter) Get

func (l *Limiter) Get(ctx context.Context, key string) (Resource, error)

Get returns the limited resource for given key.

func (*Limiter) Peek

func (l *Limiter) Peek(ctx context.Context, key string) (Resource, error)

Peek returns the limited resource for given key, without modification on current values.

type Rate

type Rate struct {
	Period time.Duration
	Count  int64
}

Rate is the rate of limiter.

func ParseRate

func ParseRate(s string) (r *Rate, err error)

ParseRate try parsing a string to Rate. Valid format: 100/5s, 1000/1m...

type Resource

type Resource struct {
	Limit     int64
	Remaining int64
	// reset time in UnixNano
	Reset int64
}

Resource is the limit resource information.

func (Resource) OK

func (r Resource) OK() bool

OK returns whether the resource is valid.

type Store

type Store interface {
	// Get returns the limited resource for given key.
	Get(ctx context.Context, rate *Rate, key string) (Resource, error)
	// Peek returns the limited resource for given key, without modification on current values.
	Peek(ctx context.Context, rate *Rate, key string) (Resource, error)
}

Store is the common interface for limiter stores.

Directories

Path Synopsis
store

Jump to

Keyboard shortcuts

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