refactor(resolver): 重命名核心类型移除冗余前缀
ResolverStats → Stats dnsCacheEntry → DNSCacheEntry (导出以符合 Go 命名规范) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
4e535deb80
commit
b28ad378fa
@ -27,9 +27,9 @@ func (r *DNSResolver) GetCacheStats() CacheStats {
|
||||
// 统计缓存条目
|
||||
var entries, expired int
|
||||
now := time.Now()
|
||||
r.cache.Range(func(key, value interface{}) bool {
|
||||
r.cache.Range(func(_ interface{}, value interface{}) bool {
|
||||
entries++
|
||||
entry := value.(*dnsCacheEntry)
|
||||
entry := value.(*DNSCacheEntry)
|
||||
entry.mu.RLock()
|
||||
if now.After(entry.ExpiresAt) {
|
||||
expired++
|
||||
@ -47,9 +47,9 @@ func (r *DNSResolver) GetCacheStats() CacheStats {
|
||||
}
|
||||
|
||||
// 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 {
|
||||
return entry.(*dnsCacheEntry), true
|
||||
return entry.(*DNSCacheEntry), true
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
@ -84,7 +84,7 @@ func (r *DNSResolver) GetHitRate() float64 {
|
||||
// IsCached 检查指定主机是否在缓存中且未过期。
|
||||
func (r *DNSResolver) IsCached(host string) bool {
|
||||
if entry, ok := r.cache.Load(host); ok {
|
||||
cacheEntry := entry.(*dnsCacheEntry)
|
||||
cacheEntry := entry.(*DNSCacheEntry)
|
||||
cacheEntry.mu.RLock()
|
||||
expiresAt := cacheEntry.ExpiresAt
|
||||
cacheEntry.mu.RUnlock()
|
||||
|
||||
@ -44,11 +44,11 @@ type Resolver interface {
|
||||
Stop() error
|
||||
|
||||
// Stats 返回统计信息
|
||||
Stats() ResolverStats
|
||||
Stats() Stats
|
||||
}
|
||||
|
||||
// ResolverStats 解析器统计信息。
|
||||
type ResolverStats struct {
|
||||
// Stats 解析器统计信息。
|
||||
type Stats struct {
|
||||
CacheHits int64 // 缓存命中次数
|
||||
CacheMisses int64 // 缓存未命中次数
|
||||
CacheEntries int // 当前缓存条目数
|
||||
@ -59,7 +59,7 @@ type ResolverStats struct {
|
||||
// DNSResolver 实现 Resolver 接口的 DNS 解析器。
|
||||
type DNSResolver struct {
|
||||
config *config.ResolverConfig
|
||||
cache sync.Map // key: hostname, value: *dnsCacheEntry
|
||||
cache sync.Map // key: hostname, value: *DNSCacheEntry
|
||||
serverIdx atomic.Uint32
|
||||
|
||||
// 统计信息
|
||||
@ -76,8 +76,8 @@ type DNSResolver struct {
|
||||
refreshHosts map[string]struct{} // 需要刷新的主机列表
|
||||
}
|
||||
|
||||
// dnsCacheEntry DNS 缓存条目。
|
||||
type dnsCacheEntry struct {
|
||||
// DNSCacheEntry DNS 缓存条目。
|
||||
type DNSCacheEntry struct {
|
||||
IPs []string
|
||||
ExpiresAt time.Time
|
||||
LastLookup time.Time
|
||||
@ -136,7 +136,7 @@ func (r *DNSResolver) lookup(ctx context.Context, host string, useCache bool) ([
|
||||
// 尝试从缓存获取
|
||||
if useCache {
|
||||
if entry, ok := r.cache.Load(host); ok {
|
||||
cacheEntry := entry.(*dnsCacheEntry)
|
||||
cacheEntry := entry.(*DNSCacheEntry)
|
||||
cacheEntry.mu.RLock()
|
||||
ips := cacheEntry.IPs
|
||||
expiresAt := cacheEntry.ExpiresAt
|
||||
@ -169,7 +169,7 @@ func (r *DNSResolver) lookup(ctx context.Context, host string, useCache bool) ([
|
||||
}
|
||||
|
||||
// 更新缓存
|
||||
entry := &dnsCacheEntry{
|
||||
entry := &DNSCacheEntry{
|
||||
IPs: ips,
|
||||
ExpiresAt: time.Now().Add(r.config.TTL()),
|
||||
LastLookup: time.Now(),
|
||||
@ -218,7 +218,7 @@ func (r *DNSResolver) queryWithResolver(ctx context.Context, host, server string
|
||||
if server != "" {
|
||||
resolver = &net.Resolver{
|
||||
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{}
|
||||
return d.DialContext(ctx, "udp", server)
|
||||
},
|
||||
@ -362,13 +362,13 @@ func (r *DNSResolver) Stop() error {
|
||||
}
|
||||
|
||||
// Stats 返回统计信息。
|
||||
func (r *DNSResolver) Stats() ResolverStats {
|
||||
func (r *DNSResolver) Stats() Stats {
|
||||
hits := r.hits.Load()
|
||||
misses := r.misses.Load()
|
||||
|
||||
// 统计缓存条目数
|
||||
var entries int
|
||||
r.cache.Range(func(key, value interface{}) bool {
|
||||
r.cache.Range(func(_, _ interface{}) bool {
|
||||
entries++
|
||||
return true
|
||||
})
|
||||
@ -380,7 +380,7 @@ func (r *DNSResolver) Stats() ResolverStats {
|
||||
avgLatency = time.Duration(r.latencyNs.Load() / count)
|
||||
}
|
||||
|
||||
return ResolverStats{
|
||||
return Stats{
|
||||
CacheHits: hits,
|
||||
CacheMisses: misses,
|
||||
CacheEntries: entries,
|
||||
@ -392,7 +392,7 @@ func (r *DNSResolver) Stats() ResolverStats {
|
||||
// noopResolver 是禁用状态下的空实现。
|
||||
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")
|
||||
}
|
||||
|
||||
@ -400,7 +400,7 @@ func (n *noopResolver) LookupHostWithCache(ctx context.Context, host string) ([]
|
||||
return n.LookupHost(ctx, host)
|
||||
}
|
||||
|
||||
func (n *noopResolver) Refresh(host string) error {
|
||||
func (n *noopResolver) Refresh(_ string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -412,6 +412,6 @@ func (n *noopResolver) Stop() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (n *noopResolver) Stats() ResolverStats {
|
||||
return ResolverStats{}
|
||||
func (n *noopResolver) Stats() Stats {
|
||||
return Stats{}
|
||||
}
|
||||
|
||||
@ -108,7 +108,7 @@ func TestCache(t *testing.T) {
|
||||
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"},
|
||||
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"},
|
||||
ExpiresAt: time.Now().Add(-1 * time.Second), // 已过期
|
||||
})
|
||||
@ -157,13 +157,13 @@ func TestIsCached(t *testing.T) {
|
||||
r := New(cfg).(*DNSResolver)
|
||||
|
||||
// 添加未过期的缓存
|
||||
r.cache.Store("active.example.com", &dnsCacheEntry{
|
||||
r.cache.Store("active.example.com", &DNSCacheEntry{
|
||||
IPs: []string{"192.168.1.1"},
|
||||
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"},
|
||||
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"},
|
||||
ExpiresAt: time.Now().Add(1 * time.Minute),
|
||||
})
|
||||
@ -227,11 +227,11 @@ func TestStats(t *testing.T) {
|
||||
r := New(cfg).(*DNSResolver)
|
||||
|
||||
// 添加缓存条目
|
||||
r.cache.Store("test1.example.com", &dnsCacheEntry{
|
||||
r.cache.Store("test1.example.com", &DNSCacheEntry{
|
||||
IPs: []string{"192.168.1.1"},
|
||||
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"},
|
||||
ExpiresAt: time.Now().Add(1 * time.Minute),
|
||||
})
|
||||
@ -329,7 +329,7 @@ func TestDeleteCacheEntry(t *testing.T) {
|
||||
r := New(cfg).(*DNSResolver)
|
||||
|
||||
// 添加缓存
|
||||
r.cache.Store("test.example.com", &dnsCacheEntry{
|
||||
r.cache.Store("test.example.com", &DNSCacheEntry{
|
||||
IPs: []string{"192.168.1.1"},
|
||||
ExpiresAt: time.Now().Add(1 * time.Minute),
|
||||
})
|
||||
@ -358,7 +358,7 @@ func TestClearCache(t *testing.T) {
|
||||
// 添加多个缓存
|
||||
for i := 0; i < 5; 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)},
|
||||
ExpiresAt: time.Now().Add(1 * time.Minute),
|
||||
})
|
||||
@ -387,7 +387,7 @@ func TestConcurrentAccess(t *testing.T) {
|
||||
r := New(cfg).(*DNSResolver)
|
||||
|
||||
// 添加测试缓存
|
||||
r.cache.Store("test.example.com", &dnsCacheEntry{
|
||||
r.cache.Store("test.example.com", &DNSCacheEntry{
|
||||
IPs: []string{"192.168.1.1"},
|
||||
ExpiresAt: time.Now().Add(1 * time.Minute),
|
||||
})
|
||||
@ -533,13 +533,13 @@ func TestCacheStats(t *testing.T) {
|
||||
r := New(cfg).(*DNSResolver)
|
||||
|
||||
// 添加活跃缓存
|
||||
r.cache.Store("active.example.com", &dnsCacheEntry{
|
||||
r.cache.Store("active.example.com", &DNSCacheEntry{
|
||||
IPs: []string{"192.168.1.1"},
|
||||
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"},
|
||||
ExpiresAt: time.Now().Add(-1 * time.Second),
|
||||
})
|
||||
|
||||
@ -20,7 +20,7 @@ type StatsCollector interface {
|
||||
// RecordLatency 记录解析延迟
|
||||
RecordLatency(latency time.Duration)
|
||||
// GetStats 获取当前统计
|
||||
GetStats() ResolverStats
|
||||
GetStats() Stats
|
||||
}
|
||||
|
||||
// ResetStats 重置所有统计信息。
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user