mirror of
https://github.com/qdm12/gluetun.git
synced 2026-05-09 20:29:23 +02:00
Maint: rework publicip package
- Use loopstate package - Loop interface composition - Return concrete struct from constructors - Split into more files - Add publicip/state package
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
package state
|
||||
|
||||
import (
|
||||
"net"
|
||||
)
|
||||
|
||||
type PublicIPGetSetter interface {
|
||||
PublicIPGetter
|
||||
SetPublicIP(publicIP net.IP)
|
||||
}
|
||||
|
||||
type PublicIPGetter interface {
|
||||
GetPublicIP() (publicIP net.IP)
|
||||
}
|
||||
|
||||
func (s *State) GetPublicIP() (publicIP net.IP) {
|
||||
s.publicIPMu.RLock()
|
||||
defer s.publicIPMu.RUnlock()
|
||||
publicIP = make(net.IP, len(s.publicIP))
|
||||
copy(publicIP, s.publicIP)
|
||||
return publicIP
|
||||
}
|
||||
|
||||
func (s *State) SetPublicIP(publicIP net.IP) {
|
||||
s.settingsMu.Lock()
|
||||
defer s.settingsMu.Unlock()
|
||||
s.publicIP = make(net.IP, len(publicIP))
|
||||
copy(s.publicIP, publicIP)
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package state
|
||||
|
||||
import (
|
||||
"context"
|
||||
"reflect"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/configuration"
|
||||
)
|
||||
|
||||
type SettingsGetterSetter interface {
|
||||
GetSettings() (settings configuration.PublicIP)
|
||||
SetSettings(ctx context.Context,
|
||||
settings configuration.PublicIP) (outcome string)
|
||||
}
|
||||
|
||||
func (s *State) GetSettings() (settings configuration.PublicIP) {
|
||||
s.settingsMu.RLock()
|
||||
defer s.settingsMu.RUnlock()
|
||||
return s.settings
|
||||
}
|
||||
|
||||
func (s *State) SetSettings(ctx context.Context, settings configuration.PublicIP) (
|
||||
outcome string) {
|
||||
s.settingsMu.Lock()
|
||||
defer s.settingsMu.Unlock()
|
||||
|
||||
settingsUnchanged := reflect.DeepEqual(s.settings, settings)
|
||||
if settingsUnchanged {
|
||||
return "settings left unchanged"
|
||||
}
|
||||
|
||||
periodChanged := s.settings.Period != settings.Period
|
||||
s.settings = settings
|
||||
if periodChanged {
|
||||
s.updateTicker <- struct{}{}
|
||||
// TODO blocking
|
||||
}
|
||||
return "settings updated"
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package state
|
||||
|
||||
import (
|
||||
"net"
|
||||
"sync"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/configuration"
|
||||
"github.com/qdm12/gluetun/internal/loopstate"
|
||||
)
|
||||
|
||||
var _ Manager = (*State)(nil)
|
||||
|
||||
type Manager interface {
|
||||
SettingsGetterSetter
|
||||
PublicIPGetSetter
|
||||
}
|
||||
|
||||
func New(statusApplier loopstate.Applier,
|
||||
settings configuration.PublicIP,
|
||||
updateTicker chan<- struct{}) *State {
|
||||
return &State{
|
||||
statusApplier: statusApplier,
|
||||
settings: settings,
|
||||
updateTicker: updateTicker,
|
||||
}
|
||||
}
|
||||
|
||||
type State struct {
|
||||
statusApplier loopstate.Applier
|
||||
|
||||
settings configuration.PublicIP
|
||||
settingsMu sync.RWMutex
|
||||
|
||||
publicIP net.IP
|
||||
publicIPMu sync.RWMutex
|
||||
|
||||
updateTicker chan<- struct{}
|
||||
}
|
||||
Reference in New Issue
Block a user