From 1c9d5bc32629b75a9e082ed5b399e33184dad4ba Mon Sep 17 00:00:00 2001 From: wood chen Date: Mon, 2 Jun 2025 06:50:37 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A7=BB=E9=99=A4=E6=89=A9=E5=B1=95=E5=90=8D?= =?UTF-8?q?=E5=8C=B9=E9=85=8D=E5=99=A8=E7=9A=84=E7=BC=93=E5=AD=98=E6=9C=BA?= =?UTF-8?q?=E5=88=B6=EF=BC=8C=E7=AE=80=E5=8C=96=E7=9B=B8=E5=85=B3=E9=80=BB?= =?UTF-8?q?=E8=BE=91=EF=BC=8C=E4=BC=98=E5=8C=96=E9=85=8D=E7=BD=AE=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E6=97=B6=E7=9A=84=E5=A4=84=E7=90=86=E6=B5=81=E7=A8=8B?= =?UTF-8?q?=EF=BC=8C=E6=8F=90=E5=8D=87=E4=BB=A3=E7=A0=81=E5=8F=AF=E8=AF=BB?= =?UTF-8?q?=E6=80=A7=E5=92=8C=E7=BB=B4=E6=8A=A4=E6=80=A7=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/config/types.go | 32 ----------------- internal/handler/proxy.go | 4 --- internal/utils/utils.go | 75 +++------------------------------------ 3 files changed, 5 insertions(+), 106 deletions(-) diff --git a/internal/config/types.go b/internal/config/types.go index 1a53f3a..a422dc6 100644 --- a/internal/config/types.go +++ b/internal/config/types.go @@ -2,7 +2,6 @@ package config import ( "strings" - "sync" ) type Config struct { @@ -15,11 +14,6 @@ type PathConfig struct { ExtensionMap []ExtRuleConfig `json:"ExtensionMap"` // 扩展名映射规则 ExtRules []ExtensionRule `json:"-"` // 内部使用,存储处理后的扩展名规则 RedirectMode bool `json:"RedirectMode"` // 是否使用302跳转模式 - - // 缓存相关字段(不参与JSON序列化) - matcherCache interface{} `json:"-"` // 缓存的ExtensionMatcher,使用interface{}避免循环导入 - matcherCacheMux sync.RWMutex `json:"-"` // 缓存读写锁 - cacheValid bool `json:"-"` // 缓存是否有效 } // ExtensionRule 表示一个扩展名映射规则(内部使用) @@ -79,32 +73,6 @@ func (p *PathConfig) ProcessExtensionMap() { p.ExtRules = append(p.ExtRules, extRule) } } - - // 清除缓存,因为规则已经改变 - p.InvalidateMatcherCache() -} - -// InvalidateMatcherCache 清除ExtensionMatcher缓存 -func (p *PathConfig) InvalidateMatcherCache() { - p.matcherCacheMux.Lock() - defer p.matcherCacheMux.Unlock() - p.matcherCache = nil - p.cacheValid = false -} - -// GetMatcherCache 获取缓存的ExtensionMatcher -func (p *PathConfig) GetMatcherCache() (interface{}, bool) { - p.matcherCacheMux.RLock() - defer p.matcherCacheMux.RUnlock() - return p.matcherCache, p.cacheValid -} - -// SetMatcherCache 设置ExtensionMatcher缓存 -func (p *PathConfig) SetMatcherCache(matcher interface{}) { - p.matcherCacheMux.Lock() - defer p.matcherCacheMux.Unlock() - p.matcherCache = matcher - p.cacheValid = true } // GetProcessedExtTarget 快速获取扩展名对应的目标URL,如果存在返回true diff --git a/internal/handler/proxy.go b/internal/handler/proxy.go index f880aa8..38f34b3 100644 --- a/internal/handler/proxy.go +++ b/internal/handler/proxy.go @@ -182,10 +182,6 @@ func NewProxyHandler(cfg *config.Config) *ProxyHandler { // 注册配置更新回调 config.RegisterUpdateCallback(func(newCfg *config.Config) { // 注意:config包已经在回调触发前处理了所有ExtRules,这里无需再次处理 - - // 清理所有ExtensionMatcher缓存,因为配置已更新 - utils.ClearAllMatcherCaches(newCfg.MAP) - handler.pathMap = newCfg.MAP handler.prefixTree.update(newCfg.MAP) // 更新前缀匹配树 handler.config = newCfg diff --git a/internal/utils/utils.go b/internal/utils/utils.go index 82a9675..ee76a7e 100644 --- a/internal/utils/utils.go +++ b/internal/utils/utils.go @@ -14,7 +14,6 @@ import ( "sort" "strings" "sync" - "sync/atomic" "time" ) @@ -38,10 +37,6 @@ var ( cacheTTL = 5 * time.Minute accessTTL = 30 * time.Second maxCacheSize = 10000 // 最大缓存条目数 - - // 缓存统计 - matcherCacheHits int64 - matcherCacheMisses int64 ) // 清理过期缓存 @@ -86,19 +81,6 @@ func init() { }) } }() - - // 定期打印缓存统计信息 - go func() { - ticker := time.NewTicker(5 * time.Minute) - for range ticker.C { - hits, misses := GetMatcherCacheStats() - if hits+misses > 0 { - hitRate := float64(hits) / float64(hits+misses) * 100 - log.Printf("[Cache] ExtensionMatcher stats: hits=%d, misses=%d, hit_rate=%.2f%%", - hits, misses, hitRate) - } - } - }() } // GenerateRequestID 生成唯一的请求ID @@ -296,40 +278,7 @@ func extractExtension(path string) string { return "" } -// GetMatcherCacheStats 获取缓存统计信息 -func GetMatcherCacheStats() (hits, misses int64) { - return atomic.LoadInt64(&matcherCacheHits), atomic.LoadInt64(&matcherCacheMisses) -} - -// ResetMatcherCacheStats 重置缓存统计 -func ResetMatcherCacheStats() { - atomic.StoreInt64(&matcherCacheHits, 0) - atomic.StoreInt64(&matcherCacheMisses, 0) -} - -// getOrCreateExtensionMatcher 获取或创建ExtensionMatcher(带缓存和统计) -func getOrCreateExtensionMatcher(pathConfig *config.PathConfig) *ExtensionMatcher { - // 尝试从缓存获取 - if cached, valid := pathConfig.GetMatcherCache(); valid && cached != nil { - if matcher, ok := cached.(*ExtensionMatcher); ok { - atomic.AddInt64(&matcherCacheHits, 1) - return matcher - } - } - - // 缓存未命中,创建新的匹配器 - atomic.AddInt64(&matcherCacheMisses, 1) - matcher := NewExtensionMatcher(pathConfig.ExtRules) - - // 存储到缓存 - pathConfig.SetMatcherCache(matcher) - - log.Printf("[Cache] ExtensionMatcher created and cached for %d rules", len(pathConfig.ExtRules)) - - return matcher -} - -// SelectBestRule 根据文件大小和扩展名选择最合适的规则(优化版本,带缓存) +// SelectBestRule 根据文件大小和扩展名选择最合适的规则(优化版本) // 返回值: (选中的规则, 是否找到匹配的规则, 是否使用了备用目标) func SelectBestRule(client *http.Client, pathConfig config.PathConfig, path string) (*config.ExtensionRule, bool, bool) { // 如果没有扩展名规则,返回nil @@ -340,8 +289,8 @@ func SelectBestRule(client *http.Client, pathConfig config.PathConfig, path stri // 提取扩展名 ext := extractExtension(path) - // 获取或创建缓存的扩展名匹配器 - matcher := getOrCreateExtensionMatcher(&pathConfig) + // 创建扩展名匹配器(可以考虑缓存这个匹配器) + matcher := NewExtensionMatcher(pathConfig.ExtRules) // 获取匹配的规则 matchingRules := matcher.GetMatchingRules(ext) @@ -385,7 +334,7 @@ func SelectBestRule(client *http.Client, pathConfig config.PathConfig, path stri return nil, false, false } -// SelectRuleForRedirect 专门为302跳转优化的规则选择函数(带缓存) +// SelectRuleForRedirect 专门为302跳转优化的规则选择函数 func SelectRuleForRedirect(client *http.Client, pathConfig config.PathConfig, path string) *RuleSelectionResult { result := &RuleSelectionResult{} @@ -405,9 +354,7 @@ func SelectRuleForRedirect(client *http.Client, pathConfig config.PathConfig, pa // 检查扩展名规则 if len(pathConfig.ExtRules) > 0 { ext := extractExtension(path) - - // 获取或创建缓存的扩展名匹配器 - matcher := getOrCreateExtensionMatcher(&pathConfig) + matcher := NewExtensionMatcher(pathConfig.ExtRules) // 快速检查:如果没有任何302跳转规则,跳过复杂逻辑 if !matcher.HasRedirectRule() { @@ -591,15 +538,3 @@ func ParseInt(s string, defaultValue int) int { } return result } - -// ClearAllMatcherCaches 清理所有ExtensionMatcher缓存(用于配置更新时) -func ClearAllMatcherCaches(configMap map[string]config.PathConfig) { - cleared := 0 - for path := range configMap { - pathConfig := configMap[path] - pathConfig.InvalidateMatcherCache() - configMap[path] = pathConfig - cleared++ - } - log.Printf("[Cache] Cleared %d ExtensionMatcher caches due to config update", cleared) -}