Skip to content

Commit

Permalink
Unify upstream method
Browse files Browse the repository at this point in the history
  • Loading branch information
lixmal committed Jan 29, 2024
1 parent 631fa43 commit 5a37bf3
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 14 deletions.
8 changes: 7 additions & 1 deletion client/internal/dns/upstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ const (
const testRecord = "."

type upstreamClient interface {
exchange(upstream string, r *dns.Msg) (*dns.Msg, time.Duration, error)
exchangeContext(ctx context.Context, upstream string, r *dns.Msg) (*dns.Msg, time.Duration, error)
}

Expand Down Expand Up @@ -253,6 +252,13 @@ func (u *upstreamResolverBase) disable() {
}
}

func (u *upstreamResolverBase) exchange(upstream string, r *dns.Msg) (rm *dns.Msg, t time.Duration, err error) {
// default upstream timeout
ctx, cancel := context.WithTimeout(context.Background(), u.upstreamTimeout)
defer cancel()
return u.upstreamClient.exchangeContext(ctx, upstream, r)
}

func (u *upstreamResolverBase) testNameserver(server string) error {
ctx, cancel := context.WithTimeout(u.ctx, probeTimeout)
defer cancel()
Expand Down
6 changes: 0 additions & 6 deletions client/internal/dns/upstream_ios.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,6 @@ func newUpstreamResolver(parentCTX context.Context, interfaceName string, ip net
return ios, nil
}

func (u *upstreamResolverIOS) exchange(upstream string, r *dns.Msg) (rm *dns.Msg, t time.Duration, err error) {
ctx, cancel := context.WithTimeout(context.Background(), u.upstreamTimeout)
defer cancel()
return u.exchangeContext(ctx, upstream, r)
}

func (u *upstreamResolverIOS) exchangeContext(ctx context.Context, upstream string, r *dns.Msg) (rm *dns.Msg, t time.Duration, err error) {
client := &dns.Client{}
upstreamHost, _, err := net.SplitHostPort(upstream)
Expand Down
7 changes: 0 additions & 7 deletions client/internal/dns/upstream_nonios.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,6 @@ func newUpstreamResolver(parentCTX context.Context, interfaceName string, ip net
return nonIOS, nil
}

func (u *upstreamResolverNonIOS) exchange(upstream string, r *dns.Msg) (rm *dns.Msg, t time.Duration, err error) {
// default upstream timeout
ctx, cancel := context.WithTimeout(u.ctx, u.upstreamTimeout)
defer cancel()
return u.exchangeContext(ctx, upstream, r)
}

func (u *upstreamResolverNonIOS) exchangeContext(ctx context.Context, upstream string, r *dns.Msg) (rm *dns.Msg, t time.Duration, err error) {
upstreamExchangeClient := &dns.Client{}
return upstreamExchangeClient.ExchangeContext(ctx, r, upstream)
Expand Down

0 comments on commit 5a37bf3

Please sign in to comment.