Skip to content

Commit fbd47f9

Browse files
committed
Define constants for version semantics
1 parent b380f49 commit fbd47f9

File tree

5 files changed

+46
-28
lines changed

5 files changed

+46
-28
lines changed

client/internal/engine.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ const (
7474
PeerConnectionTimeoutMax = 45000 // ms
7575
PeerConnectionTimeoutMin = 30000 // ms
7676
connInitLimit = 200
77+
// skipAutoUpdateVersion used as a placeholder for autoUpdateVersion in proto responses to indicate response contains no new updates
78+
skipAutoUpdateVersion = "skip"
79+
disableAutoUpdate = "disabled"
7780
)
7881

7982
var ErrResetConnection = fmt.Errorf("reset connection")
@@ -673,15 +676,17 @@ func (e *Engine) handleSync(update *mgmProto.SyncResponse) error {
673676
e.syncMsgMux.Lock()
674677
defer e.syncMsgMux.Unlock()
675678

676-
if e.updateManager == nil && update.GetAutoUpdateVersion() != "disabled" {
677-
e.updateManager = updatemanager.NewUpdateManager(e.statusRecorder)
678-
e.updateManager.Start(e.ctx)
679-
} else if e.updateManager != nil && update.GetAutoUpdateVersion() == "disabled" {
680-
e.updateManager.Stop()
681-
e.updateManager = nil
682-
}
683-
if e.updateManager != nil {
684-
e.updateManager.SetVersion(update.GetAutoUpdateVersion())
679+
if update.GetAutoUpdateVersion() != skipAutoUpdateVersion {
680+
if e.updateManager == nil && update.GetAutoUpdateVersion() != disableAutoUpdate {
681+
e.updateManager = updatemanager.NewUpdateManager(e.statusRecorder)
682+
e.updateManager.Start(e.ctx)
683+
} else if e.updateManager != nil && update.GetAutoUpdateVersion() == disableAutoUpdate {
684+
e.updateManager.Stop()
685+
e.updateManager = nil
686+
}
687+
if e.updateManager != nil {
688+
e.updateManager.SetVersion(update.GetAutoUpdateVersion())
689+
}
685690
}
686691
if update.GetNetbirdConfig() != nil {
687692
wCfg := update.GetNetbirdConfig()

client/internal/updatemanager/manager.go

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ type UpdateManager struct {
3434
cancel context.CancelFunc
3535
update *version.Update
3636

37-
expectedVersion string
38-
expectedVersionMutex sync.Mutex
37+
expectedVersion *v.Version
38+
updateToLatestVersion bool
39+
expectedVersionMutex sync.Mutex
3940
}
4041

4142
func NewUpdateManager(statusRecorder *peer.Status) *UpdateManager {
@@ -69,20 +70,30 @@ func (u *UpdateManager) Start(ctx context.Context) {
6970
go u.updateLoop(ctx)
7071
}
7172

72-
func (u *UpdateManager) SetVersion(v string) {
73+
func (u *UpdateManager) SetVersion(expectedVersion string) {
7374
if u.cancel == nil {
7475
log.Errorf("UpdateManager not started")
7576
return
7677
}
7778

7879
u.expectedVersionMutex.Lock()
7980
defer u.expectedVersionMutex.Unlock()
80-
if u.expectedVersion == v {
81-
return
81+
if expectedVersion == latestVersion {
82+
u.updateToLatestVersion = true
83+
u.expectedVersion = nil
84+
} else {
85+
expectedSemVer, err := v.NewVersion(expectedVersion)
86+
if err != nil {
87+
log.Errorf("Error parsing version: %v", err)
88+
return
89+
}
90+
if u.expectedVersion.Equal(expectedSemVer) {
91+
return
92+
}
93+
u.expectedVersion = expectedSemVer
94+
u.updateToLatestVersion = false
8295
}
8396

84-
u.expectedVersion = v
85-
8697
select {
8798
case u.mgmUpdateChan <- struct{}{}:
8899
default:
@@ -126,19 +137,17 @@ func (u *UpdateManager) handleUpdate(ctx context.Context) {
126137
u.expectedVersionMutex.Unlock()
127138

128139
// Resolve "latest" to actual version
129-
if expectedVersion == latestVersion {
140+
if u.updateToLatestVersion {
130141
if !u.isVersionAvailable() {
131142
log.Tracef("Latest version not fetched yet")
132143
return
133144
}
134145
updateVersion = u.update.LatestVersion()
146+
} else if u.expectedVersion != nil {
147+
updateVersion = expectedVersion
135148
} else {
136-
var err error
137-
updateVersion, err = v.NewSemver(expectedVersion)
138-
if err != nil {
139-
log.Errorf("Failed to parse latest version: %v", err)
140-
return
141-
}
149+
log.Debugf("No expected version information set")
150+
return
142151
}
143152

144153
if !u.shouldUpdate(updateVersion) {

management/server/account.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ const (
5252
peerSchedulerRetryInterval = 3 * time.Second
5353
emptyUserID = "empty user ID in claims"
5454
errorGettingDomainAccIDFmt = "error getting account ID by private domain: %v"
55+
// skipAutoUpdateVersion used as a placeholder for autoUpdateVersion in proto responses to indicate response contains no new updates
56+
skipAutoUpdateVersion = "skip"
5557
)
5658

5759
type userLoggedInOnce bool

management/server/http/handlers/accounts/accounts_handler.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ const (
2828
// MinNetworkBits is the minimum prefix length for IPv4 network ranges (e.g., /29 gives 8 addresses, /28 gives 16)
2929
MinNetworkBitsIPv4 = 28
3030
// MinNetworkBitsIPv6 is the minimum prefix length for IPv6 network ranges
31-
MinNetworkBitsIPv6 = 120
31+
MinNetworkBitsIPv6 = 120
32+
disableAutoUpdate = "disabled"
33+
autoUpdateLatestVersion = "latest"
3234
)
3335

3436
// handler is a handler that handles the server.Account HTTP endpoints
@@ -206,8 +208,8 @@ func (h *handler) updateAccountRequestSettings(req api.PutApiAccountsAccountIdJS
206208
}
207209
if req.Settings.AutoUpdateVersion != nil {
208210
_, err := goversion.NewSemver(*req.Settings.AutoUpdateVersion)
209-
if *req.Settings.AutoUpdateVersion == "latest" ||
210-
*req.Settings.AutoUpdateVersion == "disabled" ||
211+
if *req.Settings.AutoUpdateVersion == autoUpdateLatestVersion ||
212+
*req.Settings.AutoUpdateVersion == disableAutoUpdate ||
211213
err == nil {
212214
returnSettings.AutoUpdateVersion = *req.Settings.AutoUpdateVersion
213215
} else if *req.Settings.AutoUpdateVersion != "" {

management/server/token_mgr.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ func (m *TimeBasedAuthSecretsManager) pushNewTURNAndRelayTokens(ctx context.Cont
210210
NetbirdConfig: &proto.NetbirdConfig{
211211
Turns: turns,
212212
},
213-
AutoUpdateVersion: "skip",
213+
AutoUpdateVersion: skipAutoUpdateVersion,
214214
}
215215

216216
// workaround for the case when client is unable to handle turn and relay updates at different time
@@ -247,7 +247,7 @@ func (m *TimeBasedAuthSecretsManager) pushNewRelayTokens(ctx context.Context, ac
247247
},
248248
// omit Turns to avoid updates there
249249
},
250-
AutoUpdateVersion: "skip",
250+
AutoUpdateVersion: skipAutoUpdateVersion,
251251
}
252252

253253
m.extendNetbirdConfig(ctx, peerID, accountID, update)

0 commit comments

Comments
 (0)