fix: 网元状态加ip/直连获取超时200毫秒

This commit is contained in:
TsMask
2024-01-05 12:00:34 +08:00
parent c00e2d6c56
commit d600c61333
3 changed files with 30 additions and 7 deletions

View File

@@ -1,6 +1,8 @@
package controller
import (
"fmt"
"ems.agt/src/framework/i18n"
"ems.agt/src/framework/utils/ctx"
"ems.agt/src/framework/utils/parse"
@@ -23,6 +25,9 @@ type NeInfoController struct {
neInfoService neService.INeInfo
}
// neStateCacheMap 网元状态缓存最后一次成功的信息
var neStateCacheMap map[string]map[string]any = make(map[string]map[string]any)
// 网元状态
//
// GET /state
@@ -42,14 +47,31 @@ func (s *NeInfoController) NeState(c *gin.Context) {
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.noNEInfo")))
return
}
neKey := fmt.Sprintf("%s_%s", neInfo.NeType, neInfo.NeId)
// 网元直连
resData, err := neService.NeState(neInfo)
if err != nil {
c.JSON(200, result.ErrMsg("connection failure"))
// 异常取上次缓存
if v, ok := neStateCacheMap[neKey]; ok && v != nil {
v["online"] = false
neStateCacheMap[neKey] = v
} else {
neStateCacheMap[neKey] = map[string]any{
"online": false,
"neId": neInfo.NeId,
"neName": neInfo.NeName,
"neType": neInfo.NeType,
"neIP": neInfo.IP,
}
}
c.JSON(200, result.OkData(neStateCacheMap[neKey]))
return
}
// 存入缓存
resData["online"] = true
neStateCacheMap[neKey] = resData
c.JSON(200, result.OkData(resData))
}