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
This commit is contained in:
wood chen 2025-02-17 17:56:29 +08:00
parent 429664b598
commit 69050adf57

View File

@ -181,22 +181,26 @@ func GetTargetURL(client *http.Client, r *http.Request, pathConfig config.PathCo
ext = ext[1:] // 移除开头的点 ext = ext[1:] // 移除开头的点
// 先检查是否在扩展名映射中 // 先检查是否在扩展名映射中
if altTarget, exists := pathConfig.GetExtensionTarget(ext); exists { if altTarget, exists := pathConfig.GetExtensionTarget(ext); exists {
// 先检查扩展名映射的目标是否可访问
if isTargetAccessible(client, altTarget+path) {
// 检查文件大小 // 检查文件大小
contentLength := r.ContentLength contentLength := r.ContentLength
if contentLength <= 0 { if contentLength <= 0 {
// 如果无法获取 Content-Length尝试发送 HEAD 请求 // 如果无法获取 Content-Length尝试发送 HEAD 请求到备用源
if size, err := GetFileSize(client, pathConfig.DefaultTarget+path); err == nil { if size, err := GetFileSize(client, altTarget+path); err == nil {
contentLength = size contentLength = size
log.Printf("[FileSize] Path: %s, Size: %s (from %s)", log.Printf("[FileSize] Path: %s, Size: %s (from alternative source)",
path, FormatBytes(contentLength), path, FormatBytes(contentLength))
func() string {
if isCacheHit(pathConfig.DefaultTarget + path) {
return "cache"
}
return "HEAD request"
}())
} else { } else {
log.Printf("[FileSize] Failed to get size for %s: %v", path, err) 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 { } else {
log.Printf("[FileSize] Path: %s, Size: %s (from Content-Length)", log.Printf("[FileSize] Path: %s, Size: %s (from Content-Length)",
@ -205,19 +209,17 @@ func GetTargetURL(client *http.Client, r *http.Request, pathConfig config.PathCo
// 只有当文件大于阈值时才使用扩展名映射的目标 // 只有当文件大于阈值时才使用扩展名映射的目标
if contentLength > threshold { if contentLength > threshold {
// 检查扩展名映射的目标是否可访问
if isTargetAccessible(client, altTarget+path) {
log.Printf("[Route] %s -> %s (size: %s > %s)", log.Printf("[Route] %s -> %s (size: %s > %s)",
path, altTarget, FormatBytes(contentLength), FormatBytes(threshold)) path, altTarget, FormatBytes(contentLength), FormatBytes(threshold))
targetBase = altTarget targetBase = altTarget
} else {
log.Printf("[Route] %s -> %s (fallback: alternative target not accessible)",
path, targetBase)
}
} else { } else {
log.Printf("[Route] %s -> %s (size: %s <= %s)", log.Printf("[Route] %s -> %s (size: %s <= %s)",
path, targetBase, FormatBytes(contentLength), FormatBytes(threshold)) path, targetBase, FormatBytes(contentLength), FormatBytes(threshold))
} }
} else {
log.Printf("[Route] %s -> %s (fallback: alternative target not accessible)",
path, targetBase)
}
} else { } else {
// 记录没有匹配扩展名映射的情况 // 记录没有匹配扩展名映射的情况
log.Printf("[Route] %s -> %s (no extension mapping)", path, targetBase) log.Printf("[Route] %s -> %s (no extension mapping)", path, targetBase)
@ -257,14 +259,6 @@ func isTargetAccessible(client *http.Client, url string) bool {
return resp.StatusCode >= 200 && resp.StatusCode < 400 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 // SafeInt64 安全地将 interface{} 转换为 int64
func SafeInt64(v interface{}) int64 { func SafeInt64(v interface{}) int64 {
if v == nil { if v == nil {