Skip to content

Commit

Permalink
chore(all): prefer local over imported interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
qdm12 committed Aug 7, 2023
1 parent ff4fc58 commit 1f69110
Show file tree
Hide file tree
Showing 70 changed files with 1,001 additions and 1,212 deletions.
2 changes: 1 addition & 1 deletion cmd/dns/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func _main(ctx context.Context, buildInfo models.BuildInformation, //nolint:cycl
client := &http.Client{Timeout: clientTimeout}
blockBuilder := setup.BuildBlockBuilder(settings.Block, client)
prometheusRegistry := prometheus.NewRegistry()
cacheMetrics, err := setup.CacheMetrics(settings.Metrics, prometheusRegistry)
cacheMetrics, err := setup.BuildCacheMetrics(settings.Metrics, prometheusRegistry)
if err != nil {
return fmt.Errorf("cache metrics: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/dns/loop.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func (l *loop) runSubsequent(ctx context.Context, ready chan<- struct{}) (err er

func (l *loop) setupAll(ctx context.Context, downloadBlockFiles bool) ( //nolint:ireturn
dnsServer Service, err error) {
filterMetrics, err := setup.FilterMetrics(l.settings.Metrics, l.prometheusRegistry)
filterMetrics, err := setup.BuildFilterMetrics(l.settings.Metrics, l.prometheusRegistry)
if err != nil {
return nil, fmt.Errorf("setting up filter metrics: %w", err)
}
Expand Down
6 changes: 0 additions & 6 deletions internal/health/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,6 @@ func IsClientMode(args []string) bool {
return len(args) > 1 && args[1] == "healthcheck"
}

var _ Querier = (*Client)(nil)

type Querier interface {
Query(ctx context.Context) error
}

type Client struct {
*http.Client
}
Expand Down
8 changes: 2 additions & 6 deletions internal/metrics/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,8 @@ import (
"github.com/qdm12/log"
)

type Logger interface {
type ParentLogger interface {
New(options ...log.Option) *log.Logger
Debug(s string)
Info(s string)
Warn(s string)
Error(s string)
}

type PrometheusGatherer interface {
Expand All @@ -30,7 +26,7 @@ type Service interface {
}

func New(settings settings.Metrics, //nolint:ireturn
parentLogger Logger, prometheusGatherer PrometheusGatherer) (
parentLogger ParentLogger, prometheusGatherer PrometheusGatherer) (
service Service, err error) {
switch settings.Type {
case "noop":
Expand Down
7 changes: 0 additions & 7 deletions internal/models/server.go

This file was deleted.

3 changes: 1 addition & 2 deletions internal/server/exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"net"

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

type Exchange = func(ctx context.Context, request *dns.Msg) (
Expand All @@ -15,7 +14,7 @@ type Exchange = func(ctx context.Context, request *dns.Msg) (

type Dial = func(ctx context.Context, _, _ string) (net.Conn, error)

func NewExchange(name string, dial Dial, warner log.Warner) Exchange {
func NewExchange(name string, dial Dial, warner Warner) Exchange {
client := &dns.Client{}
return func(ctx context.Context, request *dns.Msg) (response *dns.Msg, err error) {
netConn, err := dial(ctx, "", "")
Expand Down
12 changes: 4 additions & 8 deletions internal/server/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,20 @@ import (
"context"

"github.com/miekg/dns"
"github.com/qdm12/dns/v2/pkg/cache"
"github.com/qdm12/dns/v2/pkg/filter"
"github.com/qdm12/dns/v2/pkg/log"
)

var _ dns.Handler = (*Handler)(nil)

type Handler struct {
ctx context.Context //nolint:containedctx
exchange Exchange
filter filter.Interface
cache cache.Interface
logger log.Logger
filter Filter
cache Cache
logger Logger
}

func New(ctx context.Context, exchange Exchange,
filter filter.Interface, cache cache.Interface,
logger log.Logger) *Handler {
filter Filter, cache Cache, logger Logger) *Handler {
return &Handler{
ctx: ctx,
exchange: exchange,
Expand Down
29 changes: 29 additions & 0 deletions internal/server/interfaces.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package server

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

type Filter interface {
FilterRequest(request *dns.Msg) (blocked bool)
FilterResponse(response *dns.Msg) (blocked bool)
Update(settings update.Settings)
}

type Cache interface {
Add(request, response *dns.Msg)
Get(request *dns.Msg) (response *dns.Msg)
Remove(request *dns.Msg)
}

type Logger interface {
Debug(s string)
Info(s string)
Warner
Error(s string)
}

type Warner interface {
Warn(s string)
}
7 changes: 3 additions & 4 deletions internal/setup/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/prometheus/client_golang/prometheus"
"github.com/qdm12/dns/v2/internal/config/settings"
"github.com/qdm12/dns/v2/pkg/cache/lru"
"github.com/qdm12/dns/v2/pkg/cache/metrics"
noopmetrics "github.com/qdm12/dns/v2/pkg/cache/metrics/noop"
prommetrics "github.com/qdm12/dns/v2/pkg/cache/metrics/prometheus"
"github.com/qdm12/dns/v2/pkg/cache/noop"
Expand All @@ -21,7 +20,7 @@ type Cache interface {
}

func BuildCache(userSettings settings.Cache, //nolint:ireturn
metrics metrics.Interface) (
metrics CacheMetrics) (
cache Cache) {
switch userSettings.Type {
case noop.CacheType:
Expand All @@ -36,9 +35,9 @@ func BuildCache(userSettings settings.Cache, //nolint:ireturn
}
}

func CacheMetrics(userSettings settings.Metrics, //nolint:ireturn
func BuildCacheMetrics(userSettings settings.Metrics, //nolint:ireturn
registry prometheus.Registerer) (
metrics metrics.Interface, err error) {
metrics CacheMetrics, err error) {
switch userSettings.Type {
case noopString:
return noopmetrics.New(), nil
Expand Down
5 changes: 2 additions & 3 deletions internal/setup/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ import (

"github.com/prometheus/client_golang/prometheus"
"github.com/qdm12/dns/v2/internal/config/settings"
"github.com/qdm12/dns/v2/pkg/filter/metrics"
noopmetrics "github.com/qdm12/dns/v2/pkg/filter/metrics/noop"
prommetrics "github.com/qdm12/dns/v2/pkg/filter/metrics/prometheus"
promcommon "github.com/qdm12/dns/v2/pkg/metrics/prometheus"
)

func FilterMetrics(userSettings settings.Metrics, //nolint:ireturn
func BuildFilterMetrics(userSettings settings.Metrics, //nolint:ireturn
registry prometheus.Registerer) (
metrics metrics.Interface, err error) {
metrics FilterMetrics, err error) {
switch userSettings.Type {
case noopString:
return noopmetrics.New(), nil
Expand Down
27 changes: 22 additions & 5 deletions internal/setup/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,20 @@ type Filter interface {
Update(settings update.Settings)
}

type FilterMetrics interface {
SetBlockedHostnames(n int)
SetBlockedIPs(n int)
SetBlockedIPPrefixes(n int)
HostnamesFilteredInc(qClass, qType string)
IPsFilteredInc(rrtype string)
}

type Middleware interface {
Wrap(next dns.Handler) dns.Handler
}

type PrometheusRegisterer prometheus.Registerer

type Metrics interface {
DoTMetrics
DoHMetrics
}

type DoTMetrics interface {
DoTDialInc(provider, address, outcome string)
DNSDialInc(address, outcome string)
Expand All @@ -40,3 +43,17 @@ type DoHMetrics interface {
DoTDialInc(provider, address, outcome string)
DNSDialInc(address, outcome string)
}

type CacheMetrics interface { //nolint:interfacebloat
SetCacheType(cacheType string)
CacheInsertInc()
CacheRemoveInc()
CacheMoveInc()
CacheGetEmptyInc()
CacheInsertEmptyInc()
CacheRemoveEmptyInc()
CacheHitInc()
CacheMissInc()
CacheExpiredInc()
CacheMaxEntriesSet(maxEntries uint)
}
10 changes: 9 additions & 1 deletion internal/setup/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,15 @@ import (
func middlewareMetrics(metricsType string,
commonPrometheus prometheus.Settings) (
middleware *metricsmiddleware.Middleware, err error) {
var metrics metricsmiddleware.Interface
var metrics interface {
RequestsInc()
QuestionsInc(class, qType string)
RcodeInc(rcode string)
AnswersInc(class, qType string)
ResponsesInc()
InFlightRequestsInc()
InFlightRequestsDec()
}
switch metricsType {
case noopString:
metrics = noopmetrics.New()
Expand Down
7 changes: 0 additions & 7 deletions pkg/blockbuilder/builder.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
package blockbuilder

import (
"context"
"net/http"
"net/netip"
)

var _ Interface = (*Builder)(nil)

type Interface interface {
BuildAll(ctx context.Context) Result
}

func New(settings Settings) *Builder {
settings.SetDefaults()

Expand Down
11 changes: 0 additions & 11 deletions pkg/cache/cache.go

This file was deleted.

15 changes: 15 additions & 0 deletions pkg/cache/lru/interfaces.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package lru

type Metrics interface { //nolint:interfacebloat
SetCacheType(cacheType string)
CacheInsertInc()
CacheRemoveInc()
CacheMoveInc()
CacheGetEmptyInc()
CacheInsertEmptyInc()
CacheRemoveEmptyInc()
CacheHitInc()
CacheMissInc()
CacheExpiredInc()
CacheMaxEntriesSet(maxEntries uint)
}
3 changes: 1 addition & 2 deletions pkg/cache/lru/lru.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"time"

"github.com/miekg/dns"
"github.com/qdm12/dns/v2/pkg/cache/metrics"
)

type LRU struct {
Expand All @@ -19,7 +18,7 @@ type LRU struct {
mutex sync.Mutex

// External objects
metrics metrics.Interface
metrics Metrics

// Mock fields
timeNow func() time.Time
Expand Down
2 changes: 0 additions & 2 deletions pkg/cache/lru/lru_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ func newTestMsgs(name string, expUnix uint32) (request, response *dns.Msg) {
return request, response
}

//go:generate mockgen -destination=mock_metrics_test.go -package $GOPACKAGE -mock_names Interface=MockMetrics github.com/qdm12/dns/v2/pkg/cache/metrics Interface

func Test_lru_e2e(t *testing.T) {
t.Parallel()

Expand Down
3 changes: 3 additions & 0 deletions pkg/cache/lru/mocks_generate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package lru

//go:generate mockgen -destination=mocks_test.go -package $GOPACKAGE . Metrics

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

3 changes: 1 addition & 2 deletions pkg/cache/lru/settings.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package lru

import (
"github.com/qdm12/dns/v2/pkg/cache/metrics"
"github.com/qdm12/dns/v2/pkg/cache/metrics/noop"
"github.com/qdm12/gosettings"
"github.com/qdm12/gotree"
Expand All @@ -13,7 +12,7 @@ type Settings struct {
MaxEntries uint
// Metrics is the metrics interface to record metric information
// for the cache. It defaults to a No-Op metric implementation.
Metrics metrics.Interface
Metrics Metrics
}

func (s *Settings) SetDefaults() {
Expand Down
Loading

0 comments on commit 1f69110

Please sign in to comment.