@@ -702,7 +702,7 @@ func CleanMonitorHistory() (int64, error) {
702702 if db .DB != nil {
703703 // 使用BadgerDB的MonitorHistoryOps清理过期数据
704704 monitorOps := db .NewMonitorHistoryOps (db .DB )
705- maxAge := 60 * 24 * time .Hour // 60天
705+ maxAge := 7 * 24 * time .Hour // 修改为7天,减少数据库大小
706706 count , err := monitorOps .CleanupOldMonitorHistories (maxAge )
707707 if err != nil {
708708 log .Printf ("BadgerDB监控历史清理失败: %v" , err )
@@ -743,11 +743,11 @@ func CleanMonitorHistory() (int64, error) {
743743 err := executeWithoutLock (func () error {
744744 batchSize := 25 // 增加批次大小到25(从20增加)
745745 maxRetries := 3 // 减少重试次数,更快失败
746- cutoffDate := time .Now ().AddDate (0 , 0 , - 60 ) // 延长到60天,避免误删除有效历史数据
746+ cutoffDate := time .Now ().AddDate (0 , 0 , - 7 ) // 修改为7天,减少数据库大小和内存占用
747747 safetyBuffer := time .Now ().Add (- 6 * time .Hour ) // 添加6小时安全缓冲区,避免误删新数据
748748
749749 // 使用非事务方式分批清理,避免长时间事务锁定
750- // 清理60天前的监控记录 ,并确保不删除最近6小时的数据
750+ // 清理7天前的监控记录 ,并确保不删除最近6小时的数据
751751 for {
752752 var count int64
753753 var err error
@@ -884,6 +884,38 @@ func CleanMonitorHistory() (int64, error) {
884884 break
885885 }
886886
887+ // 清理7天前的流量记录
888+ for retry := 0 ; retry < maxRetries ; retry ++ {
889+ // 等待确保没有其他操作在进行
890+ time .Sleep (time .Duration (retry * 100 ) * time .Millisecond )
891+
892+ if DB == nil {
893+ return fmt .Errorf ("数据库未初始化" )
894+ }
895+ result := DB .Exec ("DELETE FROM transfers WHERE created_at < ?" , cutoffDate )
896+
897+ if result .Error != nil {
898+ err := result .Error
899+
900+ if strings .Contains (err .Error (), "database is locked" ) ||
901+ strings .Contains (err .Error (), "SQL statements in progress" ) ||
902+ strings .Contains (err .Error (), "cannot commit" ) {
903+ if retry < maxRetries - 1 {
904+ backoffDelay := time .Duration ((retry + 1 )* 1000 ) * time .Millisecond
905+ log .Printf ("数据库忙碌,%v 后重试历史流量记录清理 (%d/%d)" , backoffDelay , retry + 1 , maxRetries )
906+ time .Sleep (backoffDelay )
907+ continue
908+ }
909+ }
910+
911+ log .Printf ("清理历史流量记录失败: %v" , err )
912+ return err
913+ }
914+
915+ totalCleaned += result .RowsAffected
916+ break
917+ }
918+
887919 if totalCleaned > 0 {
888920 log .Printf ("历史数据清理完成,共清理 %d 条记录" , totalCleaned )
889921 }
@@ -1208,7 +1240,10 @@ func SaveAllTrafficToDB() {
12081240 // 等待所有批次完成
12091241 wg .Wait ()
12101242
1211- log .Printf ("成功保存 %d 个服务器的累计流量数据" , len (serverData ))
1243+ // 只在调试模式下输出详细的保存日志
1244+ if Conf .Debug {
1245+ log .Printf ("成功保存 %d 个服务器的累计流量数据" , len (serverData ))
1246+ }
12121247}
12131248
12141249// SaveAllTrafficToBadgerDB 保存所有服务器的累计流量到BadgerDB
@@ -1292,7 +1327,10 @@ func SaveAllTrafficToBadgerDB() {
12921327 // 等待所有批次完成
12931328 wg .Wait ()
12941329
1295- log .Printf ("BadgerDB: 成功保存 %d 个服务器的累计流量数据" , successCount )
1330+ // 只在调试模式下输出详细的保存日志
1331+ if Conf .Debug {
1332+ log .Printf ("BadgerDB: 成功保存 %d 个服务器的累计流量数据" , successCount )
1333+ }
12961334}
12971335
12981336// SaveAllDataToDB 保存所有数据到数据库,确保数据永久化
@@ -1399,7 +1437,10 @@ func SaveAllServerDataToDB() {
13991437 }
14001438 }
14011439
1402- log .Printf ("BadgerDB: 成功保存 %d 个服务器的完整数据" , successCount )
1440+ // 只在调试模式下输出详细的保存日志
1441+ if Conf .Debug {
1442+ log .Printf ("BadgerDB: 成功保存 %d 个服务器的完整数据" , successCount )
1443+ }
14031444}
14041445
14051446// SaveAllUserDataToDB 保存所有用户数据到BadgerDB
@@ -1428,7 +1469,10 @@ func SaveAllUserDataToDB() {
14281469 }
14291470 }
14301471
1431- log .Printf ("BadgerDB: 成功保存 %d 个用户的数据" , successCount )
1472+ // 只在调试模式下输出详细的保存日志
1473+ if Conf .Debug {
1474+ log .Printf ("BadgerDB: 成功保存 %d 个用户的数据" , successCount )
1475+ }
14321476}
14331477
14341478// SaveAllDDNSStateToDB 保存所有DDNS状态到BadgerDB
@@ -1438,7 +1482,10 @@ func SaveAllDDNSStateToDB() {
14381482 }
14391483
14401484 // DDNS状态通常在变更时已经保存,这里只是确保一致性
1441- log .Printf ("BadgerDB: DDNS状态数据已在变更时保存" )
1485+ // 只在调试模式下输出详细的保存日志
1486+ if Conf .Debug {
1487+ log .Printf ("BadgerDB: DDNS状态数据已在变更时保存" )
1488+ }
14421489}
14431490
14441491// SaveAllAPITokensToDB 保存所有API令牌到BadgerDB
@@ -1515,7 +1562,10 @@ func SaveAllAPITokensToDB() {
15151562 }
15161563
15171564 if successCount > 0 || skipCount > 0 || deleteCount > 0 {
1518- log .Printf ("BadgerDB: 成功保存 %d 个API令牌,跳过 %d 个已存在的令牌,删除 %d 个过期令牌" , successCount , skipCount , deleteCount )
1565+ // 只在调试模式下输出详细的保存日志
1566+ if Conf .Debug {
1567+ log .Printf ("BadgerDB: 成功保存 %d 个API令牌,跳过 %d 个已存在的令牌,删除 %d 个过期令牌" , successCount , skipCount , deleteCount )
1568+ }
15191569 }
15201570}
15211571
@@ -2255,9 +2305,7 @@ func executeDBInsertRequest(req DBInsertRequest) error {
22552305 // 特殊处理monitor_histories表,确保不使用@id字段
22562306 if req .TableName == "monitor_histories" {
22572307 // 移除@id字段如果存在
2258- if _ , exists := req .Data ["@id" ]; exists {
2259- delete (req .Data , "@id" )
2260- }
2308+ delete (req .Data , "@id" )
22612309
22622310 // 避免直接使用GORM的Create方法,改用手动SQL
22632311 fields , values , args := prepareInsertSQL (req .Data )
@@ -2524,9 +2572,7 @@ func StartMonitorHistoryWorker() {
25242572 go func () {
25252573 for req := range monitorHistoryQueue {
25262574 // 确保没有使用@id字段
2527- if _ , exists := req .Data ["@id" ]; exists {
2528- delete (req .Data , "@id" )
2529- }
2575+ delete (req .Data , "@id" )
25302576
25312577 // 增加更多字段检查和验证
25322578 var monitorID uint64
@@ -2663,9 +2709,7 @@ func StartBadgerMonitorHistoryWorker() {
26632709 go func () {
26642710 for req := range monitorHistoryQueue {
26652711 // 确保没有使用@id字段
2666- if _ , exists := req .Data ["@id" ]; exists {
2667- delete (req .Data , "@id" )
2668- }
2712+ delete (req .Data , "@id" )
26692713
26702714 // 转换数据为MonitorHistory结构
26712715 history := & model.MonitorHistory {
@@ -3019,9 +3063,7 @@ func batchInsertMonitorHistories(requests []DBInsertRequest) error {
30193063 allData := make ([]map [string ]interface {}, len (requests ))
30203064 for i , req := range requests {
30213065 // 移除@id字段
3022- if _ , exists := req .Data ["@id" ]; exists {
3023- delete (req .Data , "@id" )
3024- }
3066+ delete (req .Data , "@id" )
30253067 allData [i ] = req .Data
30263068 }
30273069
0 commit comments