Skip to content

Commit

Permalink
change: remove Picker injected settings field
Browse files Browse the repository at this point in the history
- More future compatibility
- Availability for Picker change and new methods
  • Loading branch information
qdm12 committed Nov 26, 2023
1 parent 95ae9e8 commit 65ca23e
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 97 deletions.
3 changes: 2 additions & 1 deletion pkg/doh/dial.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net"
"sync"

"github.com/qdm12/dns/v2/internal/picker"
"github.com/qdm12/dns/v2/internal/server"
"github.com/qdm12/dns/v2/pkg/provider"
)
Expand All @@ -28,7 +29,7 @@ func newDoHDial(settings ResolverSettings) (dial server.Dial) {
},
}

picker := settings.Picker
picker := picker.New()

return func(ctx context.Context, _, _ string) (conn net.Conn, err error) {
// Pick DoH server pseudo-randomly from the chosen providers
Expand Down
5 changes: 0 additions & 5 deletions pkg/doh/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package doh

import (
"github.com/miekg/dns"
"github.com/qdm12/dns/v2/pkg/provider"
)

type Middleware interface {
Expand All @@ -27,7 +26,3 @@ type Logger interface {
type Warner interface {
Warn(s string)
}

type Picker interface {
DoHServer(servers []provider.DoHServer) provider.DoHServer
}
7 changes: 0 additions & 7 deletions pkg/doh/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"os"
"time"

"github.com/qdm12/dns/v2/internal/picker"
metricsnoop "github.com/qdm12/dns/v2/pkg/doh/metrics/noop"
lognoop "github.com/qdm12/dns/v2/pkg/log/noop"
"github.com/qdm12/dns/v2/pkg/provider"
Expand Down Expand Up @@ -42,11 +41,6 @@ type ResolverSettings struct {
// Metrics is the metrics interface to record metric data.
// It defaults to a No-Op metrics implementation.
Metrics Metrics
// Picker is the picker to use for each upstream call to pick
// a server from a pool of servers. It must be thread safe.
// It defaults to a fast thread safe pseudo random picker
// with uniform distribution.
Picker Picker
}

func (s *ServerSettings) SetDefaults() {
Expand All @@ -62,7 +56,6 @@ func (s *ResolverSettings) SetDefaults() {
const defaultTimeout = 5 * time.Second
s.Timeout = gosettings.DefaultComparable(s.Timeout, defaultTimeout)
s.Metrics = gosettings.DefaultComparable[Metrics](s.Metrics, metricsnoop.New())
s.Picker = gosettings.DefaultComparable[Picker](s.Picker, picker.New())
}

var (
Expand Down
4 changes: 0 additions & 4 deletions pkg/doh/settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"testing"
"time"

"github.com/qdm12/dns/v2/internal/picker"
metrics "github.com/qdm12/dns/v2/pkg/doh/metrics/noop"
log "github.com/qdm12/dns/v2/pkg/log/noop"
"github.com/qdm12/dns/v2/pkg/provider"
Expand All @@ -16,14 +15,12 @@ func Test_ServerSettings_SetDefaults(t *testing.T) {

metrics := metrics.New()
logger := log.New()
picker := picker.New()

s := ServerSettings{
Middlewares: []Middleware{},
Logger: logger,
Resolver: ResolverSettings{
Metrics: metrics,
Picker: picker,
},
}
s.SetDefaults()
Expand All @@ -41,7 +38,6 @@ func Test_ServerSettings_SetDefaults(t *testing.T) {
IPVersion: "ipv4",
Timeout: 5 * time.Second,
Metrics: metrics,
Picker: picker,
},
ListeningAddress: ptrTo(":53"),
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/dot/dial.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/tls"
"net"

"github.com/qdm12/dns/v2/internal/picker"
"github.com/qdm12/dns/v2/internal/server"
"github.com/qdm12/dns/v2/pkg/provider"
)
Expand All @@ -22,7 +23,7 @@ func newDoTDial(settings ResolverSettings) (dial server.Dial) {
Timeout: settings.Timeout,
}

picker := settings.Picker
picker := picker.New()
ipv6 := settings.IPVersion == "ipv6"

return func(ctx context.Context, _, _ string) (net.Conn, error) {
Expand All @@ -47,7 +48,7 @@ func newDoTDial(settings ResolverSettings) (dial server.Dial) {
}
}

func pickNameAddress(picker Picker, servers []provider.DoTServer,
func pickNameAddress(picker *picker.Picker, servers []provider.DoTServer,
ipv6 bool) (name, address string) {
server := picker.DoTServer(servers)
addrPort := picker.DoTAddrPort(server, ipv6)
Expand Down
31 changes: 23 additions & 8 deletions pkg/dot/dial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,42 @@ package dot
import (
"testing"

"github.com/golang/mock/gomock"
"github.com/qdm12/dns/v2/internal/picker"
"github.com/qdm12/dns/v2/pkg/provider"
"github.com/stretchr/testify/assert"
)

func Test_pickNameAddress(t *testing.T) {
t.Parallel()
ctrl := gomock.NewController(t)

picker := NewMockPicker(ctrl)
picker := picker.New()
servers := []provider.DoTServer{
provider.Cloudflare().DoT,
provider.Google().DoT,
}
const ipv6 = true

picker.EXPECT().DoTServer(servers).Return(servers[0])
picker.EXPECT().DoTAddrPort(servers[0], ipv6).Return(servers[0].IPv6[0])

name, address := pickNameAddress(picker, servers, ipv6)

assert.Equal(t, "cloudflare-dns.com", name)
assert.Equal(t, "[2606:4700:4700::1111]:853", address)
found := false
for _, server := range servers {
if server.Name != name {
continue
}
ips := server.IPv4
if ipv6 {
ips = append(ips, server.IPv6...)
}
for _, addrPort := range ips {
if addrPort.String() == address {
found = true
break
}
}
if found {
break
}
}

assert.True(t, found)
}
8 changes: 0 additions & 8 deletions pkg/dot/interfaces.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package dot

import (
"net/netip"

"github.com/miekg/dns"
"github.com/qdm12/dns/v2/pkg/provider"
)

type Middleware interface {
Expand All @@ -28,8 +25,3 @@ type Logger interface {
type Warner interface {
Warn(s string)
}

type Picker interface {
DoTServer(servers []provider.DoTServer) provider.DoTServer
DoTAddrPort(server provider.DoTServer, ipv6 bool) netip.AddrPort
}
2 changes: 1 addition & 1 deletion pkg/dot/mocks_generate_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dot

//go:generate mockgen -destination=mocks_test.go -package $GOPACKAGE . Metrics,Logger,Picker
//go:generate mockgen -destination=mocks_test.go -package $GOPACKAGE . Metrics,Logger
//go:generate mockgen -destination=mocks_local_test.go -package $GOPACKAGE -source interfaces_test.go
55 changes: 1 addition & 54 deletions pkg/dot/mocks_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 0 additions & 7 deletions pkg/dot/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"os"
"time"

"github.com/qdm12/dns/v2/internal/picker"
metricsnoop "github.com/qdm12/dns/v2/pkg/dot/metrics/noop"
lognoop "github.com/qdm12/dns/v2/pkg/log/noop"
"github.com/qdm12/dns/v2/pkg/provider"
Expand Down Expand Up @@ -50,11 +49,6 @@ type ResolverSettings struct {
// Metrics is the metrics interface to record metric data.
// It defaults to a No-Op metrics implementation.
Metrics Metrics
// Picker is the picker to use for each upstream call to pick
// a server and/or IP address. It must be thread safe.
// It defaults to a fast thread safe pseudo random picker
// with uniform distribution.
Picker Picker
}

func (s *ServerSettings) SetDefaults() {
Expand All @@ -73,7 +67,6 @@ func (s *ResolverSettings) SetDefaults() {
s.IPVersion = gosettings.DefaultComparable(s.IPVersion, "ipv4")
s.Warner = gosettings.DefaultComparable[Warner](s.Warner, lognoop.New())
s.Metrics = gosettings.DefaultComparable[Metrics](s.Metrics, metricsnoop.New())
s.Picker = gosettings.DefaultComparable[Picker](s.Picker, picker.New())
}

var (
Expand Down

0 comments on commit 65ca23e

Please sign in to comment.