Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring/config struct names #1315

Merged
merged 16 commits into from
Dec 20, 2023
Merged
4 changes: 2 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"editor.formatOnSave": true,
"editor.formatOnPaste": true,
"editor.codeActionsOnSave": {
"source.organizeImports": true,
"source.fixAll": true
"source.organizeImports": "explicit",
"source.fixAll": "explicit"
},
"editor.rulers": [120],
"go.showWelcome": false,
Expand Down
2 changes: 1 addition & 1 deletion config/blocking.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type Blocking struct {
ClientGroupsBlock map[string][]string `yaml:"clientGroupsBlock"`
BlockType string `yaml:"blockType" default:"ZEROIP"`
BlockTTL Duration `yaml:"blockTTL" default:"6h"`
Loading SourceLoadingConfig `yaml:"loading"`
Loading SourceLoading `yaml:"loading"`

// Deprecated options
Deprecated struct {
Expand Down
10 changes: 5 additions & 5 deletions config/caching.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"github.com/sirupsen/logrus"
)

// CachingConfig configuration for domain caching
type CachingConfig struct {
// Caching configuration for domain caching
type Caching struct {
MinCachingTime Duration `yaml:"minTime"`
MaxCachingTime Duration `yaml:"maxTime"`
CacheTimeNegative Duration `yaml:"cacheTimeNegative" default:"30m"`
Expand All @@ -19,12 +19,12 @@ type CachingConfig struct {
}

// IsEnabled implements `config.Configurable`.
func (c *CachingConfig) IsEnabled() bool {
func (c *Caching) IsEnabled() bool {
return c.MaxCachingTime.IsAtLeastZero()
}

// LogConfig implements `config.Configurable`.
func (c *CachingConfig) LogConfig(logger *logrus.Entry) {
func (c *Caching) LogConfig(logger *logrus.Entry) {
logger.Infof("minTime = %s", c.MinCachingTime)
logger.Infof("maxTime = %s", c.MaxCachingTime)
logger.Infof("cacheTimeNegative = %s", c.CacheTimeNegative)
Expand All @@ -39,7 +39,7 @@ func (c *CachingConfig) LogConfig(logger *logrus.Entry) {
}
}

func (c *CachingConfig) EnablePrefetch() {
func (c *Caching) EnablePrefetch() {
const day = Duration(24 * time.Hour)

if !c.IsEnabled() {
Expand Down
14 changes: 7 additions & 7 deletions config/caching_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,27 @@ import (
)

var _ = Describe("CachingConfig", func() {
var cfg CachingConfig
var cfg Caching

suiteBeforeEach()

BeforeEach(func() {
cfg = CachingConfig{
cfg = Caching{
MaxCachingTime: Duration(time.Hour),
}
})

Describe("IsEnabled", func() {
It("should be true by default", func() {
cfg := CachingConfig{}
cfg := Caching{}
Expect(defaults.Set(&cfg)).Should(Succeed())

Expect(cfg.IsEnabled()).Should(BeTrue())
})

When("the config is disabled", func() {
BeforeEach(func() {
cfg = CachingConfig{
cfg = Caching{
MaxCachingTime: Duration(time.Hour * -1),
}
})
Expand All @@ -46,7 +46,7 @@ var _ = Describe("CachingConfig", func() {

When("the config is disabled", func() {
It("should be false", func() {
cfg := CachingConfig{
cfg := Caching{
MaxCachingTime: Duration(-1),
}

Expand All @@ -58,7 +58,7 @@ var _ = Describe("CachingConfig", func() {
Describe("LogConfig", func() {
When("prefetching is enabled", func() {
BeforeEach(func() {
cfg = CachingConfig{
cfg = Caching{
Prefetching: true,
}
})
Expand All @@ -75,7 +75,7 @@ var _ = Describe("CachingConfig", func() {
Describe("EnablePrefetch", func() {
When("prefetching is enabled", func() {
BeforeEach(func() {
cfg = CachingConfig{}
cfg = Caching{}
})

It("should return configuration", func() {
Expand Down
82 changes: 41 additions & 41 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,41 +171,41 @@ func (l *ListenConfig) UnmarshalText(data []byte) error {
return nil
}

// UnmarshalYAML creates BootstrapDNSConfig from YAML
func (b *BootstrapDNSConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
var single BootstrappedUpstreamConfig
// UnmarshalYAML creates BootstrapDNS from YAML
func (b *BootstrapDNS) UnmarshalYAML(unmarshal func(interface{}) error) error {
var single BootstrappedUpstream
if err := unmarshal(&single); err == nil {
*b = BootstrapDNSConfig{single}
*b = BootstrapDNS{single}

return nil
}

// bootstrapDNSConfig is used to avoid infinite recursion:
// if we used BootstrapDNSConfig, unmarshal would just call us again.
var c bootstrapDNSConfig
// bootstrapDNS is used to avoid infinite recursion:
// if we used BootstrapDNS, unmarshal would just call us again.
var c bootstrapDNS
if err := unmarshal(&c); err != nil {
return err
}

*b = BootstrapDNSConfig(c)
*b = BootstrapDNS(c)

return nil
}

// UnmarshalYAML creates BootstrapConfig from YAML
func (b *BootstrappedUpstreamConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
// UnmarshalYAML creates BootstrappedUpstream from YAML
func (b *BootstrappedUpstream) UnmarshalYAML(unmarshal func(interface{}) error) error {
if err := unmarshal(&b.Upstream); err == nil {
return nil
}

// bootstrapConfig is used to avoid infinite recursion:
// if we used BootstrapConfig, unmarshal would just call us again.
var c bootstrappedUpstreamConfig
// bootstrappedUpstream is used to avoid infinite recursion:
// if we used BootstrappedUpstream, unmarshal would just call us again.
var c bootstrappedUpstream
if err := unmarshal(&c); err != nil {
return err
}

*b = BootstrappedUpstreamConfig(c)
*b = BootstrappedUpstream(c)

return nil
}
Expand All @@ -218,19 +218,19 @@ type Config struct {
Conditional ConditionalUpstream `yaml:"conditional"`
Blocking Blocking `yaml:"blocking"`
ClientLookup ClientLookup `yaml:"clientLookup"`
Caching CachingConfig `yaml:"caching"`
QueryLog QueryLogConfig `yaml:"queryLog"`
Prometheus MetricsConfig `yaml:"prometheus"`
Caching Caching `yaml:"caching"`
QueryLog QueryLog `yaml:"queryLog"`
Prometheus Metrics `yaml:"prometheus"`
Redis Redis `yaml:"redis"`
Log log.Config `yaml:"log"`
Ports PortsConfig `yaml:"ports"`
Ports Ports `yaml:"ports"`
MinTLSServeVer TLSVersion `yaml:"minTlsServeVersion" default:"1.2"`
CertFile string `yaml:"certFile"`
KeyFile string `yaml:"keyFile"`
BootstrapDNS BootstrapDNSConfig `yaml:"bootstrapDns"`
HostsFile HostsFileConfig `yaml:"hostsFile"`
BootstrapDNS BootstrapDNS `yaml:"bootstrapDns"`
HostsFile HostsFile `yaml:"hostsFile"`
FQDNOnly FQDNOnly `yaml:"fqdnOnly"`
Filtering FilteringConfig `yaml:"filtering"`
Filtering Filtering `yaml:"filtering"`
EDE EDE `yaml:"ede"`
ECS ECS `yaml:"ecs"`
SUDN SUDN `yaml:"specialUseDomains"`
Expand All @@ -253,40 +253,40 @@ type Config struct {
} `yaml:",inline"`
}

type PortsConfig struct {
type Ports struct {
DNS ListenConfig `yaml:"dns" default:"53"`
HTTP ListenConfig `yaml:"http"`
HTTPS ListenConfig `yaml:"https"`
TLS ListenConfig `yaml:"tls"`
}

func (c *PortsConfig) LogConfig(logger *logrus.Entry) {
func (c *Ports) LogConfig(logger *logrus.Entry) {
logger.Infof("DNS = %s", c.DNS)
logger.Infof("TLS = %s", c.TLS)
logger.Infof("HTTP = %s", c.HTTP)
logger.Infof("HTTPS = %s", c.HTTPS)
}

// split in two types to avoid infinite recursion. See `BootstrapDNSConfig.UnmarshalYAML`.
// split in two types to avoid infinite recursion. See `BootstrapDNS.UnmarshalYAML`.
type (
BootstrapDNSConfig bootstrapDNSConfig
bootstrapDNSConfig []BootstrappedUpstreamConfig
BootstrapDNS bootstrapDNS
bootstrapDNS []BootstrappedUpstream
)

func (b *BootstrapDNSConfig) IsEnabled() bool {
func (b *BootstrapDNS) IsEnabled() bool {
return len(*b) != 0
}

func (b *BootstrapDNSConfig) LogConfig(*logrus.Entry) {
func (b *BootstrapDNS) LogConfig(*logrus.Entry) {
// This should not be called, at least for now:
// The Boostrap resolver is not in the chain and thus its config is not logged
panic("not implemented")
}

// split in two types to avoid infinite recursion. See `BootstrappedUpstreamConfig.UnmarshalYAML`.
// split in two types to avoid infinite recursion. See `BootstrappedUpstream.UnmarshalYAML`.
type (
BootstrappedUpstreamConfig bootstrappedUpstreamConfig
bootstrappedUpstreamConfig struct {
BootstrappedUpstream bootstrappedUpstream
bootstrappedUpstream struct {
Upstream Upstream `yaml:"upstream"`
IPs []net.IP `yaml:"ips"`
}
Expand Down Expand Up @@ -319,16 +319,16 @@ func (c *Init) LogConfig(logger *logrus.Entry) {
logger.Debugf("strategy = %s", c.Strategy)
}

type SourceLoadingConfig struct {
type SourceLoading struct {
Init `yaml:",inline"`

Concurrency uint `yaml:"concurrency" default:"4"`
MaxErrorsPerSource int `yaml:"maxErrorsPerSource" default:"5"`
RefreshPeriod Duration `yaml:"refreshPeriod" default:"4h"`
Downloads DownloaderConfig `yaml:"downloads"`
Concurrency uint `yaml:"concurrency" default:"4"`
MaxErrorsPerSource int `yaml:"maxErrorsPerSource" default:"5"`
RefreshPeriod Duration `yaml:"refreshPeriod" default:"4h"`
Downloads Downloader `yaml:"downloads"`
}

func (c *SourceLoadingConfig) LogConfig(logger *logrus.Entry) {
func (c *SourceLoading) LogConfig(logger *logrus.Entry) {
c.Init.LogConfig(logger)
logger.Infof("concurrency = %d", c.Concurrency)
logger.Debugf("maxErrorsPerSource = %d", c.MaxErrorsPerSource)
Expand All @@ -343,7 +343,7 @@ func (c *SourceLoadingConfig) LogConfig(logger *logrus.Entry) {
log.WithIndent(logger, " ", c.Downloads.LogConfig)
}

func (c *SourceLoadingConfig) StartPeriodicRefresh(
func (c *SourceLoading) StartPeriodicRefresh(
ctx context.Context, refresh func(context.Context) error, logErr func(error),
) error {
err := c.Strategy.Do(ctx, refresh, logErr)
Expand All @@ -358,7 +358,7 @@ func (c *SourceLoadingConfig) StartPeriodicRefresh(
return nil
}

func (c *SourceLoadingConfig) periodically(
func (c *SourceLoading) periodically(
ctx context.Context, refresh func(context.Context) error, logErr func(error),
) {
refresh = recoverToError(refresh, func(panicVal any) error {
Expand Down Expand Up @@ -394,13 +394,13 @@ func recoverToError(do func(context.Context) error, onPanic func(any) error) fun
}
}

type DownloaderConfig struct {
type Downloader struct {
Timeout Duration `yaml:"timeout" default:"5s"`
Attempts uint `yaml:"attempts" default:"3"`
Cooldown Duration `yaml:"cooldown" default:"500ms"`
}

func (c *DownloaderConfig) LogConfig(logger *logrus.Entry) {
func (c *Downloader) LogConfig(logger *logrus.Entry) {
logger.Infof("timeout = %s", c.Timeout)
logger.Infof("attempts = %d", c.Attempts)
logger.Debugf("cooldown = %s", c.Cooldown)
Expand Down
18 changes: 9 additions & 9 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -547,10 +547,10 @@ bootstrapDns:
)

Describe("SourceLoadingConfig", func() {
var cfg SourceLoadingConfig
var cfg SourceLoading

BeforeEach(func() {
cfg = SourceLoadingConfig{
cfg = SourceLoading{
Concurrency: 12,
RefreshPeriod: Duration(time.Hour),
}
Expand Down Expand Up @@ -744,22 +744,22 @@ bootstrapDns:

Describe("BootstrapDNSConfig", func() {
It("is not enabled when empty", func() {
var sut BootstrapDNSConfig
var sut BootstrapDNS

Expect(sut.IsEnabled()).Should(BeFalse())
})

It("is enabled if non empty", func() {
sut := BootstrapDNSConfig{
BootstrappedUpstreamConfig{},
BootstrappedUpstreamConfig{},
sut := BootstrapDNS{
BootstrappedUpstream{},
BootstrappedUpstream{},
}

Expect(sut.IsEnabled()).Should(BeTrue())
})

It("LogConfig panics", func() {
sut := BootstrapDNSConfig{}
sut := BootstrapDNS{}

Expect(func() {
sut.LogConfig(logger)
Expand All @@ -777,7 +777,7 @@ bootstrapDns:
DeferCleanup(cancelFn)
})
It("handles panics", func() {
sut := SourceLoadingConfig{
sut := SourceLoading{
Init: Init{Strategy: InitStrategyFailOnError},
}

Expand All @@ -793,7 +793,7 @@ bootstrapDns:
})

It("periodically calls refresh", func() {
sut := SourceLoadingConfig{
sut := SourceLoading{
Init: Init{Strategy: InitStrategyFast},
RefreshPeriod: Duration(5 * time.Millisecond),
}
Expand Down
6 changes: 3 additions & 3 deletions config/filtering.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ import (
"github.com/sirupsen/logrus"
)

type FilteringConfig struct {
type Filtering struct {
QueryTypes QTypeSet `yaml:"queryTypes"`
}

// IsEnabled implements `config.Configurable`.
func (c *FilteringConfig) IsEnabled() bool {
func (c *Filtering) IsEnabled() bool {
return len(c.QueryTypes) != 0
}

// LogConfig implements `config.Configurable`.
func (c *FilteringConfig) LogConfig(logger *logrus.Entry) {
func (c *Filtering) LogConfig(logger *logrus.Entry) {
logger.Info("query types:")

for qType := range c.QueryTypes {
Expand Down