Maint: improve http proxy loop Run
This commit is contained in:
@@ -31,6 +31,7 @@ type Loop struct {
|
|||||||
running chan models.LoopStatus
|
running chan models.LoopStatus
|
||||||
stop, stopped chan struct{}
|
stop, stopped chan struct{}
|
||||||
start chan struct{}
|
start chan struct{}
|
||||||
|
userTrigger bool
|
||||||
backoffTime time.Duration
|
backoffTime time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -54,6 +55,7 @@ func NewLoop(logger logging.Logger, settings configuration.HTTPProxy) *Loop {
|
|||||||
running: running,
|
running: running,
|
||||||
stop: stop,
|
stop: stop,
|
||||||
stopped: stopped,
|
stopped: stopped,
|
||||||
|
userTrigger: true,
|
||||||
backoffTime: defaultBackoffTime,
|
backoffTime: defaultBackoffTime,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ package httpproxy
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"strconv"
|
||||||
|
|
||||||
"github.com/qdm12/gluetun/internal/constants"
|
"github.com/qdm12/gluetun/internal/constants"
|
||||||
)
|
)
|
||||||
@@ -14,34 +14,28 @@ type Runner interface {
|
|||||||
func (l *Loop) Run(ctx context.Context, done chan<- struct{}) {
|
func (l *Loop) Run(ctx context.Context, done chan<- struct{}) {
|
||||||
defer close(done)
|
defer close(done)
|
||||||
|
|
||||||
crashed := false
|
if !l.state.GetSettings().Enabled {
|
||||||
|
|
||||||
if l.state.GetSettings().Enabled {
|
|
||||||
go func() {
|
|
||||||
_, _ = l.statusManager.ApplyStatus(ctx, constants.Running)
|
|
||||||
}()
|
|
||||||
}
|
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case <-l.start:
|
case <-l.start:
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for ctx.Err() == nil {
|
for ctx.Err() == nil {
|
||||||
runCtx, runCancel := context.WithCancel(ctx)
|
runCtx, runCancel := context.WithCancel(ctx)
|
||||||
|
|
||||||
settings := l.state.GetSettings()
|
settings := l.state.GetSettings()
|
||||||
address := fmt.Sprintf(":%d", settings.Port)
|
address := ":" + strconv.Itoa(int(settings.Port))
|
||||||
server := New(runCtx, address, l.logger, settings.Stealth, settings.Log, settings.User, settings.Password)
|
server := New(runCtx, address, l.logger, settings.Stealth, settings.Log, settings.User, settings.Password)
|
||||||
|
|
||||||
errorCh := make(chan error)
|
errorCh := make(chan error)
|
||||||
go server.Run(runCtx, errorCh)
|
go server.Run(runCtx, errorCh)
|
||||||
|
|
||||||
// TODO stable timer, check Shadowsocks
|
// TODO stable timer, check Shadowsocks
|
||||||
if !crashed {
|
if l.userTrigger {
|
||||||
l.running <- constants.Running
|
l.running <- constants.Running
|
||||||
crashed = false
|
l.userTrigger = false
|
||||||
} else {
|
} else {
|
||||||
l.backoffTime = defaultBackoffTime
|
l.backoffTime = defaultBackoffTime
|
||||||
l.statusManager.SetStatus(constants.Running)
|
l.statusManager.SetStatus(constants.Running)
|
||||||
@@ -53,21 +47,26 @@ func (l *Loop) Run(ctx context.Context, done chan<- struct{}) {
|
|||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
runCancel()
|
runCancel()
|
||||||
<-errorCh
|
<-errorCh
|
||||||
|
close(errorCh)
|
||||||
return
|
return
|
||||||
case <-l.start:
|
case <-l.start:
|
||||||
|
l.userTrigger = true
|
||||||
l.logger.Info("starting")
|
l.logger.Info("starting")
|
||||||
runCancel()
|
runCancel()
|
||||||
<-errorCh
|
<-errorCh
|
||||||
|
close(errorCh)
|
||||||
stayHere = false
|
stayHere = false
|
||||||
case <-l.stop:
|
case <-l.stop:
|
||||||
|
l.userTrigger = true
|
||||||
l.logger.Info("stopping")
|
l.logger.Info("stopping")
|
||||||
runCancel()
|
runCancel()
|
||||||
<-errorCh
|
<-errorCh
|
||||||
|
// Do not close errorCh or this for loop won't work
|
||||||
l.stopped <- struct{}{}
|
l.stopped <- struct{}{}
|
||||||
case err := <-errorCh:
|
case err := <-errorCh:
|
||||||
|
close(errorCh)
|
||||||
l.statusManager.SetStatus(constants.Crashed)
|
l.statusManager.SetStatus(constants.Crashed)
|
||||||
l.logAndWait(ctx, err)
|
l.logAndWait(ctx, err)
|
||||||
crashed = true
|
|
||||||
stayHere = false
|
stayHere = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user