Maint: dns package interface rework

- return concrete struct type
- split interface is sub-interfaces
This commit is contained in:
Quentin McGaw (desktop)
2021-07-23 18:57:29 +00:00
parent 9436f604ba
commit 02492c34a7
9 changed files with 47 additions and 23 deletions

View File

@@ -8,7 +8,7 @@ import (
"github.com/qdm12/golibs/logging"
)
func (l *looper) collectLines(stdout, stderr <-chan string, done chan<- struct{}) {
func (l *Loop) collectLines(stdout, stderr <-chan string, done chan<- struct{}) {
defer close(done)
var line string
var ok bool

View File

@@ -14,18 +14,16 @@ import (
"github.com/qdm12/golibs/logging"
)
var _ Looper = (*Loop)(nil)
type Looper interface {
Run(ctx context.Context, done chan<- struct{})
RunRestartTicker(ctx context.Context, done chan<- struct{})
GetStatus() (status models.LoopStatus)
ApplyStatus(ctx context.Context, status models.LoopStatus) (
outcome string, err error)
GetSettings() (settings configuration.DNS)
SetSettings(ctx context.Context, settings configuration.DNS) (
outcome string)
Runner
RestartTickerRunner
StatusGetterApplier
SettingsGetterSetter
}
type looper struct {
type Loop struct {
state *state
conf unbound.Configurator
resolvConf string
@@ -46,7 +44,7 @@ type looper struct {
const defaultBackoffTime = 10 * time.Second
func NewLoop(conf unbound.Configurator, settings configuration.DNS, client *http.Client,
logger logging.Logger) Looper {
logger logging.Logger) *Loop {
start := make(chan struct{})
running := make(chan models.LoopStatus)
stop := make(chan struct{})
@@ -55,7 +53,7 @@ func NewLoop(conf unbound.Configurator, settings configuration.DNS, client *http
state := newState(constants.Stopped, settings, start, running, stop, stopped, updateTicker)
return &looper{
return &Loop{
state: state,
conf: conf,
resolvConf: "/etc/resolv.conf",
@@ -74,7 +72,7 @@ func NewLoop(conf unbound.Configurator, settings configuration.DNS, client *http
}
}
func (l *looper) logAndWait(ctx context.Context, err error) {
func (l *Loop) logAndWait(ctx context.Context, err error) {
if err != nil {
l.logger.Warn(err.Error())
}
@@ -90,7 +88,7 @@ func (l *looper) logAndWait(ctx context.Context, err error) {
}
}
func (l *looper) signalOrSetStatus(status models.LoopStatus) {
func (l *Loop) signalOrSetStatus(status models.LoopStatus) {
if l.userTrigger {
l.userTrigger = false
select {

View File

@@ -2,7 +2,7 @@ package dns
import "github.com/qdm12/dns/pkg/nameserver"
func (l *looper) useUnencryptedDNS(fallback bool) {
func (l *Loop) useUnencryptedDNS(fallback bool) {
settings := l.GetSettings()
// Try with user provided plaintext ip address

View File

@@ -7,7 +7,11 @@ import (
"github.com/qdm12/gluetun/internal/constants"
)
func (l *looper) Run(ctx context.Context, done chan<- struct{}) {
type Runner interface {
Run(ctx context.Context, done chan<- struct{})
}
func (l *Loop) Run(ctx context.Context, done chan<- struct{}) {
defer close(done)
const fallback = false

View File

@@ -6,14 +6,23 @@ import (
"github.com/qdm12/gluetun/internal/configuration"
)
func (l *looper) GetSettings() (settings configuration.DNS) { return l.state.GetSettings() }
type SettingsGetterSetter interface {
SettingsGetter
SettingsSetter
}
type SettingsGetter interface {
GetSettings() (settings configuration.DNS)
}
func (l *Loop) GetSettings() (settings configuration.DNS) { return l.state.GetSettings() }
type SettingsSetter interface {
SetSettings(ctx context.Context, settings configuration.DNS) (
outcome string)
}
func (l *looper) SetSettings(ctx context.Context, settings configuration.DNS) (
func (l *Loop) SetSettings(ctx context.Context, settings configuration.DNS) (
outcome string) {
return l.state.SetSettings(ctx, settings)
}

View File

@@ -14,7 +14,7 @@ var errUpdateFiles = errors.New("cannot update files")
// Returning cancel == nil signals we want to re-run setupUnbound
// Returning err == errUpdateFiles signals we should not fall back
// on the plaintext DNS as DOT is still up and running.
func (l *looper) setupUnbound(ctx context.Context) (
func (l *Loop) setupUnbound(ctx context.Context) (
cancel context.CancelFunc, waitError chan error, closeStreams func(), err error) {
err = l.updateFiles(ctx)
if err != nil {

View File

@@ -6,14 +6,23 @@ import (
"github.com/qdm12/gluetun/internal/models"
)
func (l *looper) GetStatus() (status models.LoopStatus) { return l.state.GetStatus() }
type StatusGetterApplier interface {
StatusGetter
StatusApplier
}
type StatusGetter interface {
GetStatus() (status models.LoopStatus)
}
func (l *Loop) GetStatus() (status models.LoopStatus) { return l.state.GetStatus() }
type StatusApplier interface {
ApplyStatus(ctx context.Context, status models.LoopStatus) (
outcome string, err error)
}
func (l *looper) ApplyStatus(ctx context.Context, status models.LoopStatus) (
func (l *Loop) ApplyStatus(ctx context.Context, status models.LoopStatus) (
outcome string, err error) {
return l.state.ApplyStatus(ctx, status)
}

View File

@@ -7,7 +7,11 @@ import (
"github.com/qdm12/gluetun/internal/constants"
)
func (l *looper) RunRestartTicker(ctx context.Context, done chan<- struct{}) {
type RestartTickerRunner interface {
RunRestartTicker(ctx context.Context, done chan<- struct{})
}
func (l *Loop) RunRestartTicker(ctx context.Context, done chan<- struct{}) {
defer close(done)
// Timer that acts as a ticker
timer := time.NewTimer(time.Hour)

View File

@@ -2,7 +2,7 @@ package dns
import "context"
func (l *looper) updateFiles(ctx context.Context) (err error) {
func (l *Loop) updateFiles(ctx context.Context) (err error) {
l.logger.Info("downloading DNS over TLS cryptographic files")
if err := l.conf.SetupFiles(ctx); err != nil {
return err