Files
gluetun/internal/httpproxy/loop.go

75 lines
1.7 KiB
Go
Raw Normal View History

2021-02-06 16:26:23 +00:00
// Package httpproxy defines an interface to run an HTTP(s) proxy server.
package httpproxy
import (
"context"
"time"
2021-02-06 11:05:50 -05:00
"github.com/qdm12/gluetun/internal/configuration"
"github.com/qdm12/gluetun/internal/constants"
2021-07-23 20:47:36 +00:00
"github.com/qdm12/gluetun/internal/httpproxy/state"
"github.com/qdm12/gluetun/internal/loopstate"
"github.com/qdm12/gluetun/internal/models"
)
var _ Looper = (*Loop)(nil)
type Looper interface {
Runner
loopstate.Getter
loopstate.Applier
SettingsGetSetter
}
type Loop struct {
2021-07-23 20:47:36 +00:00
statusManager loopstate.Manager
state state.Manager
// Other objects
logger Logger
// Internal channels and locks
running chan models.LoopStatus
stop, stopped chan struct{}
start chan struct{}
2021-07-26 01:42:37 +00:00
userTrigger bool
backoffTime time.Duration
}
const defaultBackoffTime = 10 * time.Second
func NewLoop(logger Logger, settings configuration.HTTPProxy) *Loop {
2021-07-23 20:47:36 +00:00
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{
2021-07-23 20:47:36 +00:00
statusManager: statusManager,
state: state,
logger: logger,
start: start,
running: running,
stop: stop,
stopped: stopped,
2021-07-26 01:42:37 +00:00
userTrigger: true,
2021-07-23 20:47:36 +00:00
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
}
}
}