ratelimit

package module
v0.0.0-...-350281c Latest Latest
Warning

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

Go to latest
Published: Sep 13, 2022 License: MIT Imports: 18 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

在网站的运营中,经常会遇到需要对用户访问次数做限制的情况,比如非常典型的是对于某些付费访问服务,需要对访问频率做比较精确的限制,比如单个用户(或者每个IP)地址每天只允许访问多少次,然后每小时只允许访问多少次等等,ratelimit就是针对这种情况而设计。

不同于网关级限流(包括go.uber.org/ratelimit漏桶限流以及github.com/juju/ratelimit令牌桶限流),本限流方案为业务级限流,适用于平台运营中,精细化的按单个用户,按IP等限流,为业内rdeis滑动窗口限流方案的纯GO替代方案,并且支持持久化(可选),可定期把历史数据备份到本地磁盘,程序重启也可保留之前的访问记录,另外,根据网站实际的运营需求,本库还提供一些比较特殊的函数,诸如ManualEmptyVisitorRecordsOf,允许清空某访客的访问记录,从而达到临时单独增加某用户的访问次数的目的。

github.com/yudeguang/ratelimit底层用一个大小能自动伸缩的环形队列来存储用户访问数据,并发安全,拥有较高性能的同时还非常省内存,同时拥有高达1000W次/秒的处理能力(redis约10W次/秒)。作为对比,与用redis的相关数据结构来实现用户访问控制相比,其用法相对简单。

##使用案例如下

package main

import (
	"fmt"
	"log"
	"strconv
	"sync"
	"time"

	"github.com/yudeguang/ratelimit"
)

func main() {
	log.SetFlags(log.Lshortfile | log.Ltime)
	test1()
	test2()
}

//模拟1000个用户,累计进行总共约1亿次访问测试
func test1() {
	var Visits int //因并发问题num比实际数量稍小
	fmt.Println("\r\n测试1,性能测试,预计耗时1分钟,请耐心等待:")
	//步骤一:初始化
	r := ratelimit.NewRule()
	//步骤二:增加一条或者多条规则组成复合规则,规则必须至少包含一条规则
	//此处对于性能测试,为方便准确计数,只需要添加一条规则
	r.AddRule(time.Second*10, 1000) //每10秒只允许访问1000次
	/*
	r.AddRule(time.Second*10, 10)   //每10秒只允许访问10次
	r.AddRule(time.Minute*30, 1000) //每30分钟只允许访问1000次
	r.AddRule(time.Hour*24, 5000)   //每天只允许访问500次
	*/
	//步骤三(可选):从本地磁盘加载历史访问数据
	r.LoadingAndAutoSaveToDisc("test1", time.Second*10) //设置10秒备份一次(不填写则默认60秒备份一次),备份到程序当前文件夹下,文件名为test1.ratelimit
	log.Println("性能测试正式开始")
	//步骤四:调用函数判断某用户是否允许访问
	/*
	   allow:= r.AllowVisit(user)
	*/
	//构建若干个用户,模拟用户访问
	var users = make(map[string]bool)
	for i := 1; i < 1000; i++ {
		users["user_"+strconv.Itoa(i)] = true
	}
	begin := time.Now()
	//模拟多个协程访问
	chanNum := 200
	var wg sync.WaitGroup
	wg.Add(chanNum)
	for i := 0; i < chanNum; i++ {
		go func(i int, wg *sync.WaitGroup) {
			for ii := 0; ii < 5000; ii++ {
				for user := range users {
					for {
						Visits++
						if !r.AllowVisit(user) {
							break
						}
					}
				}
			}
			wg.Done()
		}(i, &wg)
	}
	//所有线程结束,完工
	wg.Wait()
	t := int(time.Now().Sub(begin).Seconds())
	log.Println("性能测试完成:共计访问", Visits, "次,", "耗时", t, "秒,即每秒约完成", Visits/t, "次操作")
	//步骤五(可选):程序退出前主动手动存盘
	err := r.SaveToDiscOnce() //在自动备份的同时,还支持手动备份,一般在程序要退出时调用此函数
	if err == nil {
		log.Println("完成手动数据备份")
	} else {
		log.Println(err)
	}
}

//模拟用户访问并打印
func test2() {
	fmt.Println("\r\n测试2,模拟用户访问并打印:")
	//步骤一:初始化
	r := ratelimit.NewRule()
	//步骤二:增加一条或者多条规则组成复合规则,规则必须至少包含一条规则
	r.AddRule(time.Second*10, 5)  //每10秒只允许访问5次
	r.AddRule(time.Minute*30, 50) //每30分钟只允许访问50次
	r.AddRule(time.Hour*24, 500)  //每天只允许访问500次
	//步骤三:调用函数判断某用户是否允许访问
	/*
	   allow:= r.AllowVisit(user)
	*/
	//构建若干个用户,模拟用户访问
	users := []string{"andyyu", "tony", "chery"}
	for _, user := range users {
		fmt.Println("\r\n开始模拟以下用户访问:", user)
		for {
			if r.AllowVisit(user) {
				log.Println(user, "访问1次,剩余:", r.RemainingVisits(user))
			} else {
				log.Println(user, "访问过多,稍后再试")
				break
			}
			time.Sleep(time.Second * 1)
		}
	}
	//打印所有用户访问数据情况
	fmt.Println("\r\n开始打印所有用户在相关时间段内详细的剩余访问次数情况:\r\n")
	for _, user := range users {
		fmt.Println(user)
		fmt.Println("     概述:", r.RemainingVisits(user))
		fmt.Println("     具体:")
		r.PrintRemainingVisits(user)
		fmt.Println("")
	}
	/*
	在实际的平台运行过程中,往往会因为各种原因,某个客户的访问量过大,被系统临时禁止访问,这时候
	这个客户就可能会投诉之类的,根据运营的实际需要,就需要手动清除掉某用户的访问记录,让其可以再继续访问。
	对于函数ManualEmptyVisitorRecordsOf(),一般需要自行通过合理的方式,比如自行封装一个HTTP服务来间接调用
	*/
	log.Println("开始测试手动清楚某用户访问记录.")
	log.Println("chery清空访问记录前,剩余:", r.RemainingVisits("chery"))
	r.ManualEmptyVisitorRecordsOf("chery")
	log.Println("chery清空访问记录后,剩余:", r.RemainingVisits("chery"))
}

结果如下:


测试1,性能测试,预计耗时1分钟,请耐心等待:
14:16:00 t.go:35: 性能测试正式开始
14:17:14 t.go:68: 性能测试完成:共计访问 936794614 次, 耗时 73 秒,即每秒约完成 12832802 次操作
14:17:14 t.go:72: 完成手动数据备份

测试2,模拟用户访问并打印:

开始模拟以下用户访问: andyyu
14:17:14 t.go:97: andyyu 访问1次,剩余: [4 49 499]
14:17:15 t.go:97: andyyu 访问1次,剩余: [3 48 498]
14:17:16 t.go:97: andyyu 访问1次,剩余: [2 47 497]
14:17:17 t.go:97: andyyu 访问1次,剩余: [1 46 496]
14:17:18 t.go:97: andyyu 访问1次,剩余: [0 45 495]
14:17:19 t.go:99: andyyu 访问过多,稍后再试

开始模拟以下用户访问: tony
14:17:19 t.go:97: tony 访问1次,剩余: [4 49 499]
14:17:20 t.go:97: tony 访问1次,剩余: [3 48 498]
14:17:21 t.go:97: tony 访问1次,剩余: [2 47 497]
14:17:22 t.go:97: tony 访问1次,剩余: [1 46 496]
14:17:23 t.go:97: tony 访问1次,剩余: [0 45 495]
14:17:24 t.go:99: tony 访问过多,稍后再试

开始模拟以下用户访问: chery
14:17:24 t.go:97: chery 访问1次,剩余: [4 49 499]
14:17:25 t.go:97: chery 访问1次,剩余: [3 48 498]
14:17:26 t.go:97: chery 访问1次,剩余: [2 47 497]
14:17:27 t.go:97: chery 访问1次,剩余: [1 46 496]
14:17:28 t.go:97: chery 访问1次,剩余: [0 45 495]
14:17:29 t.go:99: chery 访问过多,稍后再试

开始打印所有用户在相关时间段内详细的剩余访问次数情况:

andyyu
     概述: [5 45 495]
     具体:
andyyu 在 10s 内共允许访问 5 次,剩余 5
andyyu 在 30m0s 内共允许访问 50 次,剩余 45
andyyu 在 24h0m0s 内共允许访问 500 次,剩余 495

tony
     概述: [0 45 495]
     具体:
tony 在 10s 内共允许访问 5 次,剩余 0
tony 在 30m0s 内共允许访问 50 次,剩余 45
tony 在 24h0m0s 内共允许访问 500 次,剩余 495

chery
     概述: [0 45 495]
     具体:
chery 在 10s 内共允许访问 5 次,剩余 0
chery 在 30m0s 内共允许访问 50 次,剩余 45
chery 在 24h0m0s 内共允许访问 500 次,剩余 495

可以看到,在经过一段时间后,andyyu在10S内的剩余访问次数逐步恢复

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

View Source
const (
	Chinese = iota
	English
)

Variables

This section is empty.

Functions

func IsIP

func IsIP(ip string) bool

判断是否是IP地址,同时支持IP4,IP6

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

func (r *Rule) AllowVisit(key interface{}) bool

是否还允许某用户访问,如果访问量过多,超出各细分规则中任何一条规则规定的访问量,则不允许访问 无论是否允许访问都会尝试在各细分访问规则记录中增加一条访问日志记录,函数AllowVisit也可以认为 是AddRecords 例: AllowVisit("username")

func (*Rule) AllowVisitByIP4

func (r *Rule) AllowVisitByIP4(ip string) bool

以IP作为用户名,判断该用户是否允许访问,例: AllowVisitByIP4("127.0.0.1") 在实际的网站运营中,往往需要以IP作为判断用户的标准,IP转换为int64存储可略微减少内存占用

func (*Rule) GetCurOnlineUsers

func (r *Rule) GetCurOnlineUsers() []string

获得当前所有的在线用户,注意所有用int64存储的用户会被默认认为是IP地址,会被自动转换为IP的字符串形式输出以方便查看 如果不是本身就是以int64形式存储,而不是IP4,那么可以用ip4StringToInt64自己再转换回去

func (*Rule) LoadingAndAutoSaveToDisc

func (r *Rule) LoadingAndAutoSaveToDisc(backupFileName string, backUpInterval ...time.Duration)

如果有历史备份文件,则加载,无历史备份文件则后续自动生成,并且开启自动保存,默认60秒完成一次存盘

func (*Rule) ManualEmptyVisitorRecordsOf

func (r *Rule) ManualEmptyVisitorRecordsOf(key interface{})

人工清空某用户的访问数据,主要针对某些特定客户的个性化需求,比如某个客户要求临时允许其访问更多的页面, 此时,调用出函数,清空其历史访问数据,间接实现这个目的,例: ManualEmptyVisitorRecordsOf("andyyu")

func (*Rule) PrintRemainingVisits

func (r *Rule) PrintRemainingVisits(key interface{}, language ...int)

打印各细分规则下的剩余访问次数

func (*Rule) RemainingVisits

func (r *Rule) RemainingVisits(key interface{}) []int

某用户剩余访问次数,例: RemainingVisits("username")

func (*Rule) RemainingVisitsByIP4

func (r *Rule) RemainingVisitsByIP4(ip string) []int

以IP作为用户名,此用户剩余访问次数,例: RemainingVisitsByIP4("127.0.0.1")

func (*Rule) SaveToDiscOnce

func (r *Rule) SaveToDiscOnce() (err error)

把数据保存到硬盘上,仅支持key为string,int,int64等类型数据的缓存

Jump to

Keyboard shortcuts

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