@@ -198,10 +198,9 @@ func loggedUpdate(log *zerolog.Logger, options updateOptions) UpdateOutcome {
198198
199199// AutoUpdater periodically checks for new version of cloudflared.
200200type AutoUpdater struct {
201- configurable * configurable
202- listeners * gracenet.Net
203- updateConfigChan chan * configurable
204- log * zerolog.Logger
201+ configurable * configurable
202+ listeners * gracenet.Net
203+ log * zerolog.Logger
205204}
206205
207206// AutoUpdaterConfigurable is the attributes of AutoUpdater that can be reconfigured during runtime
@@ -212,10 +211,9 @@ type configurable struct {
212211
213212func NewAutoUpdater (updateDisabled bool , freq time.Duration , listeners * gracenet.Net , log * zerolog.Logger ) * AutoUpdater {
214213 return & AutoUpdater {
215- configurable : createUpdateConfig (updateDisabled , freq , log ),
216- listeners : listeners ,
217- updateConfigChan : make (chan * configurable ),
218- log : log ,
214+ configurable : createUpdateConfig (updateDisabled , freq , log ),
215+ listeners : listeners ,
216+ log : log ,
219217 }
220218}
221219
@@ -234,9 +232,17 @@ func createUpdateConfig(updateDisabled bool, freq time.Duration, log *zerolog.Lo
234232 }
235233}
236234
235+ // Run will perodically check for cloudflared updates, download them, and then restart the current cloudflared process
236+ // to use the new version. It delays the first update check by the configured frequency as to not attempt a
237+ // download immediately and restart after starting (in the case that there is an upgrade available).
237238func (a * AutoUpdater ) Run (ctx context.Context ) error {
238239 ticker := time .NewTicker (a .configurable .freq )
239240 for {
241+ select {
242+ case <- ctx .Done ():
243+ return ctx .Err ()
244+ case <- ticker .C :
245+ }
240246 updateOutcome := loggedUpdate (a .log , updateOptions {updateDisabled : ! a .configurable .enabled })
241247 if updateOutcome .Updated {
242248 buildInfo .CloudflaredVersion = updateOutcome .Version
@@ -256,25 +262,9 @@ func (a *AutoUpdater) Run(ctx context.Context) error {
256262 } else if updateOutcome .UserMessage != "" {
257263 a .log .Warn ().Msg (updateOutcome .UserMessage )
258264 }
259-
260- select {
261- case <- ctx .Done ():
262- return ctx .Err ()
263- case newConfigurable := <- a .updateConfigChan :
264- ticker .Stop ()
265- a .configurable = newConfigurable
266- ticker = time .NewTicker (a .configurable .freq )
267- // Check if there is new version of cloudflared after receiving new AutoUpdaterConfigurable
268- case <- ticker .C :
269- }
270265 }
271266}
272267
273- // Update is the method to pass new AutoUpdaterConfigurable to a running AutoUpdater. It is safe to be called concurrently
274- func (a * AutoUpdater ) Update (updateDisabled bool , newFreq time.Duration ) {
275- a .updateConfigChan <- createUpdateConfig (updateDisabled , newFreq , a .log )
276- }
277-
278268func isAutoupdateEnabled (log * zerolog.Logger , updateDisabled bool , updateFreq time.Duration ) bool {
279269 if ! supportAutoUpdate (log ) {
280270 return false
0 commit comments