-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Modify client-side behavior #4563
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
b070304
e04b989
0d2ce56
436d740
d5ea408
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ import ( | |
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "github.com/netbirdio/netbird/client/internal/updatemanager" | ||
| "net" | ||
| "net/netip" | ||
| "runtime" | ||
|
|
@@ -272,6 +273,18 @@ func (c *ConnectClient) run(mobileDependency MobileDependency, runningChan chan | |
|
|
||
| c.engineMutex.Lock() | ||
| c.engine = NewEngine(engineCtx, cancel, signalClient, mgmClient, relayManager, engineConfig, mobileDependency, c.statusRecorder, checks) | ||
| if loginResp.PeerConfig != nil && loginResp.PeerConfig.AutoUpdate != nil { | ||
| if c.engine.updateManager == nil && loginResp.PeerConfig.AutoUpdate.Version != "disabled" { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Code duplication with the engine. Move this logic into a function |
||
| c.engine.updateManager = updatemanager.NewUpdateManager(c.statusRecorder, c.engine.stateManager) | ||
| c.engine.updateManager.StartWithTimeout(engineCtx, time.Minute) | ||
| } else if c.engine.updateManager != nil && loginResp.PeerConfig.AutoUpdate.Version == "disabled" { | ||
| c.engine.updateManager.Stop() | ||
| c.engine.updateManager = nil | ||
| } | ||
| if c.engine.updateManager != nil { | ||
| c.engine.updateManager.SetVersion(loginResp.PeerConfig.AutoUpdate.Version) | ||
| } | ||
| } | ||
| c.engine.SetSyncResponsePersistence(c.persistSyncResponse) | ||
| c.engineMutex.Unlock() | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ | |
| import ( | ||
| "context" | ||
| "fmt" | ||
| "github.com/netbirdio/netbird/client/internal/statemanager" | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please keep the order of the imported packages as usual
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And everywhere else. |
||
| "io" | ||
| "net/http" | ||
| "os" | ||
|
|
@@ -32,6 +33,15 @@ | |
| StartFetcher() | ||
| } | ||
|
|
||
| type UpdateState struct { | ||
| PreUpdateVersion string | ||
| TargetVersion string | ||
| } | ||
|
|
||
| func (u UpdateState) Name() string { | ||
| return "autoUpdate" | ||
| } | ||
|
|
||
| type UpdateManager struct { | ||
| lastTrigger time.Time | ||
| statusRecorder *peer.Status | ||
|
|
@@ -40,6 +50,7 @@ | |
| wg sync.WaitGroup | ||
| currentVersion string | ||
| updateFunc func(ctx context.Context, targetVersion string) error | ||
| stateManager *statemanager.Manager | ||
|
|
||
| cancel context.CancelFunc | ||
| update UpdateInterface | ||
|
|
@@ -49,15 +60,17 @@ | |
| expectedVersionMutex sync.Mutex | ||
| } | ||
|
|
||
| func NewUpdateManager(statusRecorder *peer.Status) *UpdateManager { | ||
| func NewUpdateManager(statusRecorder *peer.Status, stateManager *statemanager.Manager) *UpdateManager { | ||
| manager := &UpdateManager{ | ||
| statusRecorder: statusRecorder, | ||
| mgmUpdateChan: make(chan struct{}, 1), | ||
| updateChannel: make(chan struct{}, 1), | ||
| currentVersion: version.NetbirdVersion(), | ||
| updateFunc: triggerUpdate, | ||
| update: version.NewUpdate("nb/client"), | ||
| stateManager: stateManager, | ||
| } | ||
|
|
||
| return manager | ||
| } | ||
|
|
||
|
|
@@ -66,12 +79,35 @@ | |
| return u | ||
| } | ||
|
|
||
| func (u *UpdateManager) StartWithTimeout(ctx context.Context, timeout time.Duration) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What will happen to the fetcher after a timeout? It seems like it will never be freed
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since timeout will cancel the context, onContextCancel will handle fetcher cleanup |
||
| if u.cancel != nil { | ||
| log.Errorf("UpdateManager already started") | ||
| return | ||
| } | ||
|
|
||
| u.startInit(ctx) | ||
| ctx, cancel := context.WithTimeout(ctx, timeout) | ||
| u.cancel = cancel | ||
|
|
||
| u.wg.Add(1) | ||
| go u.updateLoop(ctx) | ||
| } | ||
|
|
||
| func (u *UpdateManager) Start(ctx context.Context) { | ||
| if u.cancel != nil { | ||
| log.Errorf("UpdateManager already started") | ||
| return | ||
| } | ||
|
|
||
| u.startInit(ctx) | ||
| ctx, cancel := context.WithCancel(ctx) | ||
| u.cancel = cancel | ||
|
|
||
| u.wg.Add(1) | ||
| go u.updateLoop(ctx) | ||
| } | ||
|
|
||
| func (u *UpdateManager) startInit(ctx context.Context) { | ||
| go u.update.StartFetcher() | ||
| u.update.SetDaemonVersion(u.currentVersion) | ||
| u.update.SetOnUpdateListener(func() { | ||
|
|
@@ -81,11 +117,31 @@ | |
| } | ||
| }) | ||
|
|
||
| ctx, cancel := context.WithCancel(ctx) | ||
| u.cancel = cancel | ||
|
|
||
| u.wg.Add(1) | ||
| go u.updateLoop(ctx) | ||
| u.stateManager.RegisterState(&UpdateState{}) | ||
| if err := u.stateManager.LoadState(&UpdateState{}); err != nil { | ||
| log.Warnf("failed to load state: %v", err) | ||
| return | ||
| } | ||
| if u.stateManager.GetState(&UpdateState{}) == nil { | ||
| return | ||
| } | ||
| updateState := u.stateManager.GetState(&UpdateState{}).(*UpdateState) | ||
| log.Warnf("autoUpdate state loaded, %v", *updateState) | ||
| if updateState.TargetVersion == u.currentVersion { | ||
| log.Warnf("published notification event") | ||
| u.statusRecorder.PublishEvent( | ||
| cProto.SystemEvent_INFO, | ||
| cProto.SystemEvent_SYSTEM, | ||
| "Auto-update completed", | ||
| fmt.Sprintf("Your NetBird Client was auto-updated to version %s", u.currentVersion), | ||
| nil, | ||
| ) | ||
| } | ||
| if err := u.stateManager.DeleteState(updateState); err != nil { | ||
| log.Warnf("failed to delete state: %v", err) | ||
| } else if err = u.stateManager.PersistState(ctx); err != nil { | ||
| log.Warnf("failed to persist state: %v", err) | ||
| } | ||
| } | ||
|
|
||
| func (u *UpdateManager) SetVersion(expectedVersion string) { | ||
|
|
@@ -132,12 +188,24 @@ | |
| u.wg.Wait() | ||
| } | ||
|
|
||
| func (u *UpdateManager) onContextCancel() { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You clean up updateManager with Stop and also with context cancellation. This means we have a race condition in the onContextCancel and Stop() functions.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added the locking mechanism from the original PR modifications |
||
| if u.cancel == nil { | ||
| return | ||
| } | ||
|
|
||
| if u.update != nil { | ||
| u.update.StopWatch() | ||
| u.update = nil | ||
| } | ||
| } | ||
|
|
||
| func (u *UpdateManager) updateLoop(ctx context.Context) { | ||
| defer u.wg.Done() | ||
|
|
||
| for { | ||
| select { | ||
| case <-ctx.Done(): | ||
| u.onContextCancel() | ||
| return | ||
| case <-u.mgmUpdateChan: | ||
| case <-u.updateChannel: | ||
|
|
@@ -189,9 +257,46 @@ | |
| nil, | ||
| ) | ||
|
|
||
| err := u.updateFunc(ctx, updateVersion.String()) | ||
| u.statusRecorder.PublishEvent( | ||
| cProto.SystemEvent_INFO, | ||
| cProto.SystemEvent_SYSTEM, | ||
| "", | ||
| "", | ||
| map[string]string{"progress_window": "show"}, | ||
| ) | ||
|
|
||
| updateState := UpdateState{ | ||
| PreUpdateVersion: u.currentVersion, | ||
| TargetVersion: updateVersion.String(), | ||
| } | ||
| err := u.stateManager.UpdateState(updateState) | ||
| if err != nil { | ||
| log.Warnf("failed to update state: %v", err) | ||
| } else { | ||
| err = u.stateManager.PersistState(ctx) | ||
| if err != nil { | ||
| log.Warnf("failed to persist state: %v", err) | ||
| } | ||
| } | ||
|
|
||
| err = u.updateFunc(ctx, updateVersion.String()) | ||
|
|
||
| if err != nil { | ||
| log.Errorf("Error triggering auto-update: %v", err) | ||
| u.statusRecorder.PublishEvent( | ||
| cProto.SystemEvent_ERROR, | ||
| cProto.SystemEvent_SYSTEM, | ||
| "Auto-update failed", | ||
| fmt.Sprintf("Auto-update failed: %v", err), | ||
| nil, | ||
| ) | ||
| u.statusRecorder.PublishEvent( | ||
| cProto.SystemEvent_INFO, | ||
| cProto.SystemEvent_SYSTEM, | ||
| "", | ||
| "", | ||
| map[string]string{"progress_window": "hide"}, | ||
| ) | ||
| } | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where does this string come from? It should be const