From 58e16661fe975942e0e07a2aa86637597cda66a0 Mon Sep 17 00:00:00 2001 From: Vladislav Yarmak Date: Thu, 1 Aug 2024 18:22:11 +0300 Subject: [PATCH] close resolvers properly --- go.mod | 2 ++ go.sum | 4 ++++ main.go | 4 ++++ resolver.go | 14 ++++++++++++++ 4 files changed, 24 insertions(+) diff --git a/go.mod b/go.mod index 066adbf..c0549c2 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.22.5 require ( github.com/AdguardTeam/dnsproxy v0.72.2 github.com/Snawoot/go-http-digest-auth-client v1.1.3 + github.com/hashicorp/go-multierror v1.1.1 golang.org/x/net v0.27.0 ) @@ -16,6 +17,7 @@ require ( github.com/ameshkov/dnsstamps v1.0.3 // indirect github.com/go-task/slim-sprig/v3 v3.0.0 // indirect github.com/google/pprof v0.0.0-20240727154555-813a5fbdbec8 // indirect + github.com/hashicorp/errwrap v1.0.0 // indirect github.com/miekg/dns v1.1.61 // indirect github.com/onsi/ginkgo/v2 v2.19.1 // indirect github.com/quic-go/qpack v0.4.0 // indirect diff --git a/go.sum b/go.sum index 8346795..5376bd0 100644 --- a/go.sum +++ b/go.sum @@ -22,6 +22,10 @@ github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/pprof v0.0.0-20240727154555-813a5fbdbec8 h1:FKHo8hFI3A+7w0aUQuYXQ+6EN5stWmeY/AZqtM8xk9k= github.com/google/pprof v0.0.0-20240727154555-813a5fbdbec8/go.mod h1:K1liHPHnj73Fdn/EKuT8nrFqBihUSKXoLYU0BuatOYo= +github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA= +github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= +github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo= +github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM= github.com/miekg/dns v1.1.61 h1:nLxbwF3XxhwVSm8g9Dghm9MHPaUZuqhPiGL+675ZmEs= github.com/miekg/dns v1.1.61/go.mod h1:mnAarhS3nWaW+NVP2wTkYVIZyHNJ098SJZUki3eykwQ= github.com/onsi/ginkgo/v2 v2.19.1 h1:QXgq3Z8Crl5EL1WBAC98A5sEBHARrAJNzAmMxzLcRF0= diff --git a/main.go b/main.go index d4c3565..b322d2f 100644 --- a/main.go +++ b/main.go @@ -210,6 +210,10 @@ func run() int { addrs, err := func() ([]netip.Addr, error) { ctx, cancel := context.WithTimeout(context.Background(), args.timeout) defer cancel() + defer func() { + resolver = nil + }() + defer resolver.Close() return resolver.LookupNetIP(ctx, "ip4", API_DOMAIN) }() if err != nil { diff --git a/resolver.go b/resolver.go index f460e4e..76ab612 100644 --- a/resolver.go +++ b/resolver.go @@ -3,10 +3,12 @@ package main import ( "context" "fmt" + "io" "net/netip" "time" "github.com/AdguardTeam/dnsproxy/upstream" + "github.com/hashicorp/go-multierror" ) type Resolver struct { @@ -36,3 +38,15 @@ func NewResolver(addresses []string, timeout time.Duration) (*Resolver, error) { func (r *Resolver) LookupNetIP(ctx context.Context, network string, host string) (addrs []netip.Addr, err error) { return r.resolvers.LookupNetIP(ctx, network, host) } + +func (r *Resolver) Close() error { + var res error + for _, resolver := range r.resolvers { + if closer, ok := resolver.(io.Closer); ok { + if err := closer.Close(); err != nil { + res = multierror.Append(res, err) + } + } + } + return res +}