From b823f1879426a3f39578d551e27dc61231ca3a94 Mon Sep 17 00:00:00 2001 From: Lykin <137850705+tiny-craft@users.noreply.github.com> Date: Thu, 4 Jan 2024 12:00:54 +0800 Subject: [PATCH] perf: optimize the performance of command monitor --- backend/services/monitor_service.go | 18 +++++++++++++--- .../content_value/ContentMonitor.vue | 21 +++++++++++++------ frontend/src/langs/en-us.json | 3 +-- frontend/src/langs/zh-cn.json | 1 + 4 files changed, 32 insertions(+), 11 deletions(-) diff --git a/backend/services/monitor_service.go b/backend/services/monitor_service.go index b14de65..43d0e68 100644 --- a/backend/services/monitor_service.go +++ b/backend/services/monitor_service.go @@ -17,6 +17,7 @@ import ( type monitorItem struct { client *redis.Client cmd *redis.MonitorCmd + mutex sync.Mutex ch chan string closeCh chan struct{} eventName string @@ -88,7 +89,7 @@ func (c *monitorService) StartMonitor(server string) (resp types.JSResp) { item.cmd = item.client.Monitor(c.ctx, item.ch) item.cmd.Start() - go c.processMonitor(item.ch, item.closeCh, item.eventName) + go c.processMonitor(&item.mutex, item.ch, item.closeCh, item.eventName) resp.Success = true resp.Data = struct { EventName string `json:"eventName"` @@ -98,12 +99,23 @@ func (c *monitorService) StartMonitor(server string) (resp types.JSResp) { return } -func (c *monitorService) processMonitor(ch <-chan string, closeCh <-chan struct{}, eventName string) { +func (c *monitorService) processMonitor(mutex *sync.Mutex, ch <-chan string, closeCh <-chan struct{}, eventName string) { + lastEmitTime := time.Now().Add(-1 * time.Minute) + cache := make([]string, 0, 1000) for { select { case data := <-ch: if data != "OK" { - runtime.EventsEmit(c.ctx, eventName, data) + go func() { + mutex.Lock() + defer mutex.Unlock() + cache = append(cache, data) + if time.Now().Sub(lastEmitTime) > 1*time.Second || len(cache) > 300 { + runtime.EventsEmit(c.ctx, eventName, cache) + cache = cache[:0:cap(cache)] + lastEmitTime = time.Now() + } + }() } case <-closeCh: diff --git a/frontend/src/components/content_value/ContentMonitor.vue b/frontend/src/components/content_value/ContentMonitor.vue index c2936ed..eec025d 100644 --- a/frontend/src/components/content_value/ContentMonitor.vue +++ b/frontend/src/components/content_value/ContentMonitor.vue @@ -1,6 +1,6 @@