cellhelper

package
v2.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 27, 2022 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

cellhelper redis扩展[redis-cell](https://github.com/brandur/redis-cell)帮助模块,用于返回令牌桶状态

cellhelper redis扩展[redis-cell](https://github.com/brandur/redis-cell)帮助模块,用于返回令牌桶状态

Index

Constants

This section is empty.

Variables

View Source
var Defaultopt = Options{
	Middlewaretype: "redis-ext-cell",
	MaxBurst:       99,
	CountPerPeriod: 30,
	Period:         60,
	MiddlewareOpts: []optparams.Option[middlewarehelper.Options]{},
}

Functions

func RefreshTTL

func RefreshTTL() optparams.Option[ClThrottleOpts]

RefreshTTL 设置总是刷新,该参数对pipeline无效

func RefreshTTLAtFirstTime

func RefreshTTLAtFirstTime() optparams.Option[ClThrottleOpts]

RefreshTTLAtFirstTime 设置使用MaxTTL在key第一次设置时设置过期,该参数对pipeline无效

func WithAutoRefreshInterval

func WithAutoRefreshInterval(autoRefreshInterval string) optparams.Option[Options]

WithAutoRefreshInterval 设置自动刷新过期时间的设置

func WithCountPerPeriod

func WithCountPerPeriod(countPerPeriod int64) optparams.Option[ClThrottleOpts]

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

func WithDefaultCountPerPeriod

func WithDefaultCountPerPeriod(countPerPeriod int64) optparams.Option[Options]

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

func WithDefaultMaxBurst

func WithDefaultMaxBurst(maxBurst int64) optparams.Option[Options]

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

func WithDefaultPeriod

func WithDefaultPeriod(period int64) optparams.Option[Options]

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

func WithKey

func WithKey(key string) optparams.Option[Options]

WithKey 中间件通用设置,指定使用的键,注意设置后namespace依然有效

func WithMaxBurst

func WithMaxBurst(maxBurst int64) optparams.Option[ClThrottleOpts]

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

func WithMaxTTL

func WithMaxTTL(maxTTL time.Duration) optparams.Option[Options]

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

func WithMiddlewaretype

func WithMiddlewaretype(typename string) optparams.Option[Options]

WithMiddlewaretype 设置中间件类型

func WithNamespace

func WithNamespace(ns ...string) optparams.Option[Options]

WithNamespace 中间件通用设置,指定锁的命名空间

func WithPeriod

func WithPeriod(period int64) optparams.Option[ClThrottleOpts]

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

func WithSpecifiedKey

func WithSpecifiedKey(key string) optparams.Option[Options]

WithSpecifiedKey 中间件通用设置,指定使用的键,注意设置key后namespace将失效

func WithTTL

WithTTL 设置总是使用指定的ttl刷新key,该参数对pipeline无效

func WithTTLAtFirstTime

func WithTTLAtFirstTime(t time.Duration) optparams.Option[ClThrottleOpts]

WithTTL 设置总是使用指定的ttl刷新key

func WithTaskCron

func WithTaskCron(taskCron *cron.Cron) optparams.Option[Options]

WithTaskCron 设置定时器

Types

type ClThrottleInPipePlacehold

type ClThrottleInPipePlacehold struct {
	Cmd *redis.Cmd
}

func ClThrottleInPipe

func ClThrottleInPipe(pipe redis.Pipeliner, ctx context.Context, key string, count int64, opts ...optparams.Option[ClThrottleOpts]) *ClThrottleInPipePlacehold

ClThrottleInPipe 增加桶中token数并返回桶状态 @params count int64 增加的token数量 @params opts ...optparams.Option[ClThrottleOpts] 其他参数,注意默认MaxBurst: 99,CountPerPeriod: 30,Period: 60

func (*ClThrottleInPipePlacehold) Result

type ClThrottleOpts

type ClThrottleOpts struct {
	RefreshOpts    []optparams.Option[middlewarehelper.RefreshOpt]
	MaxBurst       int64
	CountPerPeriod int64
	Period         int64
}

ClThrottleOpt ClThrottle的配置

type Options

type Options struct {
	Middlewaretype string
	MaxBurst       int64
	CountPerPeriod int64
	Period         int64
	MiddlewareOpts []optparams.Option[middlewarehelper.Options] //初始化Middleware的配置
}

Options broker的配置

type RedisCell

type RedisCell struct {
	*middlewarehelper.MiddleWareAbc
	// contains filtered or unexported fields
}

RedisCell 令牌桶对象

func New

func New(cli redis.UniversalClient, opts ...optparams.Option[Options]) (*RedisCell, error)

New 创建一个新的令牌桶对象 @params cli redis.UniversalClient

func (*RedisCell) ClThrottle

func (c *RedisCell) ClThrottle(ctx context.Context, count int64, opts ...optparams.Option[ClThrottleOpts]) (*RedisCellStatus, error)

ClThrottle 增加桶中token数并返回桶状态 @params count int64 增加的token数量 @params opts ...optparams.Option[ClThrottleOpts] 其他参数

func (*RedisCell) Clean

func (c *RedisCell) Clean(ctx context.Context) error

Clean 清除cell的key

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 celllimiter 限制器 可以用于防止短时间内大量请求同时处理,比如缓存防击穿,防爬虫等
Package celllimiter 限制器 可以用于防止短时间内大量请求同时处理,比如缓存防击穿,防爬虫等

Jump to

Keyboard shortcuts

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