Private
Public Access
1
0

修复:审查发现的高优先问题(竞态/初始化/碰撞)

- app.go: profileSvc移入App struct,用a.mu保护
- sqlite.go: InitFast加sync.Once防并发双重初始化
- client.go: Manager.Connect加sync.Mutex防竞态泄漏SSH
- service.go: 临时文件用os.CreateTemp防时间戳碰撞
- connection-manager: 密码缺失时不再塞入假WailsTransport
This commit is contained in:
2026-05-04 15:40:04 +08:00
parent 6bee55b96f
commit ee4b1f5ac1
5 changed files with 52 additions and 51 deletions

11
app.go
View File

@@ -38,6 +38,7 @@ type App struct {
pdfAPI *api.PdfAPI
filesystem *filesystem.FileSystemService
sftpService *sftp.Service
profileSvc *service.ProfileService
isAlwaysOnTop bool
mu sync.Mutex
}
@@ -986,13 +987,13 @@ type SaveProfileRequest struct {
LastConnected *int64 `json:"last_connected"`
}
var profileSvc *service.ProfileService
func (a *App) ensureProfileSvc() *service.ProfileService {
if profileSvc == nil {
profileSvc = service.NewProfileService()
a.mu.Lock()
defer a.mu.Unlock()
if a.profileSvc == nil {
a.profileSvc = service.NewProfileService()
}
return profileSvc
return a.profileSvc
}
func (a *App) LoadConnectionProfiles() ([]map[string]interface{}, error) {

View File

@@ -327,7 +327,6 @@ class ConnectionManagerImpl {
if (profile.type === 'sftp') {
if (!profile.password && !profile.keyPath) {
this._pool.set(profileId, new WailsTransport())
this.setState('error')
return
}

View File

@@ -22,6 +22,7 @@ type Client struct {
// Manager 全局 SFTP 连接管理器(以 host:port 为 key 的连接池)
type Manager struct {
clients sync.Map // map[string]*Client
mu sync.Mutex
}
var globalManager = &Manager{}
@@ -35,6 +36,9 @@ func GetManager() *Manager {
func (m *Manager) Connect(config *Config) (*Client, error) {
key := fmt.Sprintf("%s:%d", config.Host, config.Port)
m.mu.Lock()
defer m.mu.Unlock()
if existing, ok := m.clients.Load(key); ok {
c := existing.(*Client)
if c.IsHealthy() {

View File

@@ -279,8 +279,12 @@ func (s *Service) DownloadToTemp(connID string, remotePath string) (string, erro
}
tmpDir := os.TempDir()
// 用时间戳+随机数避免同名文件覆盖
localPath := filepath.Join(tmpDir, fmt.Sprintf("udesk-sftp-preview-%d-%s", time.Now().UnixNano(), filepath.Base(remotePath)))
tmpFile, e := os.CreateTemp(tmpDir, "udesk-sftp-*-"+filepath.Base(remotePath))
if e != nil {
return "", fmt.Errorf("创建临时文件失败: %w", e)
}
localPath := tmpFile.Name()
tmpFile.Close()
err = c.WithRetry(func(sc *sftpclient.Client) error {
src, e := sc.Open(remotePath)

View File

@@ -2,11 +2,12 @@ package storage
import (
"fmt"
"u-desk/internal/common"
"u-desk/internal/storage/models"
"os"
"path/filepath"
"sync"
"time"
"u-desk/internal/common"
"u-desk/internal/storage/models"
"github.com/glebarez/sqlite"
"gorm.io/gorm"
@@ -14,61 +15,53 @@ import (
)
var globalDB *gorm.DB
var initOnce sync.Once
// Init 快速初始化 SQLite兼容旧代码
func Init() (*gorm.DB, error) {
return InitFast()
}
// InitFast 超快速初始化 SQLite优化版
// 跳过不必要的检查,使用 WAL 模式,优化连接池
func InitFast() (*gorm.DB, error) {
if globalDB != nil {
return globalDB, nil
}
var initErr error
initOnce.Do(func() {
dataDir := common.GetUserDataDir()
if e := os.MkdirAll(dataDir, 0755); e != nil {
initErr = e
return
}
// 使用统一的数据目录
dataDir := common.GetUserDataDir()
if err := os.MkdirAll(dataDir, 0755); err != nil {
return nil, err
}
dbPath := filepath.Join(dataDir, "app.db")
db, e := gorm.Open(sqlite.Open(dbPath+"?_pragma=journal_mode(WAL)&_pragma=synchronous(NORMAL)&_pragma=cache_size(-64000)&_pragma=temp_store(MEMORY)&_pragma=mmap_size(30000000000)&_pragma=page_size(4096)&_pragma=foreign_keys(1)"), &gorm.Config{
Logger: logger.Default.LogMode(logger.Silent),
SkipDefaultTransaction: true,
PrepareStmt: true,
})
if e != nil {
initErr = e
return
}
dbPath := filepath.Join(dataDir, "app.db")
sqlDB, e := db.DB()
if e != nil {
initErr = fmt.Errorf("获取底层SQL数据库失败: %v", e)
return
}
sqlDB.SetMaxOpenConns(1)
sqlDB.SetMaxIdleConns(1)
sqlDB.SetConnMaxLifetime(time.Hour)
// 极限性能优化参数:
// - journal_mode=WAL: 写前日志,大幅提升并发性能
// - synchronous=NORMAL: 降低持久性要求,提升性能
// - cache_size=-64000: 64MB 缓存,减少磁盘 I/O
// - temp_store=MEMORY: 临时表存储在内存中
// - mmap_size=30000000000: 300MB 内存映射,加速读取
// - page_size=4096: 优化页面大小
db, err := gorm.Open(sqlite.Open(dbPath+"?_pragma=journal_mode(WAL)&_pragma=synchronous(NORMAL)&_pragma=cache_size(-64000)&_pragma=temp_store(MEMORY)&_pragma=mmap_size(30000000000)&_pragma=page_size(4096)&_pragma=foreign_keys(1)"), &gorm.Config{
Logger: logger.Default.LogMode(logger.Silent),
SkipDefaultTransaction: true, // 跳过默认事务,提升性能
PrepareStmt: true, // 预编译语句缓存
if e := db.AutoMigrate(&models.AppConfig{}, &models.ConnectionProfile{}); e != nil {
initErr = e
return
}
globalDB = db
})
if err != nil {
return nil, err
if initErr != nil {
return nil, initErr
}
sqlDB, err := db.DB()
if err != nil {
return nil, fmt.Errorf("获取底层SQL数据库失败: %v", err)
}
sqlDB.SetMaxOpenConns(1) // SQLite 只需要一个连接
sqlDB.SetMaxIdleConns(1)
sqlDB.SetConnMaxLifetime(time.Hour)
// AutoMigrate 在启动时执行,但只在表结构不存在时创建
// SQLite 的 AutoMigrate 很快,不会造成明显延迟
if err := db.AutoMigrate(
&models.AppConfig{},
&models.ConnectionProfile{},
); err != nil {
return nil, err
}
globalDB = db
return globalDB, nil
}