Skip to content

Commit

Permalink
Merge pull request #59 from Snawoot/fix_resolver_close
Browse files Browse the repository at this point in the history
Close resolvers properly
  • Loading branch information
Snawoot authored Aug 1, 2024
2 parents 2e3cf20 + 58e1666 commit 50bb844
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 0 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
4 changes: 4 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
14 changes: 14 additions & 0 deletions resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

0 comments on commit 50bb844

Please sign in to comment.