Skip to content

Commit 60afce0

Browse files
committed
fix: sync local HTTP speed when online limit is applied
Bug: https://pms.uniontech.com/bug-view-359485.html
1 parent 4c98733 commit 60afce0

2 files changed

Lines changed: 35 additions & 5 deletions

File tree

src/lastore-daemon/manager_update.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"os"
1515
"os/exec"
1616
"path/filepath"
17+
"reflect"
1718
"slices"
1819
"strconv"
1920
"strings"
@@ -497,8 +498,8 @@ func (m *Manager) refreshThrottlingFromPlatform() error {
497498
}
498499

499500
if downloadSpeed.IsOnlineSpeedLimit {
500-
// 如果启用了在线限速,禁用本地限速
501-
if err := m.disableLocalSpeedLimitConfig(); err != nil {
501+
// 如果启用了在线限速,禁用本地限速,并将本地限速速度同步为平台下发的限速值
502+
if err := m.disableLocalSpeedLimitConfig(downloadSpeed.LimitSpeed); err != nil {
502503
logger.Warningf("Failed to disable local speed limit %v", err)
503504
}
504505
} else {
@@ -534,7 +535,7 @@ func (m *Manager) disableOnlineSpeedLimitConfig() error {
534535
return nil
535536
}
536537

537-
func (m *Manager) disableLocalSpeedLimitConfig() error {
538+
func (m *Manager) disableLocalSpeedLimitConfig(limitSpeed string) error {
538539
var downloadSpeed downloadSpeedLimitConfig
539540
if err := json.Unmarshal([]byte(m.config.LocalDownloadSpeedLimitConfig), &downloadSpeed); err != nil {
540541
downloadSpeed = downloadSpeedLimitConfig{
@@ -543,10 +544,15 @@ func (m *Manager) disableLocalSpeedLimitConfig() error {
543544
IsOnlineSpeedLimit: false,
544545
}
545546
}
546-
if !downloadSpeed.DownloadSpeedLimitEnabled {
547-
return nil
547+
oldDownloadSpeed := downloadSpeed
548+
if limitSpeed != "" {
549+
downloadSpeed.LimitSpeed = limitSpeed
548550
}
549551
downloadSpeed.DownloadSpeedLimitEnabled = false
552+
downloadSpeed.IsOnlineSpeedLimit = false
553+
if reflect.DeepEqual(downloadSpeed, oldDownloadSpeed) {
554+
return nil
555+
}
550556
jsonStr, err := json.Marshal(downloadSpeed)
551557
if err != nil {
552558
return fmt.Errorf("failed to marshal local download speed limit config: %w", err)

src/lastore-daemon/updater_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package main
66

77
import (
8+
"encoding/json"
89
"os"
910
"path/filepath"
1011
"testing"
@@ -146,3 +147,26 @@ func TestGetStartupDownloadSpeedLimitConfig(t *testing.T) {
146147
})
147148
}
148149
}
150+
151+
func TestDisableLocalSpeedLimitConfigSyncsPlatformSpeedOnly(t *testing.T) {
152+
manager := &Manager{
153+
config: &config.Config{
154+
LocalDownloadSpeedLimitConfig: `{"DownloadSpeedLimitEnabled":true,"LimitSpeed":"888","IsOnlineSpeedLimit":false}`,
155+
},
156+
}
157+
158+
_ = manager.disableLocalSpeedLimitConfig("666")
159+
160+
var got downloadSpeedLimitConfig
161+
if err := json.Unmarshal([]byte(manager.config.LocalDownloadSpeedLimitConfig), &got); err != nil {
162+
t.Fatalf("LocalDownloadSpeedLimitConfig unmarshal error = %v", err)
163+
}
164+
want := downloadSpeedLimitConfig{
165+
DownloadSpeedLimitEnabled: false,
166+
LimitSpeed: "666",
167+
IsOnlineSpeedLimit: false,
168+
}
169+
if got != want {
170+
t.Fatalf("LocalDownloadSpeedLimitConfig = %+v, want %+v", got, want)
171+
}
172+
}

0 commit comments

Comments
 (0)