@@ -20,6 +20,7 @@ import (
2020 "github.com/AdguardTeam/dnsproxy/internal/dnsmsg"
2121 proxynetutil "github.com/AdguardTeam/dnsproxy/internal/netutil"
2222 "github.com/AdguardTeam/dnsproxy/upstream"
23+ "github.com/AdguardTeam/golibs/contextutil"
2324 "github.com/AdguardTeam/golibs/errors"
2425 "github.com/AdguardTeam/golibs/logutil/slogutil"
2526 "github.com/AdguardTeam/golibs/netutil"
@@ -112,6 +113,9 @@ type Proxy struct {
112113 // requests for private addresses.
113114 recDetector * recursionDetector
114115
116+ // reqCtx is a constructor for the request contexts. It is never nil.
117+ reqCtx contextutil.Constructor
118+
115119 // pendingRequests is a storage for duplicated requests. It is used to
116120 // prevent sending the same request to upstreams multiple times.
117121 pendingRequests pendingRequests
@@ -215,6 +219,10 @@ func New(c *Config) (p *Proxy, err error) {
215219 c .PrivateSubnets ,
216220 netutil .SubnetSetFunc (netutil .IsLocallyServed ),
217221 ),
222+ reqCtx : cmp .Or [contextutil.Constructor ](
223+ c .RequestContext ,
224+ contextutil.EmptyConstructor {},
225+ ),
218226 requestHandler : cmp .Or [Handler ](c .RequestHandler , DefaultHandler {}),
219227 upstreamRTTStats : map [string ]upstreamRTTStats {},
220228 rttLock : sync.Mutex {},
@@ -338,7 +346,9 @@ func (p *Proxy) Start(ctx context.Context) (err error) {
338346 return fmt .Errorf ("configuring listeners: %w" , errors .WithDeferred (err , closeErr ))
339347 }
340348
341- p .serveListeners ()
349+ // Use context without cancel to prevent listeners' context from being
350+ // canceled.
351+ p .serveListeners (context .WithoutCancel (ctx ))
342352
343353 p .started = true
344354
@@ -675,12 +685,8 @@ func addDO(msg *dns.Msg) {
675685const defaultUDPBufSize = 2048
676686
677687// Resolve is the default resolving method used by the DNS proxy to query
678- // upstream servers. It expects dctx is filled with the request, the client's
679- //
680- // TODO(e.burkov): Add [context.Context].
681- func (p * Proxy ) Resolve (dctx * DNSContext ) (err error ) {
682- ctx := context .Background ()
683-
688+ // upstream servers. It expects dctx is filled with the client's request.
689+ func (p * Proxy ) Resolve (ctx context.Context , dctx * DNSContext ) (err error ) {
684690 if p .EnableEDNSClientSubnet {
685691 dctx .processECS (p .EDNSAddr , p .logger )
686692 }
0 commit comments