From 69050adf5740beef76f10325a1f2dec14e8dd08e Mon Sep 17 00:00:00 2001 From: wood chen Date: Mon, 17 Feb 2025 17:56:29 +0800 Subject: [PATCH] refactor(routing): Improve file size detection and alternative target routing - Enhance file size retrieval with fallback mechanisms for alternative and default sources - Improve logging for file size detection and routing decisions - Remove unused cache hit check function - Optimize routing logic for extension-mapped targets --- internal/utils/utils.go | 64 +++++++++++++++++++---------------------- 1 file changed, 29 insertions(+), 35 deletions(-) diff --git a/internal/utils/utils.go b/internal/utils/utils.go index 895cca0..cbaca56 100644 --- a/internal/utils/utils.go +++ b/internal/utils/utils.go @@ -181,42 +181,44 @@ func GetTargetURL(client *http.Client, r *http.Request, pathConfig config.PathCo ext = ext[1:] // 移除开头的点 // 先检查是否在扩展名映射中 if altTarget, exists := pathConfig.GetExtensionTarget(ext); exists { - // 检查文件大小 - contentLength := r.ContentLength - if contentLength <= 0 { - // 如果无法获取 Content-Length,尝试发送 HEAD 请求 - if size, err := GetFileSize(client, pathConfig.DefaultTarget+path); err == nil { - contentLength = size - log.Printf("[FileSize] Path: %s, Size: %s (from %s)", - path, FormatBytes(contentLength), - func() string { - if isCacheHit(pathConfig.DefaultTarget + path) { - return "cache" - } - return "HEAD request" - }()) + // 先检查扩展名映射的目标是否可访问 + if isTargetAccessible(client, altTarget+path) { + // 检查文件大小 + contentLength := r.ContentLength + if contentLength <= 0 { + // 如果无法获取 Content-Length,尝试发送 HEAD 请求到备用源 + if size, err := GetFileSize(client, altTarget+path); err == nil { + contentLength = size + log.Printf("[FileSize] Path: %s, Size: %s (from alternative source)", + path, FormatBytes(contentLength)) + } else { + log.Printf("[FileSize] Failed to get size from alternative source for %s: %v", path, err) + // 如果从备用源获取失败,尝试从默认源获取 + if size, err := GetFileSize(client, targetBase+path); err == nil { + contentLength = size + log.Printf("[FileSize] Path: %s, Size: %s (from default source)", + path, FormatBytes(contentLength)) + } else { + log.Printf("[FileSize] Failed to get size from default source for %s: %v", path, err) + } + } } else { - log.Printf("[FileSize] Failed to get size for %s: %v", path, err) + log.Printf("[FileSize] Path: %s, Size: %s (from Content-Length)", + path, FormatBytes(contentLength)) } - } else { - log.Printf("[FileSize] Path: %s, Size: %s (from Content-Length)", - path, FormatBytes(contentLength)) - } - // 只有当文件大于阈值时才使用扩展名映射的目标 - if contentLength > threshold { - // 检查扩展名映射的目标是否可访问 - if isTargetAccessible(client, altTarget+path) { + // 只有当文件大于阈值时才使用扩展名映射的目标 + if contentLength > threshold { log.Printf("[Route] %s -> %s (size: %s > %s)", path, altTarget, FormatBytes(contentLength), FormatBytes(threshold)) targetBase = altTarget } else { - log.Printf("[Route] %s -> %s (fallback: alternative target not accessible)", - path, targetBase) + log.Printf("[Route] %s -> %s (size: %s <= %s)", + path, targetBase, FormatBytes(contentLength), FormatBytes(threshold)) } } else { - log.Printf("[Route] %s -> %s (size: %s <= %s)", - path, targetBase, FormatBytes(contentLength), FormatBytes(threshold)) + log.Printf("[Route] %s -> %s (fallback: alternative target not accessible)", + path, targetBase) } } else { // 记录没有匹配扩展名映射的情况 @@ -257,14 +259,6 @@ func isTargetAccessible(client *http.Client, url string) bool { return resp.StatusCode >= 200 && resp.StatusCode < 400 } -// 检查是否命中缓存 -func isCacheHit(url string) bool { - if cache, ok := sizeCache.Load(url); ok { - return time.Since(cache.(fileSizeCache).timestamp) < cacheTTL - } - return false -} - // SafeInt64 安全地将 interface{} 转换为 int64 func SafeInt64(v interface{}) int64 { if v == nil {