介绍
go语言gin框架获取用户的用户请求统计用户请求的频率 记录上一次的时间 c *gin.Context 当频率超过一定 禁止该ip访问
核心代码
package config
import (
"github.com/gin-gonic/gin"
"net/http"
"sync"
"time"
)
/**
* @Author Administrator
* @Description ip限速算法
* @Date 2023/12/20 19:27
* @Version 1.0
*/
// RequestInfo
// @Description: 请求信息
type RequestInfo struct {
LastAccessTime time.Time // 上次访问时间
RequestCount int // 请求计数
}
var (
requestInfoMap = make(map[string]*RequestInfo) // IP到请求信息的映射
mutex = &sync.Mutex{
} // 用于保护requestInfoMap的互斥锁
maxRequests = 30 // 允许的最大请求数
timeWindow = 15 * time.Second // 时间窗口
)
// RateLimitMiddleware
//
// @Description: ip限速算法
// @param c
func RateLimitMiddleware(c *gin.Context) {
ip := c.ClientIP()
mutex.Lock()
defer mutex.Unlock()
// 检查IP是否在map中
info, exists := requestInfoMap[ip]
// 如果IP不存在,初始化并添加到map中
if !exists {
requestInfoMap[ip] = &RequestInfo{
LastAccessTime: time.Now(), RequestCount: 1}
return
}
// 如果IP存在,检查时间窗口
if time.Since(info.LastAccessTime) > timeWindow {
// 如果超过时间窗口,重置请求计数
info.RequestCount = 1
info.LastAccessTime = time.Now()
return
}
// 如果在时间窗口内,增加请求计数
info.RequestCount++
// 如果请求计数超过限制,禁止访问
if info.RequestCount > maxRequests {
c.JSON(http.StatusTooManyRequests, gin.H{
"error": "Too many requests"})
c.Abort()
return
}
// 更新最后访问时间
info.LastAccessTime = time.Now()
}
主运行函数
记得使用基于Gin框架
r.Use(config.RateLimitMiddleware)
效果图
我设置的是15秒为一个单位 访问次数30及以内: