redis

package
v0.0.0-...-d9f8102 Latest Latest
Warning

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

Go to latest
Published: Jan 14, 2021 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// FixAddStr : 固定窗口限流Add脚本, 新增一条固定窗口限流规则。规则中idx为当前窗口的索引值,limit为限额大小,current为当前时间(微妙),
	//	wdwcnt为当前窗口已放行的访问的量
	// Input :
	// 	key : 规则对应的key,2d1b74349305508b-fix为其中前缀
	// 	limit : duration时间内的限额数目
	// 	duration : 时间段
	// 	current: 当前时间 (μs)
	FixAddStr = `` /* 289-byte string literal not displayed */

	// FixGetStr : 固定窗口限流get脚本, 固定窗口限流将整个时间段划分为长度为duration的固定窗口,并为每个窗口维护了idx、wdwcnt分别表示
	//	当前窗口对应的索引和当前窗口期中已经放行的请求数,并通过idx、wdwcnt实现窗口的定位和限额
	// Input:
	//  key: 规则ID
	//	current: 当前时间 (μs)
	// Output:
	// 0 : ok,规则可以放行
	// -1 : 规则在当前窗口已经超过了限额数
	// -2 : 相关的规则不存在
	FixGetStr = `` /* 871-byte string literal not displayed */

	// FixSetStr : 固定窗口限流set脚本,具体请求参见FixAddStr
	FixSetStr = FixAddStr

	// FixDelStr : 固定窗口限流删除脚本,删除一条规则对应的信息
	// Input:
	// 		key : 规则对应的key
	FixDelStr = `
local key = KEYS[1] --key
redis.call('HDEL','2d1b74349305508b-fix'..key,'limit','duration','idx','start','wdwcnt')
`
	// SlideAddStr : 滑动窗口限流add脚本,新增一条滑动窗口限流规则。规则中limit、duration表示在duration时间段内限制访问limit次,
	//	为了提高效率,滑动窗口采用了sortedset进行窗口的滑动(score为时间点),为了保证唯一性 还维护了递增的属性idx
	// Input:
	//	key : 规则id,实际存储的key前缀为2d1b74349305508b-slide-h,2d1b74349305508b-slide-z
	// 	limit : 限额数
	// 	duration : 时间段
	// 	current : 当前时间 (μs)
	SlideAddStr = `` /* 333-byte string literal not displayed */

	// SlideSetStr : 滑动窗口set脚本,参见SlideAddStr
	SlideSetStr = `` /* 325-byte string literal not displayed */

	// SlideDelStr : 滑动窗口del脚本,删除一条限流规则
	// Input:
	//	key : 规则id
	SlideDelStr = `` /* 170-byte string literal not displayed */

	// SlideGetStr : 滑动窗口get脚本, 脚本利用了sortedsort来实现基于时间的排序,每次get请求会根据当前时间current和规则对应的时间段duration 计算当前
	//	窗口的开始时间start,并根据sortedset中score值计算当前窗口已放行的请求数cnt,如果cnt>limit(限额数) 则返回-1,否则返回0表示放行。
	//	对于请求中可能出现的失序的访问,在计算当前窗口期中请求访问量时候,计算的是[start,+无穷间)的请求量
	// Input:
	// 	key : 规则key
	// 	current : 当前时间 (μs)
	// Output:
	//	0 : ok,放行
	//	-1 : 请求数目已经超过限额
	//	-2 : 对应的规则key不存在
	SlideGetStr = `` /* 943-byte string literal not displayed */

	// BucketAddStr : 桶限流add脚本,新增一条桶限流规则。limit、duration表示duration时间段内限制访问limit次语义,max表示最大缓存请求数,waitcnt表示当前等待的
	//	请求数目,span表示相邻两个请求需要等待的时间(用于实现宏观的匀速访问)
	// Input :
	//	key : 规则ID,实际存储的key都具有2d1b74349305508b-bucket前缀
	//	limit : 规定时间段的限额数
	//	duration : 时间段,与limit同时实现duration内限制访问limit次
	//	current : 当前时间 (μs)
	//	max : 桶的容量,能够缓存的最大请求数目
	BucketAddStr = `` /* 365-byte string literal not displayed */

	// BucketGetStr : bucket限流get脚本。get脚本用于从桶限流中获得规则对应信息,包括当前请求是否可放行、是否存在等
	//	桶限流将当前等待的请求数目存至waitcnt属性中,每次get请求前都需要检查该变量是否已经超过了max值,同时get
	//	请求中也对waitcnt进行了二次校验
	// Input :
	//	key : 规则id
	//	current : 当前时间 (μs)
	// Output:
	//	0 : ok
	// -1 : overflow
	// -2 : key not exist
	BucketGetStr = `` /* 1223-byte string literal not displayed */

	// BucketCheckAddr : 桶限流check脚本,用于校验当前规则缓存的请求数目是否已经超过了max限制,每次get请求前都应该先调用该脚本
	// Input :
	//	key : 规则id
	// Output:
	//	0 : ok,当前缓存的请求数目未超过max限制
	// -1 : 当前缓存的请求数目已超过max限制
	// -2 : 规则不存在
	BucketCheckAddr = `` /* 440-byte string literal not displayed */

	// BucketSetAddr : 桶限流set脚本,具体参见BucketAddStr
	BucketSetAddr = BucketAddStr

	// BucketDelAddr : 桶限流del脚本,删除规则对应的所有信息
	// Input :
	//	key : 规则id
	BucketDelAddr = `` /* 126-byte string literal not displayed */

	// TokenAddStr : token限流脚本,新增一条token限流,token限流以固定速率产生token 并在token溢出(>max)时抛弃产生的token,并每次get请求时减少
	//	相应token数目,当当前token<=0返回错误信息。rate用于表示token的产生速率,calstart表示上次计算token数目的时间点,left表示上次get后剩余token数目
	//	max为当前token桶的最大量
	// Input :
	//	key : 规则id,实际存储key为2d1b74349305508b-token
	//	limit : duration时间段内限制访问limit次
	//	duration : 时间段,与limit共同构成duratio时间段内限流limit次语义
	//	current : 当前时间(μs)
	// 	max : token桶的最大数量
	TokenAddStr = `` /* 385-byte string literal not displayed */

	// TokenGetStr : token限流get脚本,用于判断当前请求根据规则是否可以放行。该脚本根据current、calstart、rate计算自上次get以来新产生的token数目
	//	并根据left、max值计算当前剩余token数(min(left+new,max)),最后根据当前token判断本次请求是否可以放行
	// Input :
	//	key : 规则id
	//	current : 当前时间 (μs)
	// Output:
	//	0 : ok,本次请求可以放行
	// -1 : 已无多余token,本次请求不可放行
	// -2 : 相应规则信息不存在
	TokenGetStr = `` /* 829-byte string literal not displayed */

	// TokenSetStr : token限流set脚本,具体参见TokenAddStr
	TokenSetStr = TokenAddStr

	// TokenDelStr : token限流删除脚本,删除所有规则相关的信息
	// Input:
	//	key : 规则id
	TokenDelStr = `` /* 127-byte string literal not displayed */

)
View Source
const (
	// DefaultRedisMaxIdle 连接池中即使没有redis连接仍然需要保持60个空闲连接数,建议根据突发连接数设置辞职
	DefaultRedisMaxIdle = 60
	// DefaultRedisMaxActive 连接池最大打开200个redis连接,如果高并发场景建议提高此值
	DefaultRedisMaxActive = 500
	// DefaultRedisIdleTimeout 空闲连接超时时间,超时后此空闲连接将被关闭
	DefaultRedisIdleTimeout = 0
	// DefaultRedisConnTimeout 连接超时时间,无超时
	DefaultRedisConnTimeout = 0
	// DefaultRedisReadTimeout 读超时时间,1000毫秒,建议根据实际情况调整
	DefaultRedisReadTimeout = 1000
	// DefaultRedisWriteTimeout 写超时时间,3000毫秒,建议根据实际情况调整
	DefaultRedisWriteTimeout = 3000

	// RedisAddScript Add redis.Script
	RedisAddScript = "ADD"
	// RedisDelScript Del redis.Script
	RedisDelScript = "DEL"
	// RedisGetScirpt Get redis.Script
	RedisGetScirpt = "GET"
	// RedisSetScript Set redis.Script
	RedisSetScript = "SET"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type RedisBucket

type RedisBucket struct {
	RedisProxy
}

RedisBucket 分布式Bucket限流器

func (*RedisBucket) Get

func (r *RedisBucket) Get(key string) (int64, error)

Get 获取Bucket限流中key对应规则剩余请求数。Bucket限流会对所有的请求进行缓存,并对抛弃超过max个后的请求,

系统通过ReidsChkScript脚本实现缓存数目的计算,每次请求如果当前请求数目超过了max 则报错返回,为了保证Get时候计数值的正确性,Get时还需要进行Double Check
注意:1. 系统不会进行时间同步,集群间时间同步方法,参见:http://linux.vbird.org/linux_server/0440ntp.php 2.系统对时间的获取精确到了毫秒级
Input :
	key : 限流标识,用于标识要获取限流相关信息的规则
Output :
	int64 : 当前剩余请求数目,<-1000为错误,具体参见common.ErrorReturn*
	error : 成功为nil,否则为具体错误信息

func (*RedisBucket) Init

func (r *RedisBucket) Init(args ...interface{}) error

Init bucket限流的初始化,创建相关的redis.Script。该函数必须在RedisProxy.Init之前调用

由于bucket需要对请求量进行控制,该限流器新增了chk脚本用于检验请求量
Input :
Output :
	error : nil或者相关错误信息

type RedisFixWindow

type RedisFixWindow struct {
	RedisProxy
}

RedisFixWindow 分布式固定窗口限流器

func (*RedisFixWindow) Init

func (r *RedisFixWindow) Init(args ...interface{}) error

Init 固定窗口限流的初始化,创建相关的redis.Script。该函数必须在RedisProxy.Init之前调用

Input :
Output :
	error : nil或者相关错误信息

type RedisInfo

type RedisInfo struct {
	Address string
	Passwd  string
}

RedisInfo redis连接信息,address为ip:port格式

type RedisProxy

type RedisProxy struct {
	//Driver
	RedisClient *redis.Pool
	Scripts     map[int]*redis.Script
}

RedisProxy redis连接代理,用于维护连接池和相关脚本

func (*RedisProxy) Add

func (r *RedisProxy) Add(key string, limit int64, tmDuration time.Duration, others ...interface{}) error

Add 新增一条规则,该函数会调用RedisAddScript完成相关规则的创建。注意:1. 该函数会调用获取时间机制,请务必保证

集群中各主机间时间的同步,具体时间同步建议使用ntp服务,参见:http://linux.vbird.org/linux_server/0440ntp.php 2.系统对时间的操作精确到了ms级别
Input :
	key : 限流标识,用于唯一标识一条限流规则
	limit : tmDuriation时间段内的限流数,与tmDuriation同时实现tmDuriation时间段内限流limit次的语义
	tmDuriation : 时间段, 与limit同时实现tmDuriation时间段内限流limit次的语义
	others : 滑动窗口、固定窗口限流中未用,bucket中表示最大缓存的请求数目,token中表示最大token数量
Output :
	error : nil或者相关错误信息

func (*RedisProxy) Del

func (r *RedisProxy) Del(key string) error

Del 删除key对应规则的所有信息,key不存在并不会报错

Input :
	key : 规则ID
Output :
	erro : nil或者相关错误

func (*RedisProxy) Get

func (r *RedisProxy) Get(key string) (int64, error)

Get 获取key对应规则剩余请求数。注意:1. 系统不会进行时间同步,集群中各服务器之间应采用第三方手段保持时间同步,

集群间时间同步方法,参见:http://linux.vbird.org/linux_server/0440ntp.php 2.系统对时间的获取精确到了毫秒级
各限流方式说明参见lua-string.go
Input :
	key : 限流标识,用于标识要获取限流相关信息的规则
Output :
	int64 : 当前剩余请求数目
	error : 成功为nil,否则为具体错误信息

func (*RedisProxy) Init

func (r *RedisProxy) Init(args ...interface{}) error

Init 负责redis连接池的创建,Add、Get、Del、Set脚本的加载。注意如果初始化的过程中发生错误,

将log.fatal相关信息并退出
Input :
	args : RedisInfo格式,包含连接redis的必要信息,注意address为ip:port格式的字符串
Output :
	error : 成功为nil,否则为相关错误信息

func (*RedisProxy) LoadScript

func (r *RedisProxy) LoadScript() error

LoadScript 根据Lua脚本的sha1值判断是否已经加载脚本至redis,如果加载则返回,否则则加班相关脚本

Input : 需要加载的脚本的信息位于Scripts对应的map中
Output :
	error : 相关错误信息或者nil

func (*RedisProxy) Set

func (r *RedisProxy) Set(key string, limit int64, tmDuration time.Duration, others ...interface{}) error

Set 重置或者新增一条规则,该函数调用RedisSetScript脚本完成相关机制,注意:1. 该函数在相关key存在时,会重置规则信息。

  1. 同样各集群应保证时间同步。3.系统中时间精确到了微妙级别 Input : key : 规则ID limit : tmDuriation时间段内的限额数,与tmDuriation同时实现tmDuriation时间段内限流limit次的语义 tmDuriation : 时间段, 与limit同时实现tmDuriation时间段内限流limit次的语义 others : 滑动窗口、固定窗口限流中未用,bucket中表示最大缓存的请求数目,token中表示最大token数量 Output : erro : nil或者相关错误

type RedisSlideWindow

type RedisSlideWindow struct {
	RedisProxy
}

RedisSlideWindow 分布式滑动窗口限流器

func (*RedisSlideWindow) Init

func (r *RedisSlideWindow) Init(args ...interface{}) error

Init 滑动窗口限流的初始化,创建相关的redis.Script。该函数必须在RedisProxy.Init之前调用

Input :
Output :
	error : nil或者相关错误信息

type RedisToken

type RedisToken struct {
	RedisProxy
}

RedisToken 分布式Token限流器

func (*RedisToken) Init

func (r *RedisToken) Init(args ...interface{}) error

Init token限流的初始化,创建相关的redis.Script。该函数必须在RedisProxy.Init之前调用

Input :
Output :
	error : nil或者相关错误信息

Jump to

Keyboard shortcuts

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