fix: 并发读写网元信息

This commit is contained in:
TsMask
2024-01-26 20:36:03 +08:00
parent 1279d0fd15
commit 8ab5d1a2b7

View File

@@ -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))
}