xfy d21e27fbac fix(lint): 修复 golangci-lint 错误 (119 -> 0 issues)
主要修复:
- errcheck: defer Close 使用 //nolint:errcheck,类型断言改为 ok 检查
- govet fieldalignment: 调整结构体字段顺序优化内存布局
- revive unused-parameter: 将未使用参数改为 _
- exhaustive: 添加缺失的 switch case 或 default
- goconst: 提取重复字符串为常量 (accessAllow, accessDeny 等)
- staticcheck SA9003: 修复空分支逻辑
- gofmt: 运行 gofmt -w 格式化
- nolintlint: 修复 nolint 注释格式

其他改进:
- 更新 .golangci.yml 配置,启用更严格的检查
- 移除未使用的代码和导入
- 简化测试辅助函数调用

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-13 16:15:31 +08:00

105 lines
2.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Package resolver 提供 DNS 解析功能,支持缓存和后台刷新。
//
// 该文件包含 DNS 缓存相关的实现。
//
// 作者xfy
package resolver
import (
"sync"
"time"
)
// CacheStats 返回缓存统计信息。
type CacheStats struct {
Hits int64 // 缓存命中次数
Misses int64 // 缓存未命中次数
Entries int // 当前缓存条目数
Expired int // 过期条目数
}
// GetCacheStats 返回当前缓存统计信息。
// 这是一个辅助函数,用于测试和监控。
func (r *DNSResolver) GetCacheStats() CacheStats {
hits := r.hits.Load()
misses := r.misses.Load()
// 统计缓存条目
var entries, expired int
now := time.Now()
r.cache.Range(func(_ interface{}, value interface{}) bool {
entries++
entry, ok := value.(*DNSCacheEntry)
if !ok {
return true
}
entry.mu.RLock()
if now.After(entry.ExpiresAt) {
expired++
}
entry.mu.RUnlock()
return true
})
return CacheStats{
Hits: hits,
Misses: misses,
Entries: entries,
Expired: expired,
}
}
// GetCacheEntry 获取指定主机的缓存条目(用于测试)。
func (r *DNSResolver) GetCacheEntry(host string) (*DNSCacheEntry, bool) {
if entry, ok := r.cache.Load(host); ok {
cacheEntry, ok := entry.(*DNSCacheEntry)
if !ok {
return nil, false
}
return cacheEntry, true
}
return nil, false
}
// DeleteCacheEntry 删除指定主机的缓存条目。
func (r *DNSResolver) DeleteCacheEntry(host string) {
r.cache.Delete(host)
r.mu.Lock()
delete(r.refreshHosts, host)
r.mu.Unlock()
}
// ClearCache 清空所有缓存。
func (r *DNSResolver) ClearCache() {
r.cache = sync.Map{}
r.mu.Lock()
r.refreshHosts = make(map[string]struct{})
r.mu.Unlock()
}
// GetHitRate 返回缓存命中率。
func (r *DNSResolver) GetHitRate() float64 {
hits := r.hits.Load()
misses := r.misses.Load()
total := hits + misses
if total == 0 {
return 0
}
return float64(hits) / float64(total)
}
// IsCached 检查指定主机是否在缓存中且未过期。
func (r *DNSResolver) IsCached(host string) bool {
if entry, ok := r.cache.Load(host); ok {
cacheEntry, ok := entry.(*DNSCacheEntry)
if !ok {
return false
}
cacheEntry.mu.RLock()
expiresAt := cacheEntry.ExpiresAt
cacheEntry.mu.RUnlock()
return time.Now().Before(expiresAt)
}
return false
}