Ensure schedule never runs with non-positive ticker

This commit is contained in:
Maycon Santos
2024-03-10 06:03:07 +01:00
parent 5a3d9e401f
commit 59cf4cd97b
2 changed files with 10 additions and 1 deletions

View File

@@ -36,7 +36,7 @@ type NetworkMap struct {
type Network struct {
Identifier string `json:"id"`
Net net.IPNet `gorm:"serializer:gob"`
Net net.IPNet `gorm:"serializer:json"`
Dns string
// Serial is an ID that increments by 1 when any change to the network happened (e.g. new peer has been added).
// Used to synchronize state to the client apps.

View File

@@ -85,6 +85,11 @@ func (wm *DefaultScheduler) Schedule(in time.Duration, ID string, job func() (ne
return
}
if in < time.Second {
log.Warnf("job for %s was scheduled to run in %s which is under 1s. Adjusting that.", ID, in.String())
in = time.Second
}
ticker := time.NewTicker(in)
wm.jobs[ID] = cancel
@@ -112,6 +117,10 @@ func (wm *DefaultScheduler) Schedule(in time.Duration, ID string, job func() (ne
}
// we need this comparison to avoid resetting the ticker with the same duration and missing the current elapsesed time
if runIn != in {
if runIn < time.Second {
log.Warnf("job for %s was rescheduled to run in %s which is under 1s. Adjusting that.", ID, runIn.String())
runIn = time.Second
}
ticker.Reset(runIn)
}
case <-cancel: