Maint: loopstate package used in Openvpn state
This commit is contained in:
77
internal/loopstate/apply.go
Normal file
77
internal/loopstate/apply.go
Normal file
@@ -0,0 +1,77 @@
|
||||
package loopstate
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/constants"
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
)
|
||||
|
||||
type Applier interface {
|
||||
ApplyStatus(ctx context.Context, status models.LoopStatus) (
|
||||
outcome string, err error)
|
||||
}
|
||||
|
||||
var ErrInvalidStatus = errors.New("invalid status")
|
||||
|
||||
// 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) {
|
||||
// prevent simultaneous loop changes by restricting
|
||||
// multiple ApplyStatus 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:
|
||||
if existingStatus != constants.Stopped {
|
||||
return "already " + existingStatus.String(), nil
|
||||
}
|
||||
|
||||
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 = <-s.running:
|
||||
}
|
||||
s.SetStatus(newStatus)
|
||||
|
||||
return newStatus.String(), nil
|
||||
case constants.Stopped:
|
||||
if existingStatus != constants.Running {
|
||||
return "already " + existingStatus.String(), nil
|
||||
}
|
||||
|
||||
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 <-s.stopped:
|
||||
newStatus = constants.Stopped
|
||||
}
|
||||
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)
|
||||
}
|
||||
}
|
||||
14
internal/loopstate/get.go
Normal file
14
internal/loopstate/get.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package loopstate
|
||||
|
||||
import "github.com/qdm12/gluetun/internal/models"
|
||||
|
||||
type Getter interface {
|
||||
GetStatus() (status models.LoopStatus)
|
||||
}
|
||||
|
||||
// GetStatus gets the status thread safely.
|
||||
func (s *State) GetStatus() (status models.LoopStatus) {
|
||||
s.statusMu.RLock()
|
||||
defer s.statusMu.RUnlock()
|
||||
return s.status
|
||||
}
|
||||
9
internal/loopstate/lock.go
Normal file
9
internal/loopstate/lock.go
Normal file
@@ -0,0 +1,9 @@
|
||||
package loopstate
|
||||
|
||||
type Locker interface {
|
||||
Lock()
|
||||
Unlock()
|
||||
}
|
||||
|
||||
func (s *State) Lock() { s.loopMu.Lock() }
|
||||
func (s *State) Unlock() { s.loopMu.Unlock() }
|
||||
16
internal/loopstate/set.go
Normal file
16
internal/loopstate/set.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package loopstate
|
||||
|
||||
import "github.com/qdm12/gluetun/internal/models"
|
||||
|
||||
type Setter interface {
|
||||
SetStatus(status models.LoopStatus)
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
38
internal/loopstate/state.go
Normal file
38
internal/loopstate/state.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package loopstate
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
)
|
||||
|
||||
type Manager interface {
|
||||
Locker
|
||||
Getter
|
||||
Setter
|
||||
Applier
|
||||
}
|
||||
|
||||
func New(status models.LoopStatus,
|
||||
start chan<- struct{}, running <-chan models.LoopStatus,
|
||||
stop chan<- struct{}, stopped <-chan struct{}) *State {
|
||||
return &State{
|
||||
status: status,
|
||||
start: start,
|
||||
running: running,
|
||||
stop: stop,
|
||||
stopped: stopped,
|
||||
}
|
||||
}
|
||||
|
||||
type State struct {
|
||||
loopMu sync.RWMutex
|
||||
|
||||
status models.LoopStatus
|
||||
statusMu sync.RWMutex
|
||||
|
||||
start chan<- struct{}
|
||||
running <-chan models.LoopStatus
|
||||
stop chan<- struct{}
|
||||
stopped <-chan struct{}
|
||||
}
|
||||
Reference in New Issue
Block a user