ratelimit

package module
v0.0.1-rc.1 Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2023 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

View Source
var SlidingWindowScript = `` /* 854-byte string literal not displayed */
View Source
var TokenBucketScript = `` /* 1740-byte string literal not displayed */

Functions

func MillisToTime

func MillisToTime(m int64) time.Time

func TimeMillis

func TimeMillis(t time.Time) int64

Types

type Bucket

type Bucket struct {
	Identifier string
	Rate       int
	WindowSize int
	Store      Store
}

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 {
	Pool   *redis.Pool
	Script *redis.Script
}

func NewRedigoSWStore

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

type SlidingWindow struct {
	Identifier string
	Rate       int
	WindowSize int
	Store      Store
}

func NewSlidingWindow

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

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

func (*SlidingWindow) AllowWithStatus

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