Pull request 1938: AG-24132-rdns-ttl
Squashed commit of the following:
commit ba1e7b12cf7c0dc3ffab508d59c149f6c0930548
Merge: 8a94433ec ed86af582
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date: Tue Jul 25 13:43:25 2023 +0300
Merge branch 'master' into AG-24132-rdns-ttl
commit 8a94433ec119d2158c166dd0222f57917908f3ad
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date: Mon Jul 24 19:30:21 2023 +0300
all: imp docs
commit 4c1a3676b7be7ac4295c4e28550ddb6eb79a35d4
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date: Mon Jul 24 13:13:34 2023 +0300
all: add rdns ttl
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
|
||||
"github.com/AdguardTeam/golibs/errors"
|
||||
"github.com/AdguardTeam/golibs/log"
|
||||
"github.com/AdguardTeam/golibs/mathutil"
|
||||
"github.com/bluele/gcache"
|
||||
)
|
||||
|
||||
@@ -32,7 +33,7 @@ func (Empty) Process(_ netip.Addr) (host string, changed bool) {
|
||||
type Exchanger interface {
|
||||
// Exchange tries to resolve the ip in a suitable way, i.e. either as local
|
||||
// or as external.
|
||||
Exchange(ip netip.Addr) (host string, err error)
|
||||
Exchange(ip netip.Addr) (host string, ttl time.Duration, err error)
|
||||
}
|
||||
|
||||
// Config is the configuration structure for Default.
|
||||
@@ -82,13 +83,16 @@ func (r *Default) Process(ip netip.Addr) (host string, changed bool) {
|
||||
return fromCache, false
|
||||
}
|
||||
|
||||
host, err := r.exchanger.Exchange(ip)
|
||||
host, ttl, err := r.exchanger.Exchange(ip)
|
||||
if err != nil {
|
||||
log.Debug("rdns: resolving %q: %s", ip, err)
|
||||
}
|
||||
|
||||
// TODO(s.chzhen): Use built-in function max in Go 1.21.
|
||||
ttl = mathutil.Max(ttl, r.cacheTTL)
|
||||
|
||||
item := &cacheItem{
|
||||
expiry: time.Now().Add(r.cacheTTL),
|
||||
expiry: time.Now().Add(ttl),
|
||||
host: host,
|
||||
}
|
||||
|
||||
|
||||
@@ -55,18 +55,18 @@ func TestDefault_Process(t *testing.T) {
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
hit := 0
|
||||
onExchange := func(ip netip.Addr) (host string, err error) {
|
||||
onExchange := func(ip netip.Addr) (host string, ttl time.Duration, err error) {
|
||||
hit++
|
||||
|
||||
switch ip {
|
||||
case ip1:
|
||||
return revAddr1, nil
|
||||
return revAddr1, 0, nil
|
||||
case ip2:
|
||||
return revAddr2, nil
|
||||
return revAddr2, 0, nil
|
||||
case localIP:
|
||||
return localRevAddr1, nil
|
||||
return localRevAddr1, 0, nil
|
||||
default:
|
||||
return "", nil
|
||||
return "", 0, nil
|
||||
}
|
||||
}
|
||||
exchanger := &aghtest.Exchanger{
|
||||
|
||||
Reference in New Issue
Block a user