Documentation
¶
Overview ¶
Copyright 2020 ratelimit Author(https://github.com/yudeguang/ratelimit). All Rights Reserved.
This Source Code Form is subject to the terms of the MIT License. If a copy of the MIT was not distributed with this file, You can obtain one at https://github.com/yudeguang/ratelimit.
Copyright 2020 rateLimit Author(https://github.com/yudeguang/ratelimit). All Rights Reserved.
This Source Code Form is subject to the terms of the MIT License. If a copy of the MIT was not distributed with this file, You can obtain one at https://github.com/yudeguang/ratelimit.
Copyright 2020 rateLimit Author(https://github.com/yudeguang/ratelimit). All Rights Reserved.
q Source Code Form is subject to the terms of the MIT License. If a copy of the MIT was not distributed with q file, You can obtain one at https://github.com/yudeguang/ratelimit.
Copyright 2020 rateLimit Author(https://github.com/yudeguang/ratelimit). All Rights Reserved.
This Source Code Form is subject to the terms of the MIT License. If a copy of the MIT was not distributed with this file, You can obtain one at https://github.com/yudeguang/ratelimit.
Copyright 2020 rateLimit Author(https://github.com/yudeguang/ratelimit). All Rights Reserved.
This Source Code Form is subject to the terms of the MIT License. If a copy of the MIT was not distributed with this file, You can obtain one at https://github.com/yudeguang/ratelimit.
Copyright 2020 ratelimit Author(https://github.com/yudeguang/ratelimit). All Rights Reserved.
This Source Code Form is subject to the terms of the MIT License. If a copy of the MIT was not distributed with this file, You can obtain one at https://github.com/yudeguang/ratelimit.
Index ¶
- Constants
- func IsIP(ip string) bool
- type Rule
- func (r *Rule) AddRule(defaultExpiration time.Duration, numberOfAllowedAccesses int, ...)
- func (r *Rule) AllowVisit(key interface{}) bool
- func (r *Rule) AllowVisitByIP4(ip string) bool
- func (r *Rule) GetCurOnlineUsers() []string
- func (r *Rule) LoadingAndAutoSaveToDisc(backupFileName string, backUpInterval ...time.Duration)
- func (r *Rule) ManualEmptyVisitorRecordsOf(key interface{})
- func (r *Rule) PrintRemainingVisits(key interface{}, language ...int)
- func (r *Rule) RemainingVisits(key interface{}) []int
- func (r *Rule) RemainingVisitsByIP4(ip string) []int
- func (r *Rule) SaveToDiscOnce() (err error)
Constants ¶
const ( Chinese = iota English )
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Rule ¶
type Rule struct {
// contains filtered or unexported fields
}
用户访问控制策略,可由一个或多个访问控制规则组成
func NewRule ¶
func NewRule() *Rule
初始化一个多重规则的频率控制策略,例: r := NewRule() 初始化之后,紧跟着需要调用AddRule方法增加一条或若干条用户访问控制策略,增加用户访问控制策略后,才可以正式使用
func (*Rule) AddRule ¶
func (r *Rule) AddRule(defaultExpiration time.Duration, numberOfAllowedAccesses int, estimatedNumberOfOnlineUserNum ...int)
增加用户访问控制策略,例: r.AddRule(time.Minute*5, 20) r.AddRule(time.Minute*30, 50) r.AddRule(time.Hour*24, 200) 它表示: 在5分钟内每个用户最多允许访问20次 在30分钟内每个用户最多允许访问50次 在24小时内每个用户最多允许访问200次 其中: defaultExpiration 表示在某个时间段内 numberOfAllowedAccesses 表示允许访问的次数 estimatedNumberOfOnlineUserNum 表示预计可能有多少人访问,此参数为可变参数,可不填写 以上任何一条用户访问控制策略没通过,都不允许访问,注意单条规则中,不宜设定监控时间段过大的规则,比如设定监控某个用户一个月甚至是1年的访问规则,它会占用大多的内存
func (*Rule) AllowVisit ¶
是否还允许某用户访问,如果访问量过多,超出各细分规则中任何一条规则规定的访问量,则不允许访问 无论是否允许访问都会尝试在各细分访问规则记录中增加一条访问日志记录,函数AllowVisit也可以认为 是AddRecords 例: AllowVisit("username")
func (*Rule) AllowVisitByIP4 ¶
以IP作为用户名,判断该用户是否允许访问,例: AllowVisitByIP4("127.0.0.1") 在实际的网站运营中,往往需要以IP作为判断用户的标准,IP转换为int64存储可略微减少内存占用
func (*Rule) GetCurOnlineUsers ¶
获得当前所有的在线用户,注意所有用int64存储的用户会被默认认为是IP地址,会被自动转换为IP的字符串形式输出以方便查看 如果不是本身就是以int64形式存储,而不是IP4,那么可以用ip4StringToInt64自己再转换回去
func (*Rule) LoadingAndAutoSaveToDisc ¶
如果有历史备份文件,则加载,无历史备份文件则后续自动生成,并且开启自动保存,默认60秒完成一次存盘
func (*Rule) ManualEmptyVisitorRecordsOf ¶
func (r *Rule) ManualEmptyVisitorRecordsOf(key interface{})
人工清空某用户的访问数据,主要针对某些特定客户的个性化需求,比如某个客户要求临时允许其访问更多的页面, 此时,调用出函数,清空其历史访问数据,间接实现这个目的,例: ManualEmptyVisitorRecordsOf("andyyu")
func (*Rule) PrintRemainingVisits ¶
打印各细分规则下的剩余访问次数
func (*Rule) RemainingVisits ¶
某用户剩余访问次数,例: RemainingVisits("username")
func (*Rule) RemainingVisitsByIP4 ¶
以IP作为用户名,此用户剩余访问次数,例: RemainingVisitsByIP4("127.0.0.1")
func (*Rule) SaveToDiscOnce ¶
把数据保存到硬盘上,仅支持key为string,int,int64等类型数据的缓存