ratelimit

package module
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2022 License: MIT Imports: 3 Imported by: 0

README

Rate Limit

A bunch of rate limiting libraries.

  • Token Bucket
  • Sliding Window
  • Sliding Window with Approximation

API

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MillisToTime added in v0.0.3

func MillisToTime(m int64) time.Time

func TimeMillis added in v0.0.3

func TimeMillis(t time.Time) int64

Types

type Bucket

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

func NewTokenBucket

func NewTokenBucket(identifier string, rate, windowSize int, store Store) Bucket

NewTokenBucket returns a new rate limiter which uses the token bucket algorithm.

func (*Bucket) Allow

func (b *Bucket) Allow(key string) (bool, error)

func (*Bucket) AllowWithStatus

func (b *Bucket) AllowWithStatus(key string) (Status, error)

type RedigoStore

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

func NewRedigoSWStore added in v0.0.4

func NewRedigoSWStore(pool *redis.Pool) RedigoStore

func NewRedigoStore

func NewRedigoStore(pool *redis.Pool) RedigoStore

func (*RedigoStore) Inc

func (s *RedigoStore) Inc(key string, rate, windowSize, now int) (StoreResponse, error)

type SlidingWindow added in v0.0.4

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

func NewSlidingWindow added in v0.0.4

func NewSlidingWindow(identifier string, rate, windowSize int, store Store) SlidingWindow

NewSlidingWindow returns a new rate limiter which uses the token bucket algorithm.

func (*SlidingWindow) Allow added in v0.0.4

func (sw *SlidingWindow) Allow(key string) (bool, error)

func (*SlidingWindow) AllowWithStatus added in v0.0.4

func (sw *SlidingWindow) AllowWithStatus(key string) (Status, error)

type Status

type Status struct {
	Allowed     bool
	Remaining   int
	NextRefresh time.Duration
}

type Store

type Store interface {
	Inc(key string, rate, windowSize, now int) (StoreResponse, error)
}

type StoreResponse

type StoreResponse struct {
	Allowed    bool
	Counter    int
	LastRefill time.Time
}

Jump to

Keyboard shortcuts

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