fix: 网元状态加ip/直连获取超时200毫秒
This commit is contained in:
@@ -15,13 +15,13 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// Get 发送 GET 请求
|
// Get 发送 GET 请求
|
||||||
// timeout 超时时间(秒)
|
// timeout 超时时间(毫秒)
|
||||||
func Get(url string, headers map[string]string, timeout uint8) ([]byte, error) {
|
func Get(url string, headers map[string]string, timeout int) ([]byte, error) {
|
||||||
if timeout < 1 || timeout > 180 {
|
if timeout < 100 || timeout > 180_000 {
|
||||||
timeout = 1
|
timeout = 100
|
||||||
}
|
}
|
||||||
client := &http.Client{
|
client := &http.Client{
|
||||||
Timeout: time.Duration(timeout) * time.Second, // 设置超时时间为 5 秒
|
Timeout: time.Duration(timeout) * time.Millisecond, // 超时时间
|
||||||
}
|
}
|
||||||
|
|
||||||
req, err := http.NewRequest("GET", url, nil)
|
req, err := http.NewRequest("GET", url, nil)
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
package controller
|
package controller
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
"ems.agt/src/framework/i18n"
|
"ems.agt/src/framework/i18n"
|
||||||
"ems.agt/src/framework/utils/ctx"
|
"ems.agt/src/framework/utils/ctx"
|
||||||
"ems.agt/src/framework/utils/parse"
|
"ems.agt/src/framework/utils/parse"
|
||||||
@@ -23,6 +25,9 @@ type NeInfoController struct {
|
|||||||
neInfoService neService.INeInfo
|
neInfoService neService.INeInfo
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// neStateCacheMap 网元状态缓存最后一次成功的信息
|
||||||
|
var neStateCacheMap map[string]map[string]any = make(map[string]map[string]any)
|
||||||
|
|
||||||
// 网元状态
|
// 网元状态
|
||||||
//
|
//
|
||||||
// GET /state
|
// GET /state
|
||||||
@@ -42,14 +47,31 @@ func (s *NeInfoController) NeState(c *gin.Context) {
|
|||||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.noNEInfo")))
|
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.noNEInfo")))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
neKey := fmt.Sprintf("%s_%s", neInfo.NeType, neInfo.NeId)
|
||||||
|
|
||||||
// 网元直连
|
// 网元直连
|
||||||
resData, err := neService.NeState(neInfo)
|
resData, err := neService.NeState(neInfo)
|
||||||
if err != nil {
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 存入缓存
|
||||||
|
resData["online"] = true
|
||||||
|
neStateCacheMap[neKey] = resData
|
||||||
c.JSON(200, result.OkData(resData))
|
c.JSON(200, result.OkData(resData))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ import (
|
|||||||
func NeState(neInfo model.NeInfo) (map[string]any, error) {
|
func NeState(neInfo model.NeInfo) (map[string]any, error) {
|
||||||
// 网元直连
|
// 网元直连
|
||||||
neUrl := fmt.Sprintf("http://%s:%d/api/rest/systemManagement/v1/elementType/%s/objectType/systemState", neInfo.IP, neInfo.Port, strings.ToLower(neInfo.NeType))
|
neUrl := fmt.Sprintf("http://%s:%d/api/rest/systemManagement/v1/elementType/%s/objectType/systemState", neInfo.IP, neInfo.Port, strings.ToLower(neInfo.NeType))
|
||||||
resBytes, err := fetch.Get(neUrl, nil, 1)
|
resBytes, err := fetch.Get(neUrl, nil, 200)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Warnf("NeState %s", err.Error())
|
logger.Warnf("NeState %s", err.Error())
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -33,6 +33,7 @@ func NeState(neInfo model.NeInfo) (map[string]any, error) {
|
|||||||
"neType": neInfo.NeType,
|
"neType": neInfo.NeType,
|
||||||
"neId": neInfo.NeId,
|
"neId": neInfo.NeId,
|
||||||
"neName": neInfo.NeName,
|
"neName": neInfo.NeName,
|
||||||
|
"neIP": neInfo.IP,
|
||||||
"refreshTime": time.Now().UnixMilli(), // 获取时间
|
"refreshTime": time.Now().UnixMilli(), // 获取时间
|
||||||
"version": resData["version"],
|
"version": resData["version"],
|
||||||
"capability": resData["capability"],
|
"capability": resData["capability"],
|
||||||
|
|||||||
Reference in New Issue
Block a user