@@ -220,28 +220,28 @@ func (ss *ServiceSentinel) loadMonitorHistory() {
220220
221221 // 加载服务监控历史记录,优化查询性能
222222 var mhs []model.MonitorHistory
223-
223+
224224 // 添加查询优化和超时控制
225225 ctx , cancel := context .WithTimeout (context .Background (), 30 * time .Second )
226226 defer cancel ()
227-
227+
228228 startTime := time .Now ()
229-
229+
230230 // 直接查询月度数据,系统启动时需要快速加载
231231 fromDate := today .AddDate (0 , 0 , - 29 )
232232 toDate := today
233-
233+
234234 err = DB .WithContext (ctx ).
235235 Where ("created_at > ? AND created_at < ?" , fromDate , toDate ).
236236 Order ("created_at DESC" ).
237237 Find (& mhs ).Error
238-
238+
239239 if err != nil {
240240 log .Printf ("加载月度监控数据失败: %v" , err )
241241 return
242242 }
243243 queryDuration := time .Since (startTime )
244-
244+
245245 if queryDuration > 500 * time .Millisecond {
246246 log .Printf ("慢SQL查询警告: 分批加载月度数据耗时 %v,返回 %d 条记录" , queryDuration , len (mhs ))
247247 } else {
@@ -376,7 +376,7 @@ func (ss *ServiceSentinel) worker() {
376376
377377 // 内存压力计数器
378378 memoryPressureCounter := 0
379-
379+
380380 // 添加内存监控
381381 memoryCheckTicker := time .NewTicker (1 * time .Minute ) // 每分钟检查内存
382382 defer memoryCheckTicker .Stop ()
@@ -400,12 +400,12 @@ func (ss *ServiceSentinel) worker() {
400400 var m runtime.MemStats
401401 runtime .ReadMemStats (& m )
402402 currentMemMB := m .Alloc / 1024 / 1024
403-
403+
404404 if currentMemMB > 500 { // 如果内存超过500MB(从300MB提高)
405405 log .Printf ("ServiceSentinel检测到高内存使用: %dMB,执行清理" , currentMemMB )
406406 ss .limitDataSize ()
407407 ss .cleanupOldData ()
408-
408+
409409 // 如果内存仍然很高,强制GC
410410 if currentMemMB > 800 { // 从400MB提高到800MB
411411 runtime .GC ()
@@ -441,7 +441,7 @@ func (ss *ServiceSentinel) handleServiceReport(r ReportData) {
441441 }
442442
443443 mh := r .Data
444-
444+
445445 // 添加边界检查,防止panic
446446 if mh .GetId () == 0 {
447447 log .Printf ("NG>> 无效的监控ID: %+v" , r )
@@ -464,15 +464,15 @@ func (ss *ServiceSentinel) handleServiceReport(r ReportData) {
464464 ts .ping = float32 (Conf .MaxTCPPingValue )
465465 }
466466 ts .count = 0
467-
467+
468468 // 使用异步数据库插入队列来保存监控数据,避免并发冲突
469469 monitorData := map [string ]interface {}{
470470 "monitor_id" : mh .GetId (),
471471 "avg_delay" : ts .ping ,
472472 "data" : mh .Data ,
473473 "server_id" : r .Reporter ,
474474 }
475-
475+
476476 // 使用异步插入避免数据库锁冲突
477477 AsyncDBInsert ("monitor_histories" , monitorData , func (err error ) {
478478 if err != nil {
@@ -518,19 +518,19 @@ func (ss *ServiceSentinel) handleServiceReport(r ReportData) {
518518 ss .serviceCurrentStatusData [mh .GetId ()] = make ([]* pb.TaskResult , _CurrentStatusSize )
519519 }
520520
521- // 边界检查:确保索引不会超出当前数组的实际长度
522- currentArrayLength := len (ss .serviceCurrentStatusData [mh .GetId ()])
523- if ss .serviceCurrentStatusIndex [mh .GetId ()].index >= currentArrayLength {
524- ss .serviceCurrentStatusIndex [mh .GetId ()].index = 0
525- }
521+ // 边界检查:确保索引不会超出当前数组的实际长度
522+ currentArrayLength := len (ss .serviceCurrentStatusData [mh .GetId ()])
523+ if ss .serviceCurrentStatusIndex [mh .GetId ()].index >= currentArrayLength {
524+ ss .serviceCurrentStatusIndex [mh .GetId ()].index = 0
525+ }
526526
527- ss .serviceCurrentStatusData [mh .GetId ()][ss .serviceCurrentStatusIndex [mh .GetId ()].index ] = mh
528- ss .serviceCurrentStatusIndex [mh .GetId ()].index ++
529-
530- // 立即检查并重置index以防止越界,使用实际数组长度
531- if ss .serviceCurrentStatusIndex [mh .GetId ()].index >= currentArrayLength {
532- ss .serviceCurrentStatusIndex [mh .GetId ()].index = 0
533- }
527+ ss .serviceCurrentStatusData [mh .GetId ()][ss .serviceCurrentStatusIndex [mh .GetId ()].index ] = mh
528+ ss .serviceCurrentStatusIndex [mh .GetId ()].index ++
529+
530+ // 立即检查并重置index以防止越界,使用实际数组长度
531+ if ss .serviceCurrentStatusIndex [mh .GetId ()].index >= currentArrayLength {
532+ ss .serviceCurrentStatusIndex [mh .GetId ()].index = 0
533+ }
534534 }
535535
536536 // 更新当前状态
@@ -566,20 +566,20 @@ func (ss *ServiceSentinel) handleServiceReport(r ReportData) {
566566 // 改为基于时间间隔的保存策略,而不是依赖不可靠的计数器
567567 now := time .Now ()
568568 shouldSave := false
569-
569+
570570 // 检查是否需要保存数据
571571 if ss .serviceCurrentStatusIndex [mh .GetId ()].lastSaveTime .IsZero () {
572572 // 首次保存
573573 shouldSave = true
574574 } else if now .Sub (ss .serviceCurrentStatusIndex [mh .GetId ()].lastSaveTime ) >= 15 * time .Minute {
575575 // 超过15分钟未保存
576576 shouldSave = true
577- } else if ss .serviceCurrentStatusIndex [mh .GetId ()].index % _CurrentStatusSize == 0 &&
578- ss .serviceCurrentStatusIndex [mh .GetId ()].index > 0 {
577+ } else if ss .serviceCurrentStatusIndex [mh .GetId ()].index % _CurrentStatusSize == 0 &&
578+ ss .serviceCurrentStatusIndex [mh .GetId ()].index > 0 {
579579 // 当计数器完成一个周期时也保存
580580 shouldSave = true
581581 }
582-
582+
583583 if shouldSave {
584584 // 确保有数据才保存
585585 totalChecks := ss .serviceResponseDataStoreCurrentUp [mh .GetId ()] + ss .serviceResponseDataStoreCurrentDown [mh .GetId ()]
@@ -592,16 +592,16 @@ func (ss *ServiceSentinel) handleServiceReport(r ReportData) {
592592 "up" : ss .serviceResponseDataStoreCurrentUp [mh .GetId ()],
593593 "down" : ss .serviceResponseDataStoreCurrentDown [mh .GetId ()],
594594 }
595-
596- // 使用异步插入避免数据库锁冲突
597- AsyncDBInsert ( "monitor_histories" , monitorData , func (err error ) {
595+
596+ // 直接使用监控历史记录专用队列,避免@id字段错误
597+ AsyncMonitorHistoryInsert ( monitorData , func (err error ) {
598598 if err != nil {
599599 log .Printf ("NG>> 服务监控数据持久化失败 (MonitorID: %d): %v" , mh .GetId (), err )
600600 } else {
601601 ss .serviceCurrentStatusIndex [mh .GetId ()].lastSaveTime = now
602- log .Printf ("监控数据已保存 (MonitorID: %d, Up: %d, Down: %d)" ,
603- mh .GetId (),
604- ss .serviceResponseDataStoreCurrentUp [mh .GetId ()],
602+ log .Printf ("监控数据已保存 (MonitorID: %d, Up: %d, Down: %d)" ,
603+ mh .GetId (),
604+ ss .serviceResponseDataStoreCurrentUp [mh .GetId ()],
605605 ss .serviceResponseDataStoreCurrentDown [mh .GetId ()])
606606 }
607607 })
@@ -853,10 +853,10 @@ func (ss *ServiceSentinel) limitDataSize() {
853853 if len (statusData ) > maxStatusRecords {
854854 // 只保留最新的记录
855855 ss .serviceCurrentStatusData [monitorID ] = statusData [len (statusData )- maxStatusRecords :]
856-
856+
857857 // 重要:当数组被缩减时,必须重置索引以防止越界
858- if ss .serviceCurrentStatusIndex [monitorID ] != nil &&
859- ss .serviceCurrentStatusIndex [monitorID ].index >= maxStatusRecords {
858+ if ss .serviceCurrentStatusIndex [monitorID ] != nil &&
859+ ss .serviceCurrentStatusIndex [monitorID ].index >= maxStatusRecords {
860860 ss .serviceCurrentStatusIndex [monitorID ].index = 0
861861 }
862862 }
0 commit comments