Maint: use loopstate for httpproxy
This commit is contained in:
@@ -3,11 +3,12 @@ package httpproxy
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"sync"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/qdm12/gluetun/internal/configuration"
|
"github.com/qdm12/gluetun/internal/configuration"
|
||||||
"github.com/qdm12/gluetun/internal/constants"
|
"github.com/qdm12/gluetun/internal/constants"
|
||||||
|
"github.com/qdm12/gluetun/internal/httpproxy/state"
|
||||||
|
"github.com/qdm12/gluetun/internal/loopstate"
|
||||||
"github.com/qdm12/gluetun/internal/models"
|
"github.com/qdm12/gluetun/internal/models"
|
||||||
"github.com/qdm12/golibs/logging"
|
"github.com/qdm12/golibs/logging"
|
||||||
)
|
)
|
||||||
@@ -23,11 +24,11 @@ type Looper interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type looper struct {
|
type looper struct {
|
||||||
state state
|
statusManager loopstate.Manager
|
||||||
|
state state.Manager
|
||||||
// Other objects
|
// Other objects
|
||||||
logger logging.Logger
|
logger logging.Logger
|
||||||
// Internal channels and locks
|
// Internal channels and locks
|
||||||
loopLock sync.Mutex
|
|
||||||
running chan models.LoopStatus
|
running chan models.LoopStatus
|
||||||
stop, stopped chan struct{}
|
stop, stopped chan struct{}
|
||||||
start chan struct{}
|
start chan struct{}
|
||||||
@@ -37,17 +38,24 @@ type looper struct {
|
|||||||
const defaultBackoffTime = 10 * time.Second
|
const defaultBackoffTime = 10 * time.Second
|
||||||
|
|
||||||
func NewLooper(logger logging.Logger, settings configuration.HTTPProxy) Looper {
|
func NewLooper(logger logging.Logger, settings configuration.HTTPProxy) Looper {
|
||||||
|
start := make(chan struct{})
|
||||||
|
running := make(chan models.LoopStatus)
|
||||||
|
stop := make(chan struct{})
|
||||||
|
stopped := make(chan struct{})
|
||||||
|
|
||||||
|
statusManager := loopstate.New(constants.Stopped,
|
||||||
|
start, running, stop, stopped)
|
||||||
|
state := state.New(statusManager, settings)
|
||||||
|
|
||||||
return &looper{
|
return &looper{
|
||||||
state: state{
|
statusManager: statusManager,
|
||||||
status: constants.Stopped,
|
state: state,
|
||||||
settings: settings,
|
logger: logger,
|
||||||
},
|
start: start,
|
||||||
logger: logger,
|
running: running,
|
||||||
start: make(chan struct{}),
|
stop: stop,
|
||||||
running: make(chan models.LoopStatus),
|
stopped: stopped,
|
||||||
stop: make(chan struct{}),
|
backoffTime: defaultBackoffTime,
|
||||||
stopped: make(chan struct{}),
|
|
||||||
backoffTime: defaultBackoffTime,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ func (l *looper) Run(ctx context.Context, done chan<- struct{}) {
|
|||||||
|
|
||||||
if l.GetSettings().Enabled {
|
if l.GetSettings().Enabled {
|
||||||
go func() {
|
go func() {
|
||||||
_, _ = l.SetStatus(ctx, constants.Running)
|
_, _ = l.statusManager.ApplyStatus(ctx, constants.Running)
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -40,7 +40,7 @@ func (l *looper) Run(ctx context.Context, done chan<- struct{}) {
|
|||||||
crashed = false
|
crashed = false
|
||||||
} else {
|
} else {
|
||||||
l.backoffTime = defaultBackoffTime
|
l.backoffTime = defaultBackoffTime
|
||||||
l.state.setStatusWithLock(constants.Running)
|
l.statusManager.SetStatus(constants.Running)
|
||||||
}
|
}
|
||||||
|
|
||||||
stayHere := true
|
stayHere := true
|
||||||
@@ -61,7 +61,7 @@ func (l *looper) Run(ctx context.Context, done chan<- struct{}) {
|
|||||||
<-errorCh
|
<-errorCh
|
||||||
l.stopped <- struct{}{}
|
l.stopped <- struct{}{}
|
||||||
case err := <-errorCh:
|
case err := <-errorCh:
|
||||||
l.state.setStatusWithLock(constants.Crashed)
|
l.statusManager.SetStatus(constants.Crashed)
|
||||||
l.logAndWait(ctx, err)
|
l.logAndWait(ctx, err)
|
||||||
crashed = true
|
crashed = true
|
||||||
stayHere = false
|
stayHere = false
|
||||||
|
|||||||
@@ -2,40 +2,15 @@ package httpproxy
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"reflect"
|
|
||||||
|
|
||||||
"github.com/qdm12/gluetun/internal/configuration"
|
"github.com/qdm12/gluetun/internal/configuration"
|
||||||
"github.com/qdm12/gluetun/internal/constants"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func (l *looper) GetSettings() (settings configuration.HTTPProxy) {
|
func (l *looper) GetSettings() (settings configuration.HTTPProxy) {
|
||||||
l.state.settingsMu.RLock()
|
return l.state.GetSettings()
|
||||||
defer l.state.settingsMu.RUnlock()
|
|
||||||
return l.state.settings
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *looper) SetSettings(ctx context.Context, settings configuration.HTTPProxy) (
|
func (l *looper) SetSettings(ctx context.Context, settings configuration.HTTPProxy) (
|
||||||
outcome string) {
|
outcome string) {
|
||||||
l.state.settingsMu.Lock()
|
return l.state.SetSettings(ctx, settings)
|
||||||
settingsUnchanged := reflect.DeepEqual(settings, l.state.settings)
|
|
||||||
if settingsUnchanged {
|
|
||||||
l.state.settingsMu.Unlock()
|
|
||||||
return "settings left unchanged"
|
|
||||||
}
|
|
||||||
newEnabled := settings.Enabled
|
|
||||||
previousEnabled := l.state.settings.Enabled
|
|
||||||
l.state.settings = settings
|
|
||||||
l.state.settingsMu.Unlock()
|
|
||||||
// Either restart or set changed status
|
|
||||||
switch {
|
|
||||||
case !newEnabled && !previousEnabled:
|
|
||||||
case newEnabled && previousEnabled:
|
|
||||||
_, _ = l.SetStatus(ctx, constants.Stopped)
|
|
||||||
_, _ = l.SetStatus(ctx, constants.Running)
|
|
||||||
case newEnabled && !previousEnabled:
|
|
||||||
_, _ = l.SetStatus(ctx, constants.Running)
|
|
||||||
case !newEnabled && previousEnabled:
|
|
||||||
_, _ = l.SetStatus(ctx, constants.Stopped)
|
|
||||||
}
|
|
||||||
return "settings updated"
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,15 +0,0 @@
|
|||||||
package httpproxy
|
|
||||||
|
|
||||||
import (
|
|
||||||
"sync"
|
|
||||||
|
|
||||||
"github.com/qdm12/gluetun/internal/configuration"
|
|
||||||
"github.com/qdm12/gluetun/internal/models"
|
|
||||||
)
|
|
||||||
|
|
||||||
type state struct {
|
|
||||||
status models.LoopStatus
|
|
||||||
settings configuration.HTTPProxy
|
|
||||||
statusMu sync.RWMutex
|
|
||||||
settingsMu sync.RWMutex
|
|
||||||
}
|
|
||||||
55
internal/httpproxy/state/settings.go
Normal file
55
internal/httpproxy/state/settings.go
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
package state
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"reflect"
|
||||||
|
|
||||||
|
"github.com/qdm12/gluetun/internal/configuration"
|
||||||
|
"github.com/qdm12/gluetun/internal/constants"
|
||||||
|
)
|
||||||
|
|
||||||
|
type SettingsGetterSetter interface {
|
||||||
|
SettingsGetter
|
||||||
|
SettingsSetter
|
||||||
|
}
|
||||||
|
|
||||||
|
type SettingsGetter interface {
|
||||||
|
GetSettings() (settings configuration.HTTPProxy)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *State) GetSettings() (settings configuration.HTTPProxy) {
|
||||||
|
s.settingsMu.RLock()
|
||||||
|
defer s.settingsMu.RUnlock()
|
||||||
|
return s.settings
|
||||||
|
}
|
||||||
|
|
||||||
|
type SettingsSetter interface {
|
||||||
|
SetSettings(ctx context.Context,
|
||||||
|
settings configuration.HTTPProxy) (outcome string)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *State) SetSettings(ctx context.Context,
|
||||||
|
settings configuration.HTTPProxy) (outcome string) {
|
||||||
|
s.settingsMu.Lock()
|
||||||
|
settingsUnchanged := reflect.DeepEqual(settings, s.settings)
|
||||||
|
if settingsUnchanged {
|
||||||
|
s.settingsMu.Unlock()
|
||||||
|
return "settings left unchanged"
|
||||||
|
}
|
||||||
|
newEnabled := settings.Enabled
|
||||||
|
previousEnabled := s.settings.Enabled
|
||||||
|
s.settings = settings
|
||||||
|
s.settingsMu.Unlock()
|
||||||
|
// Either restart or set changed status
|
||||||
|
switch {
|
||||||
|
case !newEnabled && !previousEnabled:
|
||||||
|
case newEnabled && previousEnabled:
|
||||||
|
_, _ = s.statusApplier.ApplyStatus(ctx, constants.Stopped)
|
||||||
|
_, _ = s.statusApplier.ApplyStatus(ctx, constants.Running)
|
||||||
|
case newEnabled && !previousEnabled:
|
||||||
|
_, _ = s.statusApplier.ApplyStatus(ctx, constants.Running)
|
||||||
|
case !newEnabled && previousEnabled:
|
||||||
|
_, _ = s.statusApplier.ApplyStatus(ctx, constants.Stopped)
|
||||||
|
}
|
||||||
|
return "settings updated"
|
||||||
|
}
|
||||||
@@ -2,75 +2,15 @@ package httpproxy
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/qdm12/gluetun/internal/constants"
|
|
||||||
"github.com/qdm12/gluetun/internal/models"
|
"github.com/qdm12/gluetun/internal/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (s *state) setStatusWithLock(status models.LoopStatus) {
|
|
||||||
s.statusMu.Lock()
|
|
||||||
defer s.statusMu.Unlock()
|
|
||||||
s.status = status
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *looper) GetStatus() (status models.LoopStatus) {
|
func (l *looper) GetStatus() (status models.LoopStatus) {
|
||||||
l.state.statusMu.RLock()
|
return l.statusManager.GetStatus()
|
||||||
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) (
|
func (l *looper) SetStatus(ctx context.Context, status models.LoopStatus) (
|
||||||
outcome string, err error) {
|
outcome string, err error) {
|
||||||
l.state.statusMu.Lock()
|
return l.statusManager.ApplyStatus(ctx, status)
|
||||||
defer l.state.statusMu.Unlock()
|
|
||||||
existingStatus := l.state.status
|
|
||||||
|
|
||||||
switch status {
|
|
||||||
case constants.Running:
|
|
||||||
switch existingStatus {
|
|
||||||
case constants.Starting, constants.Running, constants.Stopping, constants.Crashed:
|
|
||||||
return fmt.Sprintf("already %s", existingStatus), nil
|
|
||||||
}
|
|
||||||
l.loopLock.Lock()
|
|
||||||
defer l.loopLock.Unlock()
|
|
||||||
l.state.status = constants.Starting
|
|
||||||
l.state.statusMu.Unlock()
|
|
||||||
l.start <- struct{}{}
|
|
||||||
|
|
||||||
newStatus := constants.Starting // for canceled context
|
|
||||||
select {
|
|
||||||
case <-ctx.Done():
|
|
||||||
case newStatus = <-l.running:
|
|
||||||
}
|
|
||||||
l.state.statusMu.Lock()
|
|
||||||
l.state.status = newStatus
|
|
||||||
return newStatus.String(), nil
|
|
||||||
case constants.Stopped:
|
|
||||||
switch existingStatus {
|
|
||||||
case constants.Stopped, constants.Stopping, constants.Starting, constants.Crashed:
|
|
||||||
return fmt.Sprintf("already %s", existingStatus), nil
|
|
||||||
}
|
|
||||||
l.loopLock.Lock()
|
|
||||||
defer l.loopLock.Unlock()
|
|
||||||
l.state.status = constants.Stopping
|
|
||||||
l.state.statusMu.Unlock()
|
|
||||||
l.stop <- struct{}{}
|
|
||||||
|
|
||||||
newStatus := constants.Stopping // for canceled context
|
|
||||||
select {
|
|
||||||
case <-ctx.Done():
|
|
||||||
case <-l.stopped:
|
|
||||||
newStatus = constants.Stopped
|
|
||||||
}
|
|
||||||
l.state.statusMu.Lock()
|
|
||||||
l.state.status = newStatus
|
|
||||||
return status.String(), nil
|
|
||||||
default:
|
|
||||||
return "", fmt.Errorf("%w: %s: it can only be one of: %s, %s",
|
|
||||||
ErrInvalidStatus, status, constants.Running, constants.Stopped)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user