ratelimit

package module
v0.0.0-...-4e9052a Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2024 License: BSD-3-Clause Imports: 7 Imported by: 0

Documentation

Overview

Package ratelimit implements distributed rate limiting using redis function.

Index

Constants

This section is empty.

Variables

View Source
var Error = errors.New("ratelimit")

Functions

This section is empty.

Types

type FixedWindow

type FixedWindow struct {
	Now func() time.Time
	// contains filtered or unexported fields
}

FixedWindow implements the Fixed Window algorithm.

func NewFixedWindow

func NewFixedWindow(client *redis.Client, opt *FixedWindowOption) *FixedWindow

func (*FixedWindow) Allow

func (r *FixedWindow) Allow(ctx context.Context, key string) (*Result, error)

func (*FixedWindow) AllowN

func (r *FixedWindow) AllowN(ctx context.Context, key string, n int64) (*Result, error)

type FixedWindowOption

type FixedWindowOption struct {
	Limit  int64
	Period time.Duration
}

func (*FixedWindowOption) Valid

func (opt *FixedWindowOption) Valid() error

type GCRA

type GCRA struct {
	Now func() time.Time
	// contains filtered or unexported fields
}

func NewGCRA

func NewGCRA(client *redis.Client, opt *GCRAOption) *GCRA

func (*GCRA) Allow

func (g *GCRA) Allow(ctx context.Context, key string) (*Result, error)

func (*GCRA) AllowN

func (g *GCRA) AllowN(ctx context.Context, key string, n int) (*Result, error)

type GCRAOption

type GCRAOption struct {
	Burst  int
	Limit  int
	Period time.Duration
}

type Key

type Key string

func (Key) Format

func (k Key) Format(args ...any) string

type Result

type Result struct {
	Allow     bool
	Limit     int64
	Remaining int64
	ResetAt   time.Time
	RetryAt   time.Time
}

func (*Result) ResetIn

func (r *Result) ResetIn() time.Duration

func (*Result) RetryIn

func (r *Result) RetryIn() time.Duration

func (*Result) Wait

func (r *Result) Wait()

type TokenBucket

type TokenBucket struct {
	Now func() time.Time
	// contains filtered or unexported fields
}

func NewTokenBucket

func NewTokenBucket(client *redis.Client, opt *TokenBucketOption) *TokenBucket

func (*TokenBucket) Allow

func (tb *TokenBucket) Allow(ctx context.Context, key string) (*Result, error)

func (*TokenBucket) AllowN

func (tb *TokenBucket) AllowN(ctx context.Context, key string, n int) (*Result, error)

type TokenBucketOption

type TokenBucketOption struct {
	Limit  int64
	Period time.Duration
	Burst  int64
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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