From 8ab5d1a2b7a11710ce0c71697c785d36b9baf9fe Mon Sep 17 00:00:00 2001 From: TsMask <340112800@qq.com> Date: Fri, 26 Jan 2024 20:36:03 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=B9=B6=E5=8F=91=E8=AF=BB=E5=86=99?= =?UTF-8?q?=E7=BD=91=E5=85=83=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/network_element/controller/ne_info.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/modules/network_element/controller/ne_info.go b/src/modules/network_element/controller/ne_info.go index 0c10d493..975f7d1b 100644 --- a/src/modules/network_element/controller/ne_info.go +++ b/src/modules/network_element/controller/ne_info.go @@ -27,7 +27,8 @@ type NeInfoController struct { } // neStateCacheMap 网元状态缓存最后一次成功的信息 -var neStateCacheMap = sync.Map{} +var neStateCacheMap sync.Map +var mutex sync.Mutex // 网元状态 // @@ -53,8 +54,8 @@ func (s *NeInfoController) NeState(c *gin.Context) { // 网元直连 resData, err := neService.NeState(neInfo) if err != nil { + mutex.Lock() // 异常取上次缓存 - resDataCache, ok := neStateCacheMap.Load(neKey) if ok && resDataCache != nil { resDataCache.(map[string]any)["online"] = false @@ -68,13 +69,16 @@ func (s *NeInfoController) NeState(c *gin.Context) { } } neStateCacheMap.Store(neKey, resDataCache) + mutex.Unlock() c.JSON(200, result.OkData(resDataCache)) return } // 存入缓存 resData["online"] = true + mutex.Lock() neStateCacheMap.Store(neKey, resData) + mutex.Unlock() c.JSON(200, result.OkData(resData)) }