Skip to content

Commit

Permalink
chore(test): fix race conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
0xERR0R committed Sep 18, 2023
1 parent 9f15228 commit 500c187
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
6 changes: 5 additions & 1 deletion resolver/blocking_resolver.go
Expand Up @@ -145,10 +145,14 @@ func NewBlockingResolver(
setupRedisEnabledSubscriber(res)
}

_ = evt.Bus().Subscribe(evt.ApplicationStarted, func(_ ...string) {
err = evt.Bus().SubscribeOnce(evt.ApplicationStarted, func(_ ...string) {
go res.initFQDNIPCache()
})

if err != nil {
return nil, err
}

return res, nil
}

Expand Down
12 changes: 8 additions & 4 deletions resolver/caching_resolver.go
Expand Up @@ -88,7 +88,8 @@ func setupRedisCacheSubscriber(c *CachingResolver) {
for rc := range c.redisClient.CacheChannel {
if rc != nil {
c.log().Debug("Received key from redis: ", rc.Key)
c.putInCache(rc.Key, rc.Response, false, false)
ttl := c.adjustTTLs(rc.Response.Res.Answer)
c.putInCache(rc.Key, rc.Response, ttl, false, false)
}
}
}()
Expand Down Expand Up @@ -189,7 +190,8 @@ func (r *CachingResolver) Resolve(request *model.Request) (response *model.Respo
response, err = r.next.Resolve(request)

if err == nil {
r.putInCache(cacheKey, response, false, true)
cacheTTL := r.adjustTTLs(response.Res.Answer)
r.putInCache(cacheKey, response, cacheTTL, false, true)
}
}

Expand Down Expand Up @@ -227,15 +229,17 @@ func removeEdns0Extra(msg *dns.Msg) {
}
}

func (r *CachingResolver) putInCache(cacheKey string, response *model.Response, prefetch, publish bool) {
func (r *CachingResolver) putInCache(cacheKey string, response *model.Response, ttl time.Duration,
prefetch, publish bool,
) {
respCopy := response.Res.Copy()

// don't cache any EDNS OPT records
removeEdns0Extra(respCopy)

if response.Res.Rcode == dns.RcodeSuccess && !response.Res.Truncated {
// put value into cache
r.resultCache.Put(cacheKey, &cacheValue{respCopy, prefetch}, r.adjustTTLs(response.Res.Answer))
r.resultCache.Put(cacheKey, &cacheValue{respCopy, prefetch}, ttl)
} else if response.Res.Rcode == dns.RcodeNameError {
if r.cfg.CacheTimeNegative.IsAboveZero() {
// put negative cache if result code is NXDOMAIN
Expand Down

0 comments on commit 500c187

Please sign in to comment.