Skip to content

Commit 8e220ae

Browse files
committed
all: add pending requests
1 parent d64332f commit 8e220ae

18 files changed

+566
-232
lines changed

internal/dnsproxytest/interface.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"github.com/miekg/dns"
55
)
66

7-
// FakeUpstream is a fake [Upstream] implementation for tests.
7+
// FakeUpstream is a fake [proxy.Upstream] implementation for tests.
88
//
99
// TODO(e.burkov): Move this to the golibs some time later.
1010
type FakeUpstream struct {
@@ -13,22 +13,22 @@ type FakeUpstream struct {
1313
OnClose func() (err error)
1414
}
1515

16-
// Address implements the [Upstream] interface for *FakeUpstream.
16+
// Address implements the [proxy.Upstream] interface for *FakeUpstream.
1717
func (u *FakeUpstream) Address() (addr string) {
1818
return u.OnAddress()
1919
}
2020

21-
// Exchange implements the [Upstream] interface for *FakeUpstream.
21+
// Exchange implements the [proxy.Upstream] interface for *FakeUpstream.
2222
func (u *FakeUpstream) Exchange(req *dns.Msg) (resp *dns.Msg, err error) {
2323
return u.OnExchange(req)
2424
}
2525

26-
// Close implements the [Upstream] interface for *FakeUpstream.
26+
// Close implements the [proxy.Upstream] interface for *FakeUpstream.
2727
func (u *FakeUpstream) Close() (err error) {
2828
return u.OnClose()
2929
}
3030

31-
// TestMessageConstructor is a fake [dnsmsg.MessageConstructor] implementation
31+
// TestMessageConstructor is a fake [proxy.MessageConstructor] implementation
3232
// for tests.
3333
type TestMessageConstructor struct {
3434
OnNewMsgNXDOMAIN func(req *dns.Msg) (resp *dns.Msg)
@@ -56,19 +56,19 @@ func NewTestMessageConstructor() (c *TestMessageConstructor) {
5656
}
5757
}
5858

59-
// NewMsgNXDOMAIN implements the [MessageConstructor] interface for
59+
// NewMsgNXDOMAIN implements the [proxy.MessageConstructor] interface for
6060
// *TestMessageConstructor.
6161
func (c *TestMessageConstructor) NewMsgNXDOMAIN(req *dns.Msg) (resp *dns.Msg) {
6262
return c.OnNewMsgNXDOMAIN(req)
6363
}
6464

65-
// NewMsgSERVFAIL implements the [MessageConstructor] interface for
65+
// NewMsgSERVFAIL implements the [proxy.MessageConstructor] interface for
6666
// *TestMessageConstructor.
6767
func (c *TestMessageConstructor) NewMsgSERVFAIL(req *dns.Msg) (resp *dns.Msg) {
6868
return c.OnNewMsgSERVFAIL(req)
6969
}
7070

71-
// NewMsgNOTIMPLEMENTED implements the [MessageConstructor] interface for
71+
// NewMsgNOTIMPLEMENTED implements the [proxy.MessageConstructor] interface for
7272
// *TestMessageConstructor.
7373
func (c *TestMessageConstructor) NewMsgNOTIMPLEMENTED(req *dns.Msg) (resp *dns.Msg) {
7474
return c.OnNewMsgNOTIMPLEMENTED(req)

proxy/beforerequest_test.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"testing"
88
"time"
99

10+
"github.com/AdguardTeam/dnsproxy/internal/dnsproxytest"
1011
"github.com/AdguardTeam/dnsproxy/upstream"
1112
"github.com/AdguardTeam/golibs/errors"
1213
"github.com/AdguardTeam/golibs/logutil/slogutil"
@@ -52,19 +53,19 @@ func TestProxy_HandleDNSRequest_beforeRequestHandler(t *testing.T) {
5253
errorRequest.Id = errorID
5354
errorResponse := (&dns.Msg{}).SetReply(errorRequest)
5455

55-
p := mustNew(t, &Config{
56+
p := MustNew(t, &Config{
5657
Logger: slogutil.NewDiscardLogger(),
57-
TCPListenAddr: []*net.TCPAddr{net.TCPAddrFromAddrPort(localhostAnyPort)},
58+
TCPListenAddr: []*net.TCPAddr{net.TCPAddrFromAddrPort(LocalhostAnyPort)},
5859
UpstreamConfig: &UpstreamConfig{
59-
Upstreams: []upstream.Upstream{&fakeUpstream{
60-
onExchange: func(m *dns.Msg) (resp *dns.Msg, err error) {
60+
Upstreams: []upstream.Upstream{&dnsproxytest.FakeUpstream{
61+
OnExchange: func(m *dns.Msg) (resp *dns.Msg, err error) {
6162
return allowedResponse.Copy(), nil
6263
},
63-
onAddress: func() (addr string) { return "general" },
64-
onClose: func() (err error) { return nil },
64+
OnAddress: func() (addr string) { return "general" },
65+
OnClose: func() (err error) { return nil },
6566
}},
6667
},
67-
TrustedProxies: defaultTrustedProxies,
68+
TrustedProxies: DefaultTrustedProxies,
6869
PrivateSubnets: netutil.SubnetSetFunc(netutil.IsLocallyServed),
6970
BeforeRequestHandler: &testBeforeRequestHandler{
7071
onHandleBefore: func(p *Proxy, dctx *DNSContext) (err error) {

proxy/bogusnxdomain_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ import (
1515
)
1616

1717
func TestProxy_IsBogusNXDomain(t *testing.T) {
18-
prx := mustNew(t, &Config{
18+
prx := MustNew(t, &Config{
1919
Logger: slogutil.NewDiscardLogger(),
20-
UDPListenAddr: []*net.UDPAddr{net.UDPAddrFromAddrPort(localhostAnyPort)},
21-
TCPListenAddr: []*net.TCPAddr{net.TCPAddrFromAddrPort(localhostAnyPort)},
20+
UDPListenAddr: []*net.UDPAddr{net.UDPAddrFromAddrPort(LocalhostAnyPort)},
21+
TCPListenAddr: []*net.TCPAddr{net.TCPAddrFromAddrPort(LocalhostAnyPort)},
2222
UpstreamConfig: newTestUpstreamConfig(t, defaultTimeout, testDefaultUpstreamAddr),
23-
TrustedProxies: defaultTrustedProxies,
23+
TrustedProxies: DefaultTrustedProxies,
2424
RatelimitSubnetLenIPv4: 24,
2525
RatelimitSubnetLenIPv6: 64,
2626
CacheEnabled: true,

proxy/cache_test.go

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"testing"
1010
"time"
1111

12+
"github.com/AdguardTeam/dnsproxy/internal/dnsproxytest"
1213
"github.com/AdguardTeam/dnsproxy/upstream"
1314
"github.com/AdguardTeam/golibs/logutil/slogutil"
1415
"github.com/AdguardTeam/golibs/netutil"
@@ -24,19 +25,19 @@ const testCacheSize = 4096
2425

2526
const testUpsAddr = "https://upstream.address"
2627

27-
var upstreamWithAddr = &fakeUpstream{
28-
onExchange: func(m *dns.Msg) (resp *dns.Msg, err error) { panic("not implemented") },
29-
onClose: func() (err error) { panic("not implemented") },
30-
onAddress: func() (addr string) { return testUpsAddr },
28+
var upstreamWithAddr = &dnsproxytest.FakeUpstream{
29+
OnExchange: func(m *dns.Msg) (resp *dns.Msg, err error) { panic("not implemented") },
30+
OnClose: func() (err error) { panic("not implemented") },
31+
OnAddress: func() (addr string) { return testUpsAddr },
3132
}
3233

3334
func TestServeCached(t *testing.T) {
34-
dnsProxy := mustNew(t, &Config{
35+
dnsProxy := MustNew(t, &Config{
3536
Logger: slogutil.NewDiscardLogger(),
36-
UDPListenAddr: []*net.UDPAddr{net.UDPAddrFromAddrPort(localhostAnyPort)},
37-
TCPListenAddr: []*net.TCPAddr{net.TCPAddrFromAddrPort(localhostAnyPort)},
37+
UDPListenAddr: []*net.UDPAddr{net.UDPAddrFromAddrPort(LocalhostAnyPort)},
38+
TCPListenAddr: []*net.TCPAddr{net.TCPAddrFromAddrPort(LocalhostAnyPort)},
3839
UpstreamConfig: newTestUpstreamConfig(t, defaultTimeout, testDefaultUpstreamAddr),
39-
TrustedProxies: defaultTrustedProxies,
40+
TrustedProxies: DefaultTrustedProxies,
4041
RatelimitSubnetLenIPv4: 24,
4142
RatelimitSubnetLenIPv6: 64,
4243
CacheEnabled: true,
@@ -288,12 +289,12 @@ const (
288289
func TestCacheExpiration(t *testing.T) {
289290
t.Parallel()
290291

291-
dnsProxy := mustNew(t, &Config{
292+
dnsProxy := MustNew(t, &Config{
292293
Logger: slogutil.NewDiscardLogger(),
293-
UDPListenAddr: []*net.UDPAddr{net.UDPAddrFromAddrPort(localhostAnyPort)},
294-
TCPListenAddr: []*net.TCPAddr{net.TCPAddrFromAddrPort(localhostAnyPort)},
294+
UDPListenAddr: []*net.UDPAddr{net.UDPAddrFromAddrPort(LocalhostAnyPort)},
295+
TCPListenAddr: []*net.TCPAddr{net.TCPAddrFromAddrPort(LocalhostAnyPort)},
295296
UpstreamConfig: newTestUpstreamConfig(t, defaultTimeout, testDefaultUpstreamAddr),
296-
TrustedProxies: defaultTrustedProxies,
297+
TrustedProxies: DefaultTrustedProxies,
297298
RatelimitSubnetLenIPv4: 24,
298299
RatelimitSubnetLenIPv6: 64,
299300
CacheEnabled: true,
@@ -348,14 +349,14 @@ func TestCacheExpiration(t *testing.T) {
348349
func TestCacheExpirationWithTTLOverride(t *testing.T) {
349350
u := testUpstream{}
350351

351-
dnsProxy := mustNew(t, &Config{
352+
dnsProxy := MustNew(t, &Config{
352353
Logger: slogutil.NewDiscardLogger(),
353-
UDPListenAddr: []*net.UDPAddr{net.UDPAddrFromAddrPort(localhostAnyPort)},
354-
TCPListenAddr: []*net.TCPAddr{net.TCPAddrFromAddrPort(localhostAnyPort)},
354+
UDPListenAddr: []*net.UDPAddr{net.UDPAddrFromAddrPort(LocalhostAnyPort)},
355+
TCPListenAddr: []*net.TCPAddr{net.TCPAddrFromAddrPort(LocalhostAnyPort)},
355356
UpstreamConfig: &UpstreamConfig{
356357
Upstreams: []upstream.Upstream{&u},
357358
},
358-
TrustedProxies: defaultTrustedProxies,
359+
TrustedProxies: DefaultTrustedProxies,
359360
RatelimitSubnetLenIPv4: 24,
360361
RatelimitSubnetLenIPv6: 64,
361362
CacheEnabled: true,

proxy/config.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ type Config struct {
5858
// constructor will be used.
5959
MessageConstructor MessageConstructor
6060

61+
// PendingRequests limits the number of identical requests sent to the
62+
// upstream server. If nil, the default implementation will be used. See
63+
// [PendingRequests] for all available implementations.
64+
PendingRequests PendingRequests
65+
6166
// BeforeRequestHandler is an optional custom handler called before each DNS
6267
// request is started processing, see [BeforeRequestHandler]. The default
6368
// no-op implementation is used, if it's nil.

proxy/dns64_test.go

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"sync"
88
"testing"
99

10+
"github.com/AdguardTeam/dnsproxy/internal/dnsproxytest"
1011
"github.com/AdguardTeam/dnsproxy/upstream"
1112
"github.com/AdguardTeam/golibs/logutil/slogutil"
1213
"github.com/AdguardTeam/golibs/netutil"
@@ -20,36 +21,36 @@ const ipv4OnlyFqdn = "ipv4.only."
2021

2122
func TestDNS64Race(t *testing.T) {
2223
ans := newRR(t, ipv4OnlyFqdn, dns.TypeA, 3600, net.ParseIP("1.2.3.4"))
23-
ups := &fakeUpstream{
24-
onExchange: func(req *dns.Msg) (resp *dns.Msg, err error) {
24+
ups := &dnsproxytest.FakeUpstream{
25+
OnExchange: func(req *dns.Msg) (resp *dns.Msg, err error) {
2526
resp = (&dns.Msg{}).SetReply(req)
2627
if req.Question[0].Qtype == dns.TypeA {
2728
resp.Answer = []dns.RR{dns.Copy(ans)}
2829
}
2930

3031
return resp, nil
3132
},
32-
onAddress: func() (addr string) { return "fake.address" },
33-
onClose: func() (err error) { return nil },
33+
OnAddress: func() (addr string) { return "fake.address" },
34+
OnClose: func() (err error) { return nil },
3435
}
35-
localUps := &fakeUpstream{
36-
onExchange: func(_ *dns.Msg) (_ *dns.Msg, _ error) { panic("not implemented") },
37-
onAddress: func() (addr string) { return "fake.address" },
38-
onClose: func() (err error) { return nil },
36+
localUps := &dnsproxytest.FakeUpstream{
37+
OnExchange: func(_ *dns.Msg) (_ *dns.Msg, _ error) { panic("not implemented") },
38+
OnAddress: func() (addr string) { return "fake.address" },
39+
OnClose: func() (err error) { return nil },
3940
}
4041

41-
dnsProxy := mustNew(t, &Config{
42+
dnsProxy := MustNew(t, &Config{
4243
Logger: slogutil.NewDiscardLogger(),
43-
UDPListenAddr: []*net.UDPAddr{net.UDPAddrFromAddrPort(localhostAnyPort)},
44-
TCPListenAddr: []*net.TCPAddr{net.TCPAddrFromAddrPort(localhostAnyPort)},
44+
UDPListenAddr: []*net.UDPAddr{net.UDPAddrFromAddrPort(LocalhostAnyPort)},
45+
TCPListenAddr: []*net.TCPAddr{net.TCPAddrFromAddrPort(LocalhostAnyPort)},
4546
PrivateSubnets: netutil.SubnetSetFunc(netutil.IsLocallyServed),
4647
UpstreamConfig: &UpstreamConfig{
4748
Upstreams: []upstream.Upstream{ups},
4849
},
4950
PrivateRDNSUpstreamConfig: &UpstreamConfig{
5051
Upstreams: []upstream.Upstream{localUps},
5152
},
52-
TrustedProxies: defaultTrustedProxies,
53+
TrustedProxies: DefaultTrustedProxies,
5354
RatelimitSubnetLenIPv4: 24,
5455
RatelimitSubnetLenIPv6: 64,
5556

@@ -182,8 +183,8 @@ func TestProxy_Resolve_dns64(t *testing.T) {
182183

183184
pt := testutil.PanicT{}
184185
newUps := func(answers answerMap) (u upstream.Upstream) {
185-
return &fakeUpstream{
186-
onExchange: func(req *dns.Msg) (resp *dns.Msg, err error) {
186+
return &dnsproxytest.FakeUpstream{
187+
OnExchange: func(req *dns.Msg) (resp *dns.Msg, err error) {
187188
q := req.Question[0]
188189
require.Contains(pt, answers, q.Qtype)
189190

@@ -196,22 +197,22 @@ func TestProxy_Resolve_dns64(t *testing.T) {
196197

197198
return resp, nil
198199
},
199-
onAddress: func() (addr string) { return "fake.address" },
200-
onClose: func() (err error) { return nil },
200+
OnAddress: func() (addr string) { return "fake.address" },
201+
OnClose: func() (err error) { return nil },
201202
}
202203
}
203204

204205
localRR := newRR(t, ptr64Domain, dns.TypePTR, 3600, pointedDomain)
205-
localUps := &fakeUpstream{
206-
onExchange: func(req *dns.Msg) (resp *dns.Msg, err error) {
206+
localUps := &dnsproxytest.FakeUpstream{
207+
OnExchange: func(req *dns.Msg) (resp *dns.Msg, err error) {
207208
require.Equal(pt, req.Question[0].Name, ptr64Domain)
208209
resp = (&dns.Msg{}).SetReply(req)
209210
resp.Answer = []dns.RR{localRR}
210211

211212
return resp, nil
212213
},
213-
onAddress: func() (addr string) { return "fake.local.address" },
214-
onClose: func() (err error) { return nil },
214+
OnAddress: func() (addr string) { return "fake.local.address" },
215+
OnClose: func() (err error) { return nil },
215216
}
216217

217218
testCases := []struct {
@@ -356,17 +357,17 @@ func TestProxy_Resolve_dns64(t *testing.T) {
356357

357358
for _, tc := range testCases {
358359
t.Run(tc.name, func(t *testing.T) {
359-
p := mustNew(t, &Config{
360+
p := MustNew(t, &Config{
360361
Logger: slogutil.NewDiscardLogger(),
361-
UDPListenAddr: []*net.UDPAddr{net.UDPAddrFromAddrPort(localhostAnyPort)},
362-
TCPListenAddr: []*net.TCPAddr{net.TCPAddrFromAddrPort(localhostAnyPort)},
362+
UDPListenAddr: []*net.UDPAddr{net.UDPAddrFromAddrPort(LocalhostAnyPort)},
363+
TCPListenAddr: []*net.TCPAddr{net.TCPAddrFromAddrPort(LocalhostAnyPort)},
363364
UpstreamConfig: &UpstreamConfig{
364365
Upstreams: []upstream.Upstream{newUps(tc.upsAns)},
365366
},
366367
PrivateRDNSUpstreamConfig: &UpstreamConfig{
367368
Upstreams: []upstream.Upstream{localUps},
368369
},
369-
TrustedProxies: defaultTrustedProxies,
370+
TrustedProxies: DefaultTrustedProxies,
370371
RatelimitSubnetLenIPv4: 24,
371372
RatelimitSubnetLenIPv6: 64,
372373
CacheEnabled: true,

0 commit comments

Comments
 (0)