redis_cell

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Sep 13, 2021 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Defaultopt = Options{
	MaxBurst:       99,
	CountPerPeriod: 30,
	Period:         60,
}

Functions

This section is empty.

Types

type Option

type Option interface {
	Apply(*Options)
}

Option configures how we set up the connection.

func WithCountPerPeriod

func WithCountPerPeriod(countPerPeriod int64) Option

WithCountPerPeriod 设置时间间隔内的token消减数

func WithMaxBurst

func WithMaxBurst(maxBurst int64) Option

WithMaxBurst 设置最大熔断数,最大token数为MaxBurst+1

func WithPeriod

func WithPeriod(period int64) Option

WithPeriod 设置token消减间隔时长,单位s

type Options

type Options struct {
	MaxBurst       int64
	CountPerPeriod int64
	Period         int64
}

Options broker的配置

type RedisCell

type RedisCell struct {
	*clientkey.ClientKey
	// contains filtered or unexported fields
}

RedisCell 令牌桶对象

func New

func New(k *clientkey.ClientKey, opts ...Option) (*RedisCell, error)

New 创建一个新的令牌桶对象 @params k *key.Key redis客户端的键对象

func (*RedisCell) Capacity

func (c *RedisCell) Capacity() int64

Capacity 最大容量

func (*RedisCell) ClThrottle

func (c *RedisCell) ClThrottle(ctx context.Context, count int64) (*RedisCellStatus, error)

ClThrottle 增加桶中token数并返回桶状态 @params count int64 增加的token数量

type RedisCellStatus

type RedisCellStatus struct {
	Blocked          bool  `json:"blocked"`
	Max              int64 `json:"max"`
	Remaining        int64 `json:"remaining"`
	WaitForRetryTime int64 `json:"wait_for_retry_time"` //单位s,为-1表示不用等
	ResetToMaxTime   int64 `json:"reset_to_max_time"`   //单位s
}

RedisCellStatus 令牌桶的状态信息

Directories

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

Jump to

Keyboard shortcuts

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