Skip to content

Commit 0f0202c

Browse files
committed
Restructure version.Update to use channel
1 parent bb7da18 commit 0f0202c

File tree

5 files changed

+52
-25
lines changed

5 files changed

+52
-25
lines changed

client/internal/updatemanager/manager.go

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ type UpdateManager struct {
3535
lastTrigger time.Time
3636
statusRecorder *peer.Status
3737
mutex sync.Mutex
38-
waitGroup sync.WaitGroup
38+
updateChannel chan string
39+
doneChannel chan struct{}
3940
}
4041

4142
func NewUpdateManager(ctx context.Context, statusRecorder *peer.Status) *UpdateManager {
@@ -49,9 +50,12 @@ func NewUpdateManager(ctx context.Context, statusRecorder *peer.Status) *UpdateM
4950
cancel: cancel,
5051
version: disableAutoUpdate,
5152
latestVersion: unknownVersion,
53+
updateChannel: make(chan string, 4),
54+
doneChannel: make(chan struct{}),
5255
}
5356
update.SetDaemonVersion(version.NetbirdVersion())
54-
update.SetOnUpdateListener(manager.Updated)
57+
update.SetOnUpdateChannel(manager.updateChannel)
58+
go manager.UpdateLoop()
5559
return manager
5660
}
5761

@@ -61,7 +65,7 @@ func (u *UpdateManager) SetVersion(v string) {
6165
log.Tracef("Auto-update version set to %s", v)
6266
u.version = v
6367
u.mutex.Unlock()
64-
go u.Updated(unknownVersion)
68+
u.updateChannel <- unknownVersion
6569
} else {
6670
u.mutex.Unlock()
6771
}
@@ -75,20 +79,26 @@ func (u *UpdateManager) Stop() {
7579
u.update.StopWatch()
7680
u.update = nil
7781
}
78-
u.waitGroup.Wait()
82+
<-u.doneChannel
7983
}
8084

81-
func (u *UpdateManager) Updated(latestVersion string) {
82-
u.waitGroup.Add(1)
83-
defer u.waitGroup.Done()
84-
u.mutex.Lock()
85-
defer u.mutex.Unlock()
86-
if latestVersion != unknownVersion {
87-
u.latestVersion = latestVersion
85+
func (u *UpdateManager) UpdateLoop() {
86+
for {
87+
select {
88+
case <-u.ctx.Done():
89+
u.doneChannel <- struct{}{}
90+
return
91+
case latestVersion := <-u.updateChannel:
92+
u.mutex.Lock()
93+
if latestVersion != unknownVersion {
94+
u.latestVersion = latestVersion
95+
}
96+
u.mutex.Unlock()
97+
ctx, cancel := context.WithDeadline(u.ctx, time.Now().Add(time.Minute))
98+
u.CheckForUpdates(ctx)
99+
cancel()
100+
}
88101
}
89-
ctx, cancel := context.WithDeadline(u.ctx, time.Now().Add(time.Minute))
90-
defer cancel()
91-
u.CheckForUpdates(ctx)
92102
}
93103

94104
func (u *UpdateManager) CheckForUpdates(ctx context.Context) {

client/ui/client_ui.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1176,7 +1176,7 @@ func protoConfigToConfig(cfg *proto.GetConfigResponse) *profilemanager.Config {
11761176
return &config
11771177
}
11781178

1179-
func (s *serviceClient) onUpdateAvailable(_ string) {
1179+
func (s *serviceClient) onUpdateAvailable() {
11801180
s.updateIndicationLock.Lock()
11811181
defer s.updateIndicationLock.Unlock()
11821182

management/internals/server/server.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,8 @@ func (s *BaseServer) Start(ctx context.Context) error {
182182

183183
s.update = version.NewUpdate("nb/management")
184184
s.update.SetDaemonVersion(version.NetbirdVersion())
185-
s.update.SetOnUpdateListener(func(newVersion string) {
186-
log.WithContext(ctx).Infof("your management version, \"%s\", is outdated, a new management version (%s) is available. Learn more here: https://github.com/netbirdio/netbird/releases", version.NetbirdVersion(), newVersion)
185+
s.update.SetOnUpdateListener(func() {
186+
log.WithContext(ctx).Infof("your management version, \"%s\", is outdated, a new management version is available. Learn more here: https://github.com/netbirdio/netbird/releases", version.NetbirdVersion())
187187
})
188188

189189
return nil

version/update.go

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ type Update struct {
3030
fetchTicker *time.Ticker
3131
fetchDone chan struct{}
3232

33-
onUpdateListener func(latestVersion string)
33+
onUpdateListener func()
34+
onUpdateChannel chan string
3435
listenerLock sync.Mutex
3536
}
3637

@@ -57,7 +58,11 @@ func NewUpdate(httpAgent string) *Update {
5758
// StopWatch stop the version info fetch loop
5859
func (u *Update) StopWatch() {
5960
u.fetchTicker.Stop()
60-
u.fetchDone <- struct{}{}
61+
62+
select {
63+
case u.fetchDone <- struct{}{}:
64+
default:
65+
}
6166
}
6267

6368
// SetDaemonVersion update the currently running daemon version. If new version is available it will trigger
@@ -80,15 +85,24 @@ func (u *Update) SetDaemonVersion(newVersion string) bool {
8085
}
8186

8287
// SetOnUpdateListener set new update listener
83-
func (u *Update) SetOnUpdateListener(updateFn func(string)) {
88+
func (u *Update) SetOnUpdateListener(updateFn func()) {
8489
u.listenerLock.Lock()
8590
defer u.listenerLock.Unlock()
8691

8792
u.onUpdateListener = updateFn
93+
if u.isUpdateAvailable() {
94+
u.onUpdateListener()
95+
}
96+
}
97+
98+
func (u *Update) SetOnUpdateChannel(updateChannel chan string) {
99+
u.listenerLock.Lock()
100+
defer u.listenerLock.Unlock()
101+
u.onUpdateChannel = updateChannel
88102
if u.isUpdateAvailable() {
89103
u.versionsLock.Lock()
90104
defer u.versionsLock.Unlock()
91-
u.onUpdateListener(u.latestAvailable.String())
105+
u.onUpdateChannel <- u.latestAvailable.String()
92106
}
93107
}
94108

@@ -167,11 +181,14 @@ func (u *Update) checkUpdate() bool {
167181

168182
u.listenerLock.Lock()
169183
defer u.listenerLock.Unlock()
184+
if u.onUpdateChannel != nil {
185+
u.onUpdateChannel <- u.latestAvailable.String()
186+
}
170187
if u.onUpdateListener == nil {
171188
return true
172189
}
173190

174-
go u.onUpdateListener(u.latestAvailable.String())
191+
go u.onUpdateListener()
175192
return true
176193
}
177194

version/update_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func TestNewUpdate(t *testing.T) {
2525
onUpdate := false
2626
u := NewUpdate(httpAgent)
2727
defer u.StopWatch()
28-
u.SetOnUpdateListener(func(_ string) {
28+
u.SetOnUpdateListener(func() {
2929
onUpdate = true
3030
wg.Done()
3131
})
@@ -50,7 +50,7 @@ func TestDoNotUpdate(t *testing.T) {
5050
onUpdate := false
5151
u := NewUpdate(httpAgent)
5252
defer u.StopWatch()
53-
u.SetOnUpdateListener(func(_ string) {
53+
u.SetOnUpdateListener(func() {
5454
onUpdate = true
5555
wg.Done()
5656
})
@@ -75,7 +75,7 @@ func TestDaemonUpdate(t *testing.T) {
7575
onUpdate := false
7676
u := NewUpdate(httpAgent)
7777
defer u.StopWatch()
78-
u.SetOnUpdateListener(func(_ string) {
78+
u.SetOnUpdateListener(func() {
7979
onUpdate = true
8080
wg.Done()
8181
})

0 commit comments

Comments
 (0)