Skip to content

Commit

Permalink
Fix #199 when ticker period is 0
Browse files Browse the repository at this point in the history
  • Loading branch information
qdm12 committed Jul 16, 2020
1 parent e852614 commit b4a4e44
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 30 deletions.
39 changes: 16 additions & 23 deletions internal/dns/loop.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,17 @@ type Looper interface {
}

type looper struct {
conf Configurator
settings settings.DNS
settingsMutex sync.RWMutex
logger logging.Logger
streamMerger command.StreamMerger
uid int
gid int
restart chan struct{}
start chan struct{}
stop chan struct{}
updateTicker chan struct{}
tickerReady bool
tickerReadyMutex sync.Mutex
conf Configurator
settings settings.DNS
settingsMutex sync.RWMutex
logger logging.Logger
streamMerger command.StreamMerger
uid int
gid int
restart chan struct{}
start chan struct{}
stop chan struct{}
updateTicker chan struct{}
}

func NewLooper(conf Configurator, settings settings.DNS, logger logging.Logger,
Expand Down Expand Up @@ -253,27 +251,22 @@ func (l *looper) fallbackToUnencryptedDNS() {
}

func (l *looper) RunRestartTicker(ctx context.Context) {
l.tickerReadyMutex.Lock()
l.tickerReady = true
l.tickerReadyMutex.Unlock()
var ticker *time.Ticker = nil
ticker := time.NewTicker(time.Hour)
settings := l.GetSettings()
if settings.UpdatePeriod > 0 {
ticker = time.NewTicker(settings.UpdatePeriod)
} else {
ticker.Stop()
}
for {
select {
case <-ctx.Done():
if ticker != nil {
ticker.Stop()
}
ticker.Stop()
return
case <-ticker.C:
l.restart <- struct{}{}
case <-l.updateTicker:
if ticker != nil {
ticker.Stop()
}
ticker.Stop()
period := l.GetSettings().UpdatePeriod
ticker = time.NewTicker(period)
}
Expand Down
12 changes: 5 additions & 7 deletions internal/publicip/loop.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,24 +125,22 @@ func (l *looper) Run(ctx context.Context) {
}

func (l *looper) RunRestartTicker(ctx context.Context) {
ticker := time.NewTicker(time.Hour)
period := l.GetPeriod()
var ticker *time.Ticker = nil
if period > 0 {
ticker = time.NewTicker(period)
} else {
ticker.Stop()
}
for {
select {
case <-ctx.Done():
if ticker != nil {
ticker.Stop()
}
ticker.Stop()
return
case <-ticker.C:
l.restart <- struct{}{}
case <-l.updateTicker:
if ticker != nil {
ticker.Stop()
}
ticker.Stop()
ticker = time.NewTicker(l.GetPeriod())
}
}
Expand Down

0 comments on commit b4a4e44

Please sign in to comment.