TUN-8632: Delay checking auto-update by the provided frequency

Delaying the auto-update check timer to start after one full round of
the provided frequency reduces the chance of upgrading immediately
after starting.
This commit is contained in:
Devin Carr 2024-09-12 12:17:39 -07:00
parent 2484df1f81
commit cd8cb47866
1 changed files with 14 additions and 24 deletions

View File

@ -200,7 +200,6 @@ func loggedUpdate(log *zerolog.Logger, options updateOptions) UpdateOutcome {
type AutoUpdater struct { type AutoUpdater struct {
configurable *configurable configurable *configurable
listeners *gracenet.Net listeners *gracenet.Net
updateConfigChan chan *configurable
log *zerolog.Logger log *zerolog.Logger
} }
@ -214,7 +213,6 @@ func NewAutoUpdater(updateDisabled bool, freq time.Duration, listeners *gracenet
return &AutoUpdater{ return &AutoUpdater{
configurable: createUpdateConfig(updateDisabled, freq, log), configurable: createUpdateConfig(updateDisabled, freq, log),
listeners: listeners, listeners: listeners,
updateConfigChan: make(chan *configurable),
log: log, log: log,
} }
} }
@ -234,9 +232,17 @@ func createUpdateConfig(updateDisabled bool, freq time.Duration, log *zerolog.Lo
} }
} }
// Run will perodically check for cloudflared updates, download them, and then restart the current cloudflared process
// to use the new version. It delays the first update check by the configured frequency as to not attempt a
// download immediately and restart after starting (in the case that there is an upgrade available).
func (a *AutoUpdater) Run(ctx context.Context) error { func (a *AutoUpdater) Run(ctx context.Context) error {
ticker := time.NewTicker(a.configurable.freq) ticker := time.NewTicker(a.configurable.freq)
for { for {
select {
case <-ctx.Done():
return ctx.Err()
case <-ticker.C:
}
updateOutcome := loggedUpdate(a.log, updateOptions{updateDisabled: !a.configurable.enabled}) updateOutcome := loggedUpdate(a.log, updateOptions{updateDisabled: !a.configurable.enabled})
if updateOutcome.Updated { if updateOutcome.Updated {
buildInfo.CloudflaredVersion = updateOutcome.Version buildInfo.CloudflaredVersion = updateOutcome.Version
@ -256,23 +262,7 @@ func (a *AutoUpdater) Run(ctx context.Context) error {
} else if updateOutcome.UserMessage != "" { } else if updateOutcome.UserMessage != "" {
a.log.Warn().Msg(updateOutcome.UserMessage) a.log.Warn().Msg(updateOutcome.UserMessage)
} }
select {
case <-ctx.Done():
return ctx.Err()
case newConfigurable := <-a.updateConfigChan:
ticker.Stop()
a.configurable = newConfigurable
ticker = time.NewTicker(a.configurable.freq)
// Check if there is new version of cloudflared after receiving new AutoUpdaterConfigurable
case <-ticker.C:
} }
}
}
// Update is the method to pass new AutoUpdaterConfigurable to a running AutoUpdater. It is safe to be called concurrently
func (a *AutoUpdater) Update(updateDisabled bool, newFreq time.Duration) {
a.updateConfigChan <- createUpdateConfig(updateDisabled, newFreq, a.log)
} }
func isAutoupdateEnabled(log *zerolog.Logger, updateDisabled bool, updateFreq time.Duration) bool { func isAutoupdateEnabled(log *zerolog.Logger, updateDisabled bool, updateFreq time.Duration) bool {