refactor(resolver): 重命名核心类型移除冗余前缀

ResolverStats → Stats
dnsCacheEntry → DNSCacheEntry (导出以符合 Go 命名规范)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
xfy 2026-04-10 09:40:26 +08:00
parent 4e535deb80
commit b28ad378fa
4 changed files with 34 additions and 34 deletions

View File

@ -27,9 +27,9 @@ func (r *DNSResolver) GetCacheStats() CacheStats {
// 统计缓存条目 // 统计缓存条目
var entries, expired int var entries, expired int
now := time.Now() now := time.Now()
r.cache.Range(func(key, value interface{}) bool { r.cache.Range(func(_ interface{}, value interface{}) bool {
entries++ entries++
entry := value.(*dnsCacheEntry) entry := value.(*DNSCacheEntry)
entry.mu.RLock() entry.mu.RLock()
if now.After(entry.ExpiresAt) { if now.After(entry.ExpiresAt) {
expired++ expired++
@ -47,9 +47,9 @@ func (r *DNSResolver) GetCacheStats() CacheStats {
} }
// GetCacheEntry 获取指定主机的缓存条目(用于测试)。 // GetCacheEntry 获取指定主机的缓存条目(用于测试)。
func (r *DNSResolver) GetCacheEntry(host string) (*dnsCacheEntry, bool) { func (r *DNSResolver) GetCacheEntry(host string) (*DNSCacheEntry, bool) {
if entry, ok := r.cache.Load(host); ok { if entry, ok := r.cache.Load(host); ok {
return entry.(*dnsCacheEntry), true return entry.(*DNSCacheEntry), true
} }
return nil, false return nil, false
} }
@ -84,7 +84,7 @@ func (r *DNSResolver) GetHitRate() float64 {
// IsCached 检查指定主机是否在缓存中且未过期。 // IsCached 检查指定主机是否在缓存中且未过期。
func (r *DNSResolver) IsCached(host string) bool { func (r *DNSResolver) IsCached(host string) bool {
if entry, ok := r.cache.Load(host); ok { if entry, ok := r.cache.Load(host); ok {
cacheEntry := entry.(*dnsCacheEntry) cacheEntry := entry.(*DNSCacheEntry)
cacheEntry.mu.RLock() cacheEntry.mu.RLock()
expiresAt := cacheEntry.ExpiresAt expiresAt := cacheEntry.ExpiresAt
cacheEntry.mu.RUnlock() cacheEntry.mu.RUnlock()

View File

@ -44,11 +44,11 @@ type Resolver interface {
Stop() error Stop() error
// Stats 返回统计信息 // Stats 返回统计信息
Stats() ResolverStats Stats() Stats
} }
// ResolverStats 解析器统计信息。 // Stats 解析器统计信息。
type ResolverStats struct { type Stats struct {
CacheHits int64 // 缓存命中次数 CacheHits int64 // 缓存命中次数
CacheMisses int64 // 缓存未命中次数 CacheMisses int64 // 缓存未命中次数
CacheEntries int // 当前缓存条目数 CacheEntries int // 当前缓存条目数
@ -59,7 +59,7 @@ type ResolverStats struct {
// DNSResolver 实现 Resolver 接口的 DNS 解析器。 // DNSResolver 实现 Resolver 接口的 DNS 解析器。
type DNSResolver struct { type DNSResolver struct {
config *config.ResolverConfig config *config.ResolverConfig
cache sync.Map // key: hostname, value: *dnsCacheEntry cache sync.Map // key: hostname, value: *DNSCacheEntry
serverIdx atomic.Uint32 serverIdx atomic.Uint32
// 统计信息 // 统计信息
@ -76,8 +76,8 @@ type DNSResolver struct {
refreshHosts map[string]struct{} // 需要刷新的主机列表 refreshHosts map[string]struct{} // 需要刷新的主机列表
} }
// dnsCacheEntry DNS 缓存条目。 // DNSCacheEntry DNS 缓存条目。
type dnsCacheEntry struct { type DNSCacheEntry struct {
IPs []string IPs []string
ExpiresAt time.Time ExpiresAt time.Time
LastLookup time.Time LastLookup time.Time
@ -136,7 +136,7 @@ func (r *DNSResolver) lookup(ctx context.Context, host string, useCache bool) ([
// 尝试从缓存获取 // 尝试从缓存获取
if useCache { if useCache {
if entry, ok := r.cache.Load(host); ok { if entry, ok := r.cache.Load(host); ok {
cacheEntry := entry.(*dnsCacheEntry) cacheEntry := entry.(*DNSCacheEntry)
cacheEntry.mu.RLock() cacheEntry.mu.RLock()
ips := cacheEntry.IPs ips := cacheEntry.IPs
expiresAt := cacheEntry.ExpiresAt expiresAt := cacheEntry.ExpiresAt
@ -169,7 +169,7 @@ func (r *DNSResolver) lookup(ctx context.Context, host string, useCache bool) ([
} }
// 更新缓存 // 更新缓存
entry := &dnsCacheEntry{ entry := &DNSCacheEntry{
IPs: ips, IPs: ips,
ExpiresAt: time.Now().Add(r.config.TTL()), ExpiresAt: time.Now().Add(r.config.TTL()),
LastLookup: time.Now(), LastLookup: time.Now(),
@ -218,7 +218,7 @@ func (r *DNSResolver) queryWithResolver(ctx context.Context, host, server string
if server != "" { if server != "" {
resolver = &net.Resolver{ resolver = &net.Resolver{
PreferGo: true, PreferGo: true,
Dial: func(ctx context.Context, network, address string) (net.Conn, error) { Dial: func(ctx context.Context, _, _ string) (net.Conn, error) {
d := net.Dialer{} d := net.Dialer{}
return d.DialContext(ctx, "udp", server) return d.DialContext(ctx, "udp", server)
}, },
@ -362,13 +362,13 @@ func (r *DNSResolver) Stop() error {
} }
// Stats 返回统计信息。 // Stats 返回统计信息。
func (r *DNSResolver) Stats() ResolverStats { func (r *DNSResolver) Stats() Stats {
hits := r.hits.Load() hits := r.hits.Load()
misses := r.misses.Load() misses := r.misses.Load()
// 统计缓存条目数 // 统计缓存条目数
var entries int var entries int
r.cache.Range(func(key, value interface{}) bool { r.cache.Range(func(_, _ interface{}) bool {
entries++ entries++
return true return true
}) })
@ -380,7 +380,7 @@ func (r *DNSResolver) Stats() ResolverStats {
avgLatency = time.Duration(r.latencyNs.Load() / count) avgLatency = time.Duration(r.latencyNs.Load() / count)
} }
return ResolverStats{ return Stats{
CacheHits: hits, CacheHits: hits,
CacheMisses: misses, CacheMisses: misses,
CacheEntries: entries, CacheEntries: entries,
@ -392,7 +392,7 @@ func (r *DNSResolver) Stats() ResolverStats {
// noopResolver 是禁用状态下的空实现。 // noopResolver 是禁用状态下的空实现。
type noopResolver struct{} type noopResolver struct{}
func (n *noopResolver) LookupHost(ctx context.Context, host string) ([]string, error) { func (n *noopResolver) LookupHost(_ context.Context, _ string) ([]string, error) {
return nil, fmt.Errorf("resolver is disabled") return nil, fmt.Errorf("resolver is disabled")
} }
@ -400,7 +400,7 @@ func (n *noopResolver) LookupHostWithCache(ctx context.Context, host string) ([]
return n.LookupHost(ctx, host) return n.LookupHost(ctx, host)
} }
func (n *noopResolver) Refresh(host string) error { func (n *noopResolver) Refresh(_ string) error {
return nil return nil
} }
@ -412,6 +412,6 @@ func (n *noopResolver) Stop() error {
return nil return nil
} }
func (n *noopResolver) Stats() ResolverStats { func (n *noopResolver) Stats() Stats {
return ResolverStats{} return Stats{}
} }

View File

@ -108,7 +108,7 @@ func TestCache(t *testing.T) {
r := New(cfg).(*DNSResolver) r := New(cfg).(*DNSResolver)
// 模拟缓存条目 // 模拟缓存条目
r.cache.Store("test.example.com", &dnsCacheEntry{ r.cache.Store("test.example.com", &DNSCacheEntry{
IPs: []string{"192.168.1.1", "192.168.1.2"}, IPs: []string{"192.168.1.1", "192.168.1.2"},
ExpiresAt: time.Now().Add(1 * time.Minute), ExpiresAt: time.Now().Add(1 * time.Minute),
}) })
@ -133,7 +133,7 @@ func TestCache(t *testing.T) {
// 测试缓存过期 // 测试缓存过期
// 更新缓存条目为过期 // 更新缓存条目为过期
r.cache.Store("test.example.com", &dnsCacheEntry{ r.cache.Store("test.example.com", &DNSCacheEntry{
IPs: []string{"192.168.1.1"}, IPs: []string{"192.168.1.1"},
ExpiresAt: time.Now().Add(-1 * time.Second), // 已过期 ExpiresAt: time.Now().Add(-1 * time.Second), // 已过期
}) })
@ -157,13 +157,13 @@ func TestIsCached(t *testing.T) {
r := New(cfg).(*DNSResolver) r := New(cfg).(*DNSResolver)
// 添加未过期的缓存 // 添加未过期的缓存
r.cache.Store("active.example.com", &dnsCacheEntry{ r.cache.Store("active.example.com", &DNSCacheEntry{
IPs: []string{"192.168.1.1"}, IPs: []string{"192.168.1.1"},
ExpiresAt: time.Now().Add(1 * time.Minute), ExpiresAt: time.Now().Add(1 * time.Minute),
}) })
// 添加已过期的缓存 // 添加已过期的缓存
r.cache.Store("expired.example.com", &dnsCacheEntry{ r.cache.Store("expired.example.com", &DNSCacheEntry{
IPs: []string{"192.168.1.2"}, IPs: []string{"192.168.1.2"},
ExpiresAt: time.Now().Add(-1 * time.Second), ExpiresAt: time.Now().Add(-1 * time.Second),
}) })
@ -194,7 +194,7 @@ func TestCacheHitRate(t *testing.T) {
} }
// 模拟缓存命中 // 模拟缓存命中
r.cache.Store("test.example.com", &dnsCacheEntry{ r.cache.Store("test.example.com", &DNSCacheEntry{
IPs: []string{"192.168.1.1"}, IPs: []string{"192.168.1.1"},
ExpiresAt: time.Now().Add(1 * time.Minute), ExpiresAt: time.Now().Add(1 * time.Minute),
}) })
@ -227,11 +227,11 @@ func TestStats(t *testing.T) {
r := New(cfg).(*DNSResolver) r := New(cfg).(*DNSResolver)
// 添加缓存条目 // 添加缓存条目
r.cache.Store("test1.example.com", &dnsCacheEntry{ r.cache.Store("test1.example.com", &DNSCacheEntry{
IPs: []string{"192.168.1.1"}, IPs: []string{"192.168.1.1"},
ExpiresAt: time.Now().Add(1 * time.Minute), ExpiresAt: time.Now().Add(1 * time.Minute),
}) })
r.cache.Store("test2.example.com", &dnsCacheEntry{ r.cache.Store("test2.example.com", &DNSCacheEntry{
IPs: []string{"192.168.1.2"}, IPs: []string{"192.168.1.2"},
ExpiresAt: time.Now().Add(1 * time.Minute), ExpiresAt: time.Now().Add(1 * time.Minute),
}) })
@ -329,7 +329,7 @@ func TestDeleteCacheEntry(t *testing.T) {
r := New(cfg).(*DNSResolver) r := New(cfg).(*DNSResolver)
// 添加缓存 // 添加缓存
r.cache.Store("test.example.com", &dnsCacheEntry{ r.cache.Store("test.example.com", &DNSCacheEntry{
IPs: []string{"192.168.1.1"}, IPs: []string{"192.168.1.1"},
ExpiresAt: time.Now().Add(1 * time.Minute), ExpiresAt: time.Now().Add(1 * time.Minute),
}) })
@ -358,7 +358,7 @@ func TestClearCache(t *testing.T) {
// 添加多个缓存 // 添加多个缓存
for i := 0; i < 5; i++ { for i := 0; i < 5; i++ {
host := fmt.Sprintf("test%d.example.com", i) host := fmt.Sprintf("test%d.example.com", i)
r.cache.Store(host, &dnsCacheEntry{ r.cache.Store(host, &DNSCacheEntry{
IPs: []string{fmt.Sprintf("192.168.1.%d", i)}, IPs: []string{fmt.Sprintf("192.168.1.%d", i)},
ExpiresAt: time.Now().Add(1 * time.Minute), ExpiresAt: time.Now().Add(1 * time.Minute),
}) })
@ -387,7 +387,7 @@ func TestConcurrentAccess(t *testing.T) {
r := New(cfg).(*DNSResolver) r := New(cfg).(*DNSResolver)
// 添加测试缓存 // 添加测试缓存
r.cache.Store("test.example.com", &dnsCacheEntry{ r.cache.Store("test.example.com", &DNSCacheEntry{
IPs: []string{"192.168.1.1"}, IPs: []string{"192.168.1.1"},
ExpiresAt: time.Now().Add(1 * time.Minute), ExpiresAt: time.Now().Add(1 * time.Minute),
}) })
@ -533,13 +533,13 @@ func TestCacheStats(t *testing.T) {
r := New(cfg).(*DNSResolver) r := New(cfg).(*DNSResolver)
// 添加活跃缓存 // 添加活跃缓存
r.cache.Store("active.example.com", &dnsCacheEntry{ r.cache.Store("active.example.com", &DNSCacheEntry{
IPs: []string{"192.168.1.1"}, IPs: []string{"192.168.1.1"},
ExpiresAt: time.Now().Add(1 * time.Minute), ExpiresAt: time.Now().Add(1 * time.Minute),
}) })
// 添加过期缓存 // 添加过期缓存
r.cache.Store("expired.example.com", &dnsCacheEntry{ r.cache.Store("expired.example.com", &DNSCacheEntry{
IPs: []string{"192.168.1.2"}, IPs: []string{"192.168.1.2"},
ExpiresAt: time.Now().Add(-1 * time.Second), ExpiresAt: time.Now().Add(-1 * time.Second),
}) })

View File

@ -20,7 +20,7 @@ type StatsCollector interface {
// RecordLatency 记录解析延迟 // RecordLatency 记录解析延迟
RecordLatency(latency time.Duration) RecordLatency(latency time.Duration)
// GetStats 获取当前统计 // GetStats 获取当前统计
GetStats() ResolverStats GetStats() Stats
} }
// ResetStats 重置所有统计信息。 // ResetStats 重置所有统计信息。