Skip to content

Commit 85b2bb1

Browse files
committed
feat: 删除旧逻辑(回收站、SSE记录表),并去除无用方法
feat: 主控数据缓存逻辑,缓解sqlite压力 feat: 服务数据缓存逻辑,环节sqlite压力
1 parent 86a791e commit 85b2bb1

File tree

15 files changed

+1664
-972
lines changed

15 files changed

+1664
-972
lines changed

cmd/server/main.go

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"NodePassDash/internal/endpointcache"
99
"NodePassDash/internal/nodepass"
1010
"NodePassDash/internal/router"
11+
"NodePassDash/internal/servicecache"
1112

1213
// "NodePassDash/internal/lifecycle"
1314
log "NodePassDash/internal/log"
@@ -217,6 +218,13 @@ func initializeServices(sseDebugLog bool) (*gorm.DB, *auth.Service, *endpoint.Se
217218
}
218219
log.Infof("✅ Endpoint内存缓存初始化成功,已加载 %d 个端点", endpointcache.Shared.Count())
219220

221+
// 初始化Service内存缓存
222+
if err := servicecache.InitShared(gormDB); err != nil {
223+
log.Errorf("初始化Service内存缓存失败: %v", err)
224+
return nil, nil, nil, nil, nil, nil, nil, nil, fmt.Errorf("初始化Service缓存失败: %v", err)
225+
}
226+
log.Infof("✅ Service内存缓存初始化成功,已加载 %d 个服务", servicecache.Shared.Count())
227+
220228
// 初始化其他服务
221229
endpointService := endpoint.NewService(gormDB)
222230
tunnelService := tunnel.NewService(gormDB)
@@ -301,9 +309,27 @@ func startBackgroundServices(gormDB *gorm.DB, sseService *sse.Service, sseManage
301309
}()
302310
log.Info("Endpoint缓存定时持久化任务已启动(间隔: 30秒)")
303311

312+
// 启动Service缓存定时持久化任务(每30秒持久化一次变更)
313+
go func() {
314+
ticker := time.NewTicker(30 * time.Second)
315+
defer ticker.Stop()
316+
317+
for range ticker.C {
318+
if err := servicecache.Shared.PersistIfNeeded(gormDB); err != nil {
319+
log.Errorf("❌ 持久化Service缓存失败: %v", err)
320+
} else {
321+
stats := servicecache.Shared.GetStats()
322+
dirtyCount := stats["dirty_count"].(int)
323+
if dirtyCount > 0 {
324+
log.Debugf("💾 持久化了 %d 个变更的服务", dirtyCount)
325+
}
326+
}
327+
}
328+
}()
329+
log.Info("Service缓存定时持久化任务已启动(间隔: 30秒)")
330+
304331
// 启动SSE相关服务
305332
go func() {
306-
sseService.StartStoreWorkers(4) // 减少worker数量
307333
sseManager.StartDaemon()
308334

309335
// 初始化SSE系统
@@ -340,6 +366,14 @@ func gracefulShutdown(server *http.Server, gormDB *gorm.DB, trafficScheduler *da
340366
log.Infof("✅ Endpoint缓存已成功关闭并持久化")
341367
}
342368

369+
// 2. 持久化Service缓存(保证数据不丢失)
370+
log.Infof("💾 正在持久化Service缓存...")
371+
if err := servicecache.Shared.Shutdown(gormDB); err != nil {
372+
log.Errorf("❌ 关闭Service缓存失败: %v", err)
373+
} else {
374+
log.Infof("✅ Service缓存已成功关闭并持久化")
375+
}
376+
343377
// 关闭增强系统(暂时注释掉)
344378
// if err := lifecycleManager.Shutdown(); err != nil {
345379
// log.Errorf("增强系统关闭失败: %v", err)

internal/api/data.go

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package api
22

33
import (
4-
"NodePassDash/internal/db"
54
"NodePassDash/internal/endpoint"
5+
"NodePassDash/internal/endpointcache"
66
log "NodePassDash/internal/log"
77
"NodePassDash/internal/models"
88
"NodePassDash/internal/nodepass"
@@ -408,7 +408,15 @@ func (h *DataHandler) handleImportV1(c *gin.Context, baseData struct {
408408
go func() {
409409
time.Sleep(100 * time.Millisecond) // 稍作延迟确保事务提交完成
410410
for _, ep := range newEndpoints {
411-
updateEndpointTunnelCountForData(ep.ID)
411+
// 查询隧道数量
412+
var count int64
413+
if err := h.db.Model(&models.Tunnel{}).Where("endpoint_id = ?", ep.ID).Count(&count).Error; err != nil {
414+
log.Errorf("[数据导入] 查询端点 %d 隧道计数失败: %v", ep.ID, err)
415+
continue
416+
}
417+
// 直接更新缓存
418+
endpointcache.Shared.UpdateTunnelCount(ep.ID, count)
419+
log.Debugf("[数据导入] 端点 %d 隧道计数已更新为: %d (已缓存)", ep.ID, count)
412420
}
413421
}()
414422
}
@@ -533,20 +541,6 @@ func (h *DataHandler) handleImportV2(c *gin.Context, baseData struct {
533541
})
534542
}
535543

536-
// updateEndpointTunnelCountForData 更新端点的隧道计数,用于数据导入后的异步更新
537-
func updateEndpointTunnelCountForData(endpointID int64) {
538-
err := db.ExecuteWithRetry(func(db *gorm.DB) error {
539-
return db.Model(&models.Endpoint{}).Where("id = ?", endpointID).
540-
Update("tunnel_count", gorm.Expr("(SELECT COUNT(*) FROM tunnels WHERE endpoint_id = ?)", endpointID)).Error
541-
})
542-
543-
if err != nil {
544-
log.Errorf("[数据导入]更新端点 %d 隧道计数失败: %v", endpointID, err)
545-
} else {
546-
log.Debugf("[数据导入]端点 %d 隧道计数已更新", endpointID)
547-
}
548-
}
549-
550544
// ValidateImportResult 单个主控的验证结果
551545
type ValidateImportResult struct {
552546
Name string `json:"name"`

0 commit comments

Comments
 (0)