fix: 并发读写网元信息
This commit is contained in:
@@ -27,7 +27,8 @@ type NeInfoController struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// neStateCacheMap 网元状态缓存最后一次成功的信息
|
// 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)
|
resData, err := neService.NeState(neInfo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
mutex.Lock()
|
||||||
// 异常取上次缓存
|
// 异常取上次缓存
|
||||||
|
|
||||||
resDataCache, ok := neStateCacheMap.Load(neKey)
|
resDataCache, ok := neStateCacheMap.Load(neKey)
|
||||||
if ok && resDataCache != nil {
|
if ok && resDataCache != nil {
|
||||||
resDataCache.(map[string]any)["online"] = false
|
resDataCache.(map[string]any)["online"] = false
|
||||||
@@ -68,13 +69,16 @@ func (s *NeInfoController) NeState(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
neStateCacheMap.Store(neKey, resDataCache)
|
neStateCacheMap.Store(neKey, resDataCache)
|
||||||
|
mutex.Unlock()
|
||||||
c.JSON(200, result.OkData(resDataCache))
|
c.JSON(200, result.OkData(resDataCache))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 存入缓存
|
// 存入缓存
|
||||||
resData["online"] = true
|
resData["online"] = true
|
||||||
|
mutex.Lock()
|
||||||
neStateCacheMap.Store(neKey, resData)
|
neStateCacheMap.Store(neKey, resData)
|
||||||
|
mutex.Unlock()
|
||||||
c.JSON(200, result.OkData(resData))
|
c.JSON(200, result.OkData(resData))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user