limit

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 11, 2024 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// Unknown 未知状态码
	Unknown = 0
	// Allowed 小于配额状态码
	Allowed = 1
	// Reached 达到配额状态码
	Reached = 2
	// Overflowed 超出配额状态码
	Overflowed = 3

	// InvalidQuota 无效配额
	InvalidQuota = -1
)

Variables

View Source
var (
	// ErrUnknownCode 未知状态码错误
	ErrUnknownCode = stderrors.New("unknown code")
	// ErrUnexpectedType 意外类型错误
	ErrUnexpectedType = stderrors.New("unexpected type")
)

Functions

This section is empty.

Types

type Option

type Option func(op *option)

Option 限流器可选配置

func WithKeyPrefix

func WithKeyPrefix(keyPrefix string) Option

WithKeyPrefix 指定限流器键前缀

func WithPeriod

func WithPeriod(period int) Option

WithPeriod 指定限流器时间段

func WithQuota

func WithQuota(quota int) Option

WithQuota 指定限流器配额

type PeriodLimit

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

PeriodLimit 时间段限流器

func MustNewPeriodLimit

func MustNewPeriodLimit(period, quota int, keyPrefix string, store *xkv.Store) *PeriodLimit

MustNewPeriodLimit 新建时间段限流器

func NewPeriodLimit

func NewPeriodLimit(period, quota int, keyPrefix string, store *xkv.Store) (*PeriodLimit, error)

NewPeriodLimit 新建时间段限流器

func (*PeriodLimit) Allow

func (pl *PeriodLimit) Allow(key string, opts ...Option) (bool, error)

Allow 访问时间段限流器拿取配额,并判断是否允许放行

func (*PeriodLimit) Get

func (pl *PeriodLimit) Get(key string, opts ...Option) (int, error)

Get 访问时间段限流器拿取配额,并返回剩余配额,返回值含义

> 0:剩余配额大于 0,Allowed
= 0:剩余配额等于 0,Reached
< 0:剩余配额小于 0,Overflowed

func (*PeriodLimit) Take

func (pl *PeriodLimit) Take(key string, opts ...Option) (int, error)

Take 访问时间段限流器拿取配额,并返回相关状态码

type TokenLimit

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

TokenLimit 令牌桶限流器

func MustNewTokenLimit

func MustNewTokenLimit(rate, capacity int, key string, store *xkv.Store) *TokenLimit

MustNewTokenLimit 新建令牌桶限流器

func NewTokenLimit

func NewTokenLimit(rate, capacity int, key string, store *xkv.Store) (*TokenLimit, error)

NewTokenLimit 新建令牌桶限流器

func (*TokenLimit) Allow

func (tl *TokenLimit) Allow(opts ...TokenOption) (bool, error)

Allow 访问令牌桶限流器拿取令牌,并判断是否允许拿取

func (*TokenLimit) AllowN

func (tl *TokenLimit) AllowN(now time.Time, n int, opts ...TokenOption) (bool, error)

AllowN 访问令牌桶限流器拿取 n 个令牌,并判断是否允许拿取

type TokenOption

type TokenOption func(op *tokenOption)

TokenOption 令牌桶限流器可选配置

func WithCapacity

func WithCapacity(capacity int) TokenOption

WithCapacity 指定令牌桶限流器容量

func WithKey

func WithKey(key string) TokenOption

WithKey 指定令牌桶限流器键

func WithRate

func WithRate(rate int) TokenOption

WithRate 指定令牌桶限流器速率

Jump to

Keyboard shortcuts

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