Skip to content

Commit 4be05fb

Browse files
xmlqiuzhiqian
authored andcommitted
Fix: Repair randomized timer for first-time intranet updates
* When switching from public network to intranet, force-refresh the timer with a randomized value. * Use a randomized value for the initial timer upon each restart, then switch to fixed values for subsequent timer initiations Bug: https://pms.uniontech.com/bug-view-355587.html
1 parent 28cf85b commit 4be05fb

4 files changed

Lines changed: 67 additions & 23 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
lastoreAutocheck.timer定时器跟两个设定值有关系:CheckInterval和StartCheckRange有密切关系
2+
- CheckInterval是一个固定值
3+
- 是一个范围值,用来在这个返回生成一个随机值
4+
5+
公网行为:
6+
系统第一次开机(装完机后第一次触发检查更新前都算,主要依据就是lastCheckTime的值,如果是一个空值,则认为是第一次启动),则直接使用随机值来设定定时器,之后都使用固定值+随机值的组合来设置定时器,关机不影响时间计算。意思就是说,下次启动后,会与上次检查的时间进行比较,不足时间则补足剩余时间+随机值作为这次定时器,>=7天则直接使用随机值作为定时器。
7+
8+
私网行为:
9+
- 每次系统启动的时候第一次定时器是一个StartCheckRange中的随机值,后面按照固定值CheckInterval轮询。
10+
- 当从公网切换到私网的时候,需要当成第一次启动来处理,因此需要设置一个标志来表明当前是第一次启动。

src/internal/config/config.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ const (
221221
dSettingsKeySecurityRepoType = "security-repo-type"
222222
dSettingsKeyPlatformRepoComponents = "platform-repo-components"
223223
DSettingsKeyIncrementalUpdate = "incremental-update"
224-
dSettingsKeyIntranetUpdate = "intranet-update"
224+
DSettingsKeyIntranetUpdate = "intranet-update"
225225
dSettingsKeyGetHardwareIdByHelper = "hardware-id-from-helper"
226226
dSettingsKeyDeliveryRemoteDownloadGlobalLimit = "delivery-remote-download-global-limit"
227227
dSettingsKeyDeliveryRemoteUploadGlobalLimit = "delivery-remote-upload-global-limit"
@@ -309,7 +309,7 @@ func getConfigFromDSettings() *Config {
309309
c.IncrementalUpdate = v.Value().(bool)
310310
}
311311

312-
v, err = c.dsLastoreManager.Value(0, dSettingsKeyIntranetUpdate)
312+
v, err = c.dsLastoreManager.Value(0, DSettingsKeyIntranetUpdate)
313313
if err != nil {
314314
logger.Warning(err)
315315
} else {
@@ -793,12 +793,20 @@ func getConfigFromDSettings() *Config {
793793
_, err = c.dsLastoreManager.ConnectValueChanged(func(key string) {
794794
logger.Infof("config update: key=%s", key)
795795
switch key {
796-
case dSettingsKeyIntranetUpdate:
797-
v, err = c.dsLastoreManager.Value(0, dSettingsKeyIntranetUpdate)
796+
case DSettingsKeyIntranetUpdate:
797+
v, err = c.dsLastoreManager.Value(0, DSettingsKeyIntranetUpdate)
798798
if err != nil {
799799
logger.Warning(err)
800800
} else {
801-
c.IntranetUpdate = v.Value().(bool)
801+
oldValue := c.PlatformUpdate
802+
newValue := v.Value().(bool)
803+
c.IntranetUpdate = newValue
804+
c.dsettingsChangedCbMapMu.Lock()
805+
cb := c.dsettingsChangedCbMap[key]
806+
if cb != nil {
807+
go cb(oldValue, newValue)
808+
}
809+
c.dsettingsChangedCbMapMu.Unlock()
802810
}
803811
case DSettingsKeyPlatformUpdate:
804812
v, err = c.dsLastoreManager.Value(0, DSettingsKeyPlatformUpdate)
@@ -1073,6 +1081,11 @@ func (c *Config) SetAllowInstallRemovePkgExecPaths(paths []string) error {
10731081
// return c.save(dSettingsKeyNeedDownloadSize, size)
10741082
// }
10751083

1084+
func (c *Config) SetLastCheckTime(lastCheckTime time.Time) error {
1085+
c.LastCheckTime = lastCheckTime
1086+
return c.save(dSettingsKeyLastCheckTime, c.LastCheckTime.Format(configTimeLayout))
1087+
}
1088+
10761089
func (c *Config) SetDownloadSpeedLimitConfig(config string) error {
10771090
c.DownloadSpeedLimitConfig = config
10781091
return c.save(dSettingsKeyDownloadSpeedLimit, config)

src/lastore-daemon/manager.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ type Manager struct {
111111
logFds []*os.File
112112
logFdsMu sync.Mutex
113113
logTmpFile *os.File
114+
115+
isAutoCheckTimerFirstRun bool
114116
}
115117

116118
/*
@@ -243,6 +245,20 @@ func (m *Manager) initDSettingsChangedHandle() {
243245
logger.Info("AutoDownloadUpdates changed to:", autoDownloadUpdates)
244246
}
245247
})
248+
m.config.ConnectConfigChanged(config.DSettingsKeyIntranetUpdate, func(oldValue, newValue interface{}) {
249+
intranetUpdate := newValue.(bool)
250+
if intranetUpdate {
251+
// 当开启内网更新时,将上次检查时间设置为0,并重新触发lastoreAutoCheck定时器
252+
if err := m.config.SetLastCheckTime(time.Unix(0, 0)); err != nil {
253+
logger.Warningf("SetLastCheckTime failed: %v", err)
254+
}
255+
m.isAutoCheckTimerFirstRun = true
256+
if err := m.updateAutoCheckSystemUnit(); err != nil {
257+
logger.Warningf("updateAutoCheckSystemUnit failed: %v", err)
258+
}
259+
m.isAutoCheckTimerFirstRun = false
260+
}
261+
})
246262
}
247263

248264
// recreateSystem 重新创建system对象,用于incremental-update热更新
@@ -308,7 +324,11 @@ func (m *Manager) TryToStartAutoCheck() {
308324
if isTimerUnitFileExists(lastoreAutoCheck) {
309325
return
310326
}
311-
m.updateAutoCheckSystemUnit()
327+
m.isAutoCheckTimerFirstRun = true
328+
if err := m.updateAutoCheckSystemUnit(); err != nil {
329+
logger.Warning(err)
330+
}
331+
m.isAutoCheckTimerFirstRun = false
312332
}
313333

314334
func (m *Manager) delUpdatePackage(sender dbus.Sender, jobName string, packages string) (*Job, error) {

src/lastore-daemon/manager_unit.go

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -320,29 +320,30 @@ func (m *Manager) handleAbortAutoDownload() {
320320
func (m *Manager) getNextUpdateDelay() time.Duration {
321321
elapsed := time.Since(m.config.LastCheckTime)
322322
remained := m.config.CheckInterval - elapsed
323-
if remained < 0 {
323+
if remained < _minDelayTime {
324+
// ensure delay at least have 10 seconds
324325
return _minDelayTime
325326
}
326-
// ensure delay at least have 10 seconds
327-
return remained + _minDelayTime
327+
328+
return remained
328329
}
329330

330331
func (m *Manager) getNextAutoCheckDelay() int {
331-
checkInterval := m.config.CheckInterval
332-
if checkInterval < 0 {
333-
checkInterval = 0
334-
}
335-
336-
elapsed := time.Since(m.config.LastCheckTime)
337-
remained := int((checkInterval - elapsed) / time.Second)
338-
if remained < 0 {
339-
remained = 0
332+
if m.config.IntranetUpdate {
333+
if m.isAutoCheckTimerFirstRun {
334+
randomDelay := rand.New(rand.NewSource(time.Now().UnixNano())).Intn(m.config.StartCheckRange[1]-m.config.StartCheckRange[0]) + m.config.StartCheckRange[0]
335+
return randomDelay
336+
} else {
337+
checkInterval := m.config.CheckInterval
338+
if checkInterval < 0 {
339+
checkInterval = 0
340+
}
341+
return int((checkInterval) / time.Second)
342+
}
343+
} else {
344+
randomDelay := rand.New(rand.NewSource(time.Now().UnixNano())).Intn(m.config.StartCheckRange[1]-m.config.StartCheckRange[0]) + m.config.StartCheckRange[0]
345+
return int(m.getNextUpdateDelay()/time.Second) + randomDelay
340346
}
341-
342-
randomDelay := rand.New(rand.NewSource(time.Now().UnixNano())).Intn(m.config.StartCheckRange[1]-m.config.StartCheckRange[0]) + m.config.StartCheckRange[0]
343-
autoCheckDelay := remained + randomDelay
344-
logger.Infof("get next auto check delay, StartCheckRange=%v, randomDelay=%d, autoCheckDelay=%d", m.config.StartCheckRange, randomDelay, autoCheckDelay)
345-
return autoCheckDelay
346347
}
347348

348349
// isAllowedToTriggerSystemEvent checks if the uid is allowed to trigger system events

0 commit comments

Comments
 (0)