代码拉取完成,页面将自动刷新
package dnsr
import "sync"
type cache struct {
capacity int
m sync.RWMutex
entries map[string]entry
}
type entry map[RR]struct{}
const MinCacheCapacity = 1000
// newCache initializes and returns a new cache instance.
// Cache capacity defaults to MinCacheCapacity if <= 0.
func newCache(capacity int) *cache {
if capacity <= 0 {
capacity = MinCacheCapacity
}
return &cache{
capacity: capacity,
entries: make(map[string]entry),
}
}
// add adds 0 or more DNS records to the resolver cache for a specific
// domain name and record type. This ensures the cache entry exists, even
// if empty, for NXDOMAIN responses.
func (c *cache) add(qname string, rr RR) {
c.m.Lock()
c._add(qname, rr)
c.m.Unlock()
}
// addNX adds an NXDOMAIN to the cache.
// Safe for concurrent usage.
func (c *cache) addNX(qname string) {
c.m.Lock()
c._addEntry(qname)
c.m.Unlock()
}
// _add does NOT lock the mutex so unsafe for concurrent usage.
func (c *cache) _add(qname string, rr RR) {
e, ok := c.entries[qname]
if !ok {
c._evict()
}
if e == nil {
c.entries[qname] = make(map[RR]struct{})
e = c.entries[qname]
}
e[rr] = struct{}{}
}
// addEntry adds an entry for qname to c.
// Not safe for concurrent usage.
func (c *cache) _addEntry(qname string) {
_, ok := c.entries[qname]
if !ok {
c._evict()
// For NXDOMAIN responses,
// the cache entry is present, but nil.
c.entries[qname] = nil
}
}
// FIXME: better random cache eviction than Go’s random key guarantee?
// Not safe for concurrent usage.
func (c *cache) _evict() {
if len(c.entries) < c.capacity {
return
}
for k := range c.entries {
delete(c.entries, k)
if len(c.entries) < c.capacity {
return
}
}
}
// get returns a randomly ordered slice of DNS records.
func (c *cache) get(qname string) RRs {
c.m.RLock()
defer c.m.RUnlock()
e, ok := c.entries[qname]
if !ok {
return nil
}
if len(e) == 0 {
return emptyRRs
}
i := 0
rrs := make(RRs, len(e))
for rr := range e {
rrs[i] = rr
i++
}
return rrs
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。