完善项目细节

This commit is contained in:
user123456
2025-06-13 13:59:06 +08:00
parent 4756ada922
commit 8ffceb7f2b
14 changed files with 42 additions and 239 deletions

View File

@@ -8,7 +8,7 @@ import (
// SmartRateLimit 智能限流会话管理
type SmartRateLimit struct {
sessions sync.Map // IP -> *PullSession
sessions sync.Map
}
// PullSession Docker拉取会话
@@ -20,35 +20,27 @@ type PullSession struct {
// 全局智能限流实例
var smartLimiter = &SmartRateLimit{}
// 硬编码的智能限流参数 - 无需配置管理
const (
// manifest请求后的活跃窗口时间
activeWindowDuration = 3 * time.Minute
// 活跃窗口内最大免费blob请求数(防止滥用)
maxFreeBlobRequests = 100
// 会话清理间隔
sessionCleanupInterval = 10 * time.Minute
// 会话过期时间
sessionExpireTime = 30 * time.Minute
)
func init() {
// 启动会话清理协程
go smartLimiter.cleanupSessions()
}
// ShouldSkipRateLimit 判断是否应该跳过限流计数
// 返回true表示跳过限流false表示正常计入限流
func (s *SmartRateLimit) ShouldSkipRateLimit(ip, path string) bool {
// 提取请求类型
requestType, _ := parseRequestInfo(path)
// 只对manifest和blob请求做智能处理
if requestType != "manifests" && requestType != "blobs" {
return false // 其他请求正常计入限流
return false
}
// 获取或创建会话
sessionKey := ip
sessionInterface, _ := s.sessions.LoadOrStore(sessionKey, &PullSession{})
session := sessionInterface.(*PullSession)
@@ -56,35 +48,28 @@ func (s *SmartRateLimit) ShouldSkipRateLimit(ip, path string) bool {
now := time.Now()
if requestType == "manifests" {
// manifest请求始终计入限流但更新会话状态
session.LastManifestTime = now
session.RequestCount = 0 // 重置计数
return false // manifest请求正常计入限流
session.RequestCount = 0
return false
}
// blob请求检查是否在活跃窗口内
if requestType == "blobs" {
// 检查是否在活跃拉取窗口内
if !session.LastManifestTime.IsZero() &&
now.Sub(session.LastManifestTime) <= activeWindowDuration {
// 在活跃窗口内,检查是否超过最大免费请求数
session.RequestCount++
if session.RequestCount <= maxFreeBlobRequests {
return true // 跳过限流计数
return true
}
}
}
return false // 正常计入限流
return false
}
// parseRequestInfo 解析请求路径,提取请求类型和镜像引用
func parseRequestInfo(path string) (requestType, imageRef string) {
// 清理路径前缀
path = strings.TrimPrefix(path, "/v2/")
// 查找manifest或blob路径
if idx := strings.Index(path, "/manifests/"); idx != -1 {
return "manifests", path[:idx]
}
@@ -107,7 +92,6 @@ func (s *SmartRateLimit) cleanupSessions() {
now := time.Now()
expiredKeys := make([]string, 0)
// 找出过期的会话
s.sessions.Range(func(key, value interface{}) bool {
session := value.(*PullSession)
if !session.LastManifestTime.IsZero() &&
@@ -117,7 +101,6 @@ func (s *SmartRateLimit) cleanupSessions() {
return true
})
// 删除过期会话
for _, key := range expiredKeys {
s.sessions.Delete(key)
}