Maint: split httpproxy files

This commit is contained in:
Quentin McGaw (desktop)
2021-07-23 19:25:48 +00:00
parent 54610866f2
commit 762507855e
5 changed files with 189 additions and 168 deletions

View File

@@ -3,7 +3,6 @@ package httpproxy
import (
"context"
"fmt"
"sync"
"time"
@@ -52,70 +51,6 @@ func NewLooper(logger logging.Logger, settings configuration.HTTPProxy) Looper {
}
}
func (l *looper) Run(ctx context.Context, done chan<- struct{}) {
defer close(done)
crashed := false
if l.GetSettings().Enabled {
go func() {
_, _ = l.SetStatus(ctx, constants.Running)
}()
}
select {
case <-l.start:
case <-ctx.Done():
return
}
for ctx.Err() == nil {
runCtx, runCancel := context.WithCancel(ctx)
settings := l.GetSettings()
address := fmt.Sprintf(":%d", settings.Port)
server := New(runCtx, address, l.logger, settings.Stealth, settings.Log, settings.User, settings.Password)
errorCh := make(chan error)
go server.Run(runCtx, errorCh)
// TODO stable timer, check Shadowsocks
if !crashed {
l.running <- constants.Running
crashed = false
} else {
l.backoffTime = defaultBackoffTime
l.state.setStatusWithLock(constants.Running)
}
stayHere := true
for stayHere {
select {
case <-ctx.Done():
runCancel()
<-errorCh
return
case <-l.start:
l.logger.Info("starting")
runCancel()
<-errorCh
stayHere = false
case <-l.stop:
l.logger.Info("stopping")
runCancel()
<-errorCh
l.stopped <- struct{}{}
case err := <-errorCh:
l.state.setStatusWithLock(constants.Crashed)
l.logAndWait(ctx, err)
crashed = true
stayHere = false
}
}
runCancel() // repetition for linter only
}
}
func (l *looper) logAndWait(ctx context.Context, err error) {
l.logger.Error(err.Error())
l.logger.Info("retrying in " + l.backoffTime.String())