Fix: public IP loop being stuck

This commit is contained in:
Quentin McGaw (desktop)
2021-07-26 01:35:43 +00:00
parent 8105437815
commit ab910403c6
2 changed files with 12 additions and 11 deletions

View File

@@ -40,6 +40,7 @@ type Loop struct {
stopped chan struct{} stopped chan struct{}
updateTicker chan struct{} updateTicker chan struct{}
backoffTime time.Duration backoffTime time.Duration
userTrigger bool
// Mock functions // Mock functions
timeNow func() time.Time timeNow func() time.Time
} }
@@ -66,11 +67,12 @@ func NewLoop(client *http.Client, logger logging.Logger,
logger: logger, logger: logger,
puid: puid, puid: puid,
pgid: pgid, pgid: pgid,
start: make(chan struct{}), start: start,
running: make(chan models.LoopStatus), running: running,
stop: make(chan struct{}), stop: stop,
stopped: make(chan struct{}), stopped: stopped,
updateTicker: make(chan struct{}), updateTicker: updateTicker,
userTrigger: true,
backoffTime: defaultBackoffTime, backoffTime: defaultBackoffTime,
timeNow: time.Now, timeNow: time.Now,
} }

View File

@@ -15,8 +15,6 @@ 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
select { select {
case <-l.start: case <-l.start:
case <-ctx.Done(): case <-ctx.Done():
@@ -40,10 +38,10 @@ func (l *Loop) Run(ctx context.Context, done chan<- struct{}) {
ipCh <- ip ipCh <- ip
}() }()
if !crashed { if l.userTrigger {
l.userTrigger = false
l.running <- constants.Running l.running <- constants.Running
crashed = false } else { // crash
} else {
l.backoffTime = defaultBackoffTime l.backoffTime = defaultBackoffTime
l.statusManager.SetStatus(constants.Running) l.statusManager.SetStatus(constants.Running)
} }
@@ -61,9 +59,11 @@ func (l *Loop) Run(ctx context.Context, done chan<- struct{}) {
} }
return return
case <-l.start: case <-l.start:
l.userTrigger = true
getCancel() getCancel()
stayHere = false stayHere = false
case <-l.stop: case <-l.stop:
l.userTrigger = true
l.logger.Info("stopping") l.logger.Info("stopping")
getCancel() getCancel()
<-errorCh <-errorCh
@@ -92,7 +92,6 @@ func (l *Loop) Run(ctx context.Context, done chan<- struct{}) {
close(ipCh) close(ipCh)
l.statusManager.SetStatus(constants.Crashed) l.statusManager.SetStatus(constants.Crashed)
l.logAndWait(ctx, err) l.logAndWait(ctx, err)
crashed = true
stayHere = false stayHere = false
} }
} }