Maint: rework Openvpn run loop
This commit is contained in:
@@ -12,24 +12,63 @@ import (
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
)
|
||||
|
||||
type state struct {
|
||||
status models.LoopStatus
|
||||
settings configuration.OpenVPN
|
||||
allServers models.AllServers
|
||||
portForwarded uint16
|
||||
statusMu sync.RWMutex
|
||||
settingsMu sync.RWMutex
|
||||
allServersMu sync.RWMutex
|
||||
portForwardedMu sync.RWMutex
|
||||
func newState(status models.LoopStatus,
|
||||
settings configuration.OpenVPN, allServers models.AllServers,
|
||||
start chan<- struct{}, running <-chan models.LoopStatus,
|
||||
stop chan<- struct{}, stopped <-chan struct{}) *state {
|
||||
return &state{
|
||||
status: status,
|
||||
settings: settings,
|
||||
allServers: allServers,
|
||||
start: start,
|
||||
running: running,
|
||||
stop: stop,
|
||||
stopped: stopped,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *state) setStatusWithLock(status models.LoopStatus) {
|
||||
type state struct {
|
||||
loopMu sync.RWMutex
|
||||
|
||||
status models.LoopStatus
|
||||
statusMu sync.RWMutex
|
||||
|
||||
settings configuration.OpenVPN
|
||||
settingsMu sync.RWMutex
|
||||
|
||||
allServers models.AllServers
|
||||
allServersMu sync.RWMutex
|
||||
|
||||
portForwarded uint16
|
||||
portForwardedMu sync.RWMutex
|
||||
|
||||
start chan<- struct{}
|
||||
running <-chan models.LoopStatus
|
||||
stop chan<- struct{}
|
||||
stopped <-chan struct{}
|
||||
}
|
||||
|
||||
func (s *state) Lock() { s.loopMu.Lock() }
|
||||
func (s *state) Unlock() { s.loopMu.Unlock() }
|
||||
|
||||
// SetStatus sets the status thread safely.
|
||||
// It should only be called by the loop internal code since
|
||||
// it does not interact with the loop code directly.
|
||||
func (s *state) SetStatus(status models.LoopStatus) {
|
||||
s.statusMu.Lock()
|
||||
defer s.statusMu.Unlock()
|
||||
s.status = status
|
||||
}
|
||||
|
||||
func (s *state) getSettingsAndServers() (settings configuration.OpenVPN, allServers models.AllServers) {
|
||||
// GetStatus gets the status thread safely.
|
||||
func (s *state) GetStatus() (status models.LoopStatus) {
|
||||
s.statusMu.RLock()
|
||||
defer s.statusMu.RUnlock()
|
||||
return s.status
|
||||
}
|
||||
|
||||
func (s *state) GetSettingsAndServers() (settings configuration.OpenVPN,
|
||||
allServers models.AllServers) {
|
||||
s.settingsMu.RLock()
|
||||
s.allServersMu.RLock()
|
||||
settings = s.settings
|
||||
@@ -39,100 +78,102 @@ func (s *state) getSettingsAndServers() (settings configuration.OpenVPN, allServ
|
||||
return settings, allServers
|
||||
}
|
||||
|
||||
func (l *looper) GetStatus() (status models.LoopStatus) {
|
||||
l.state.statusMu.RLock()
|
||||
defer l.state.statusMu.RUnlock()
|
||||
return l.state.status
|
||||
}
|
||||
|
||||
var ErrInvalidStatus = errors.New("invalid status")
|
||||
|
||||
func (l *looper) SetStatus(ctx context.Context, status models.LoopStatus) (
|
||||
// ApplyStatus sends signals to the running loop depending on the
|
||||
// current status and status requested, such that its next status
|
||||
// matches the requested one. It is thread safe and a synchronous call
|
||||
// since it waits to the loop to fully change its status.
|
||||
func (s *state) ApplyStatus(ctx context.Context, status models.LoopStatus) (
|
||||
outcome string, err error) {
|
||||
l.state.statusMu.Lock()
|
||||
defer l.state.statusMu.Unlock()
|
||||
existingStatus := l.state.status
|
||||
// prevent simultaneous loop changes by restricting
|
||||
// multiple SetStatus calls to run sequentially.
|
||||
s.loopMu.Lock()
|
||||
defer s.loopMu.Unlock()
|
||||
|
||||
// not a read lock as we want to modify it eventually in
|
||||
// the code below before any other call.
|
||||
s.statusMu.Lock()
|
||||
existingStatus := s.status
|
||||
|
||||
switch status {
|
||||
case constants.Running:
|
||||
switch existingStatus {
|
||||
case constants.Starting, constants.Running, constants.Stopping, constants.Crashed:
|
||||
return fmt.Sprintf("already %s", existingStatus), nil
|
||||
if existingStatus != constants.Stopped {
|
||||
return "already " + existingStatus.String(), nil
|
||||
}
|
||||
l.loopLock.Lock()
|
||||
defer l.loopLock.Unlock()
|
||||
l.state.status = constants.Starting
|
||||
l.state.statusMu.Unlock()
|
||||
l.start <- struct{}{}
|
||||
|
||||
s.status = constants.Starting
|
||||
s.statusMu.Unlock()
|
||||
s.start <- struct{}{}
|
||||
|
||||
// Wait for the loop to react to the start signal
|
||||
newStatus := constants.Starting // for canceled context
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
case newStatus = <-l.running:
|
||||
case newStatus = <-s.running:
|
||||
}
|
||||
l.state.statusMu.Lock()
|
||||
l.state.status = newStatus
|
||||
s.SetStatus(newStatus)
|
||||
|
||||
return newStatus.String(), nil
|
||||
case constants.Stopped:
|
||||
switch existingStatus {
|
||||
case constants.Starting, constants.Stopping, constants.Stopped, constants.Crashed:
|
||||
return fmt.Sprintf("already %s", existingStatus), nil
|
||||
if existingStatus != constants.Running {
|
||||
return "already " + existingStatus.String(), nil
|
||||
}
|
||||
l.loopLock.Lock()
|
||||
defer l.loopLock.Unlock()
|
||||
l.state.status = constants.Stopping
|
||||
l.state.statusMu.Unlock()
|
||||
l.stop <- struct{}{}
|
||||
|
||||
s.status = constants.Stopping
|
||||
s.statusMu.Unlock()
|
||||
s.stop <- struct{}{}
|
||||
|
||||
// Wait for the loop to react to the stop signal
|
||||
newStatus := constants.Stopping // for canceled context
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
case <-l.stopped:
|
||||
case <-s.stopped:
|
||||
newStatus = constants.Stopped
|
||||
}
|
||||
l.state.statusMu.Lock()
|
||||
l.state.status = newStatus
|
||||
return status.String(), nil
|
||||
s.SetStatus(newStatus)
|
||||
|
||||
return newStatus.String(), nil
|
||||
default:
|
||||
return "", fmt.Errorf("%w: %s: it can only be one of: %s, %s",
|
||||
ErrInvalidStatus, status, constants.Running, constants.Stopped)
|
||||
}
|
||||
}
|
||||
|
||||
func (l *looper) GetSettings() (settings configuration.OpenVPN) {
|
||||
l.state.settingsMu.RLock()
|
||||
defer l.state.settingsMu.RUnlock()
|
||||
return l.state.settings
|
||||
func (s *state) GetSettings() (settings configuration.OpenVPN) {
|
||||
s.settingsMu.RLock()
|
||||
defer s.settingsMu.RUnlock()
|
||||
return s.settings
|
||||
}
|
||||
|
||||
func (l *looper) SetSettings(ctx context.Context, settings configuration.OpenVPN) (
|
||||
func (s *state) SetSettings(ctx context.Context, settings configuration.OpenVPN) (
|
||||
outcome string) {
|
||||
l.state.settingsMu.Lock()
|
||||
settingsUnchanged := reflect.DeepEqual(l.state.settings, settings)
|
||||
s.settingsMu.Lock()
|
||||
defer s.settingsMu.Unlock()
|
||||
settingsUnchanged := reflect.DeepEqual(s.settings, settings)
|
||||
if settingsUnchanged {
|
||||
l.state.settingsMu.Unlock()
|
||||
return "settings left unchanged"
|
||||
}
|
||||
l.state.settings = settings
|
||||
_, _ = l.SetStatus(ctx, constants.Stopped)
|
||||
outcome, _ = l.SetStatus(ctx, constants.Running)
|
||||
s.settings = settings
|
||||
_, _ = s.ApplyStatus(ctx, constants.Stopped)
|
||||
outcome, _ = s.ApplyStatus(ctx, constants.Running)
|
||||
return outcome
|
||||
}
|
||||
|
||||
func (l *looper) GetServers() (servers models.AllServers) {
|
||||
l.state.allServersMu.RLock()
|
||||
defer l.state.allServersMu.RUnlock()
|
||||
return l.state.allServers
|
||||
func (s *state) GetServers() (servers models.AllServers) {
|
||||
s.allServersMu.RLock()
|
||||
defer s.allServersMu.RUnlock()
|
||||
return s.allServers
|
||||
}
|
||||
|
||||
func (l *looper) SetServers(servers models.AllServers) {
|
||||
l.state.allServersMu.Lock()
|
||||
defer l.state.allServersMu.Unlock()
|
||||
l.state.allServers = servers
|
||||
func (s *state) SetServers(servers models.AllServers) {
|
||||
s.allServersMu.Lock()
|
||||
defer s.allServersMu.Unlock()
|
||||
s.allServers = servers
|
||||
}
|
||||
|
||||
func (l *looper) GetPortForwarded() (port uint16) {
|
||||
l.state.portForwardedMu.RLock()
|
||||
defer l.state.portForwardedMu.RUnlock()
|
||||
return l.state.portForwarded
|
||||
func (s *state) GetPortForwarded() (port uint16) {
|
||||
s.portForwardedMu.RLock()
|
||||
defer s.portForwardedMu.RUnlock()
|
||||
return s.portForwarded
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user