diff --git a/internal/portforward/interfaces.go b/internal/portforward/interfaces.go index 45191f7e..148eb567 100644 --- a/internal/portforward/interfaces.go +++ b/internal/portforward/interfaces.go @@ -32,6 +32,7 @@ type Logger interface { Info(s string) Warn(s string) Error(s string) + Errorf(format string, args ...any) } type Cmder interface { diff --git a/internal/portforward/loop.go b/internal/portforward/loop.go index e866bea9..93e114bb 100644 --- a/internal/portforward/loop.go +++ b/internal/portforward/loop.go @@ -6,6 +6,7 @@ import ( "fmt" "net/http" "sync" + "time" "github.com/qdm12/gluetun/internal/configuration/settings" "github.com/qdm12/gluetun/internal/portforward/service" @@ -87,6 +88,8 @@ func (l *Loop) run(runCtx context.Context, runDone chan<- struct{}, defer close(runDone) var serviceRunError <-chan error + var retryAfter <-chan time.Time + const retryDelay = 5 * time.Second for { updateReceived := false select { @@ -105,10 +108,12 @@ func (l *Loop) run(runCtx context.Context, runDone chan<- struct{}, l.settingsMutex.Unlock() case err := <-serviceRunError: l.logger.Error(err.Error()) + case <-retryAfter: + // Retry starting the service after a delay + retryAfter = nil } - firstRun := serviceRunError == nil - if !firstRun { + if l.service != nil { err := l.service.Stop() if err != nil { runErrorCh <- fmt.Errorf("stopping previous service: %w", err) @@ -132,6 +137,10 @@ func (l *Loop) run(runCtx context.Context, runDone chan<- struct{}, err = fmt.Errorf("starting port forwarding service: %w", err) } updateResult <- err + } else if err != nil { + // Log the error and schedule a retry + l.logger.Errorf("starting port forwarding service: %s - retrying in %s", err, retryDelay) + retryAfter = time.After(retryDelay) } } }