Maint: dns package state rework
- Interface composition with loopstate interfaces - Use loopstate.Manager - Create dns/state package for handling settings
This commit is contained in:
51
internal/dns/state/settings.go
Normal file
51
internal/dns/state/settings.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package state
|
||||
|
||||
import (
|
||||
"context"
|
||||
"reflect"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/configuration"
|
||||
"github.com/qdm12/gluetun/internal/constants"
|
||||
)
|
||||
|
||||
type SettingsGetterSetter interface {
|
||||
GetSettings() (settings configuration.DNS)
|
||||
SetSettings(ctx context.Context,
|
||||
settings configuration.DNS) (outcome string)
|
||||
}
|
||||
|
||||
func (s *State) GetSettings() (settings configuration.DNS) {
|
||||
s.settingsMu.RLock()
|
||||
defer s.settingsMu.RUnlock()
|
||||
return s.settings
|
||||
}
|
||||
|
||||
func (s *State) SetSettings(ctx context.Context, settings configuration.DNS) (
|
||||
outcome string) {
|
||||
s.settingsMu.Lock()
|
||||
defer s.settingsMu.Unlock()
|
||||
|
||||
settingsUnchanged := reflect.DeepEqual(s.settings, settings)
|
||||
if settingsUnchanged {
|
||||
return "settings left unchanged"
|
||||
}
|
||||
|
||||
// Check for only update period change
|
||||
tempSettings := s.settings
|
||||
tempSettings.UpdatePeriod = settings.UpdatePeriod
|
||||
onlyUpdatePeriodChanged := reflect.DeepEqual(tempSettings, settings)
|
||||
|
||||
s.settings = settings
|
||||
|
||||
if onlyUpdatePeriodChanged {
|
||||
s.updateTicker <- struct{}{}
|
||||
return "update period changed"
|
||||
}
|
||||
|
||||
// Restart
|
||||
_, _ = s.statusApplier.ApplyStatus(ctx, constants.Stopped)
|
||||
if settings.Enabled {
|
||||
outcome, _ = s.statusApplier.ApplyStatus(ctx, constants.Running)
|
||||
}
|
||||
return outcome
|
||||
}
|
||||
33
internal/dns/state/state.go
Normal file
33
internal/dns/state/state.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package state
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/configuration"
|
||||
"github.com/qdm12/gluetun/internal/loopstate"
|
||||
)
|
||||
|
||||
var _ Manager = (*State)(nil)
|
||||
|
||||
type Manager interface {
|
||||
SettingsGetterSetter
|
||||
}
|
||||
|
||||
func New(statusApplier loopstate.Applier,
|
||||
settings configuration.DNS,
|
||||
updateTicker chan<- struct{}) *State {
|
||||
return &State{
|
||||
statusApplier: statusApplier,
|
||||
settings: settings,
|
||||
updateTicker: updateTicker,
|
||||
}
|
||||
}
|
||||
|
||||
type State struct {
|
||||
statusApplier loopstate.Applier
|
||||
|
||||
settings configuration.DNS
|
||||
settingsMu sync.RWMutex
|
||||
|
||||
updateTicker chan<- struct{}
|
||||
}
|
||||
Reference in New Issue
Block a user