limiter

package
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: Jan 18, 2022 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package limiter 限制器 可以用于防止短时间内大量请求同时处理,比如缓存防击穿,防爬虫等

Index

Constants

This section is empty.

Variables

View Source
var Defaultopt = Options{
	WarningSize:  80,
	AsyncHooks:   false,
	WarningHooks: []Hook{},
	FullHooks:    []Hook{},
}

Defaultopt 默认的可选配置

View Source
var ErrLimiterMaxSizeMustLargerThanWaringSize = errors.New("limiter的最大容量必须大于警戒容量")

ErrLimiterMaxSizeMustLargerThanWaringSize limiter的最大容量必须大于警戒容量

Functions

This section is empty.

Types

type Hook

type Hook func(res *redis_cell.RedisCellStatus) error

钩子函数

type Limiter

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

Limiter 分布式限制器

func New

func New(cell *redis_cell.RedisCell, opts ...Option) (*Limiter, error)

FromRedisCell 从令牌桶对象中转化得到限制器

func (*Limiter) Capacity

func (c *Limiter) Capacity() int64

Capacity 限制器最大容量

func (*Limiter) Flood

func (c *Limiter) Flood(ctx context.Context, value int64) (bool, error)

func (*Limiter) IsFull

func (c *Limiter) IsFull(ctx context.Context) (bool, error)

IsFull 观测水位是否已满

func (*Limiter) Reset

func (c *Limiter) Reset(ctx context.Context) error

func (*Limiter) WaterLevel

func (c *Limiter) WaterLevel(ctx context.Context) (int64, error)

type Option

type Option interface {
	Apply(*Options)
}

Option configures how we set up the connection.

func WithAsyncHooks

func WithAsyncHooks() Option

WithRunHooksAsync 设置钩子并发执行

func WithFullHook

func WithFullHook(hook Hook) Option

WithFullHook 设置水满时触发的钩子

func WithWarningHook

func WithWarningHook(hook Hook) Option

WithWarningHook 设置警戒水位钩子

func WithWarningSize

func WithWarningSize(warningSize int64) Option

WithWarningSize 设置警戒水位,必须大于0

type Options

type Options struct {
	WarningSize  int64
	AsyncHooks   bool
	WarningHooks []Hook
	FullHooks    []Hook
}

Options broker的配置

Jump to

Keyboard shortcuts

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