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: 4 Imported by: 0

Documentation

Overview

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

Index

Constants

This section is empty.

Variables

View Source
var Defaultopt = Options{
	MaxSize:      100,
	WarningSize:  80,
	MaxTTL:       60 * time.Second,
	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, maxsize int64) error

钩子函数

type Limiter

type Limiter struct {
	Key string

	Client redis.UniversalClient
	// contains filtered or unexported fields
}

Limiter 分布式限制器

func New

func New(client redis.UniversalClient, key string, opts ...Option) (*Limiter, error)

New 创建一个限制器 @params client redis.UniversalClient 客户端对象 @params key string bitmap使用的key @params opts ...Option limiter的可设置项

func (*Limiter) Capacity

func (c *Limiter) Capacity() int64

Capacity 限制器最大容量

func (*Limiter) Flood

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

Flood 灌注 当返回为true说明已满,false说明未满

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)

WaterLevel 当前水位

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 WithMaxSize

func WithMaxSize(maxsize int64) Option

WithMaxSize 设置最大水位,必须大于0

func WithMaxTTL

func WithMaxTTL(maxTTL time.Duration) Option

WithMaxTTL 设置最大过期时间

func WithWarningHook

func WithWarningHook(hook Hook) Option

WithWarningHook 设置警戒水位钩子

func WithWarningSize

func WithWarningSize(warningSize int64) Option

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

type Options

type Options struct {
	MaxSize      int64
	WarningSize  int64
	MaxTTL       time.Duration
	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