Files
gluetun/internal/httpproxy/loop.go
Quentin McGaw 578ef768ab chore(all): return concrete types, accept interfaces
- Remove exported interfaces unused locally
- Define interfaces to accept arguments
- Return concrete types, not interfaces
2022-06-11 01:34:30 +00:00

78 lines
1.9 KiB
Go

// Package httpproxy defines an interface to run an HTTP(s) proxy server.
package httpproxy
import (
"context"
"time"
"github.com/qdm12/gluetun/internal/configuration/settings"
"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"
)
type Loop struct {
statusManager statusManager
state StateManager
// Other objects
logger Logger
// Internal channels and locks
running chan models.LoopStatus
stop, stopped chan struct{}
start chan struct{}
userTrigger bool
backoffTime time.Duration
}
type statusManager interface {
GetStatus() (status models.LoopStatus)
SetStatus(status models.LoopStatus)
ApplyStatus(ctx context.Context, status models.LoopStatus) (
outcome string, err error)
}
type StateManager interface {
GetSettings() settings.HTTPProxy
SetSettings(ctx context.Context, settings settings.HTTPProxy) (outcome string)
}
const defaultBackoffTime = 10 * time.Second
func NewLoop(logger Logger, settings settings.HTTPProxy) *Loop {
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 &Loop{
statusManager: statusManager,
state: state,
logger: logger,
start: start,
running: running,
stop: stop,
stopped: stopped,
userTrigger: true,
backoffTime: defaultBackoffTime,
}
}
func (l *Loop) logAndWait(ctx context.Context, err error) {
l.logger.Error(err.Error())
l.logger.Info("retrying in " + l.backoffTime.String())
timer := time.NewTimer(l.backoffTime)
l.backoffTime *= 2
select {
case <-timer.C:
case <-ctx.Done():
if !timer.Stop() {
<-timer.C
}
}
}