fix: 改进心跳报告中的错误处理和日志记录
- 增强了 RegisterNode 和 sendHeartbeat 函数中的错误消息,包含 URL 和响应体详情以便更好地调试。 - 移除了不必要的空行以使代码结构更清晰。
This commit is contained in:
@@ -18,13 +18,13 @@ import (
|
|||||||
// 节点信息存储(通过心跳更新,优先从配置文件读取)
|
// 节点信息存储(通过心跳更新,优先从配置文件读取)
|
||||||
var nodeInfo struct {
|
var nodeInfo struct {
|
||||||
sync.RWMutex
|
sync.RWMutex
|
||||||
nodeID uint
|
nodeID uint
|
||||||
nodeIP string
|
nodeIP string
|
||||||
country string
|
country string
|
||||||
province string
|
province string
|
||||||
city string
|
city string
|
||||||
isp string
|
isp string
|
||||||
cfg *config.Config
|
cfg *config.Config
|
||||||
initialized bool
|
initialized bool
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -32,7 +32,7 @@ var nodeInfo struct {
|
|||||||
func InitNodeInfo(cfg *config.Config) {
|
func InitNodeInfo(cfg *config.Config) {
|
||||||
nodeInfo.Lock()
|
nodeInfo.Lock()
|
||||||
defer nodeInfo.Unlock()
|
defer nodeInfo.Unlock()
|
||||||
|
|
||||||
nodeInfo.cfg = cfg
|
nodeInfo.cfg = cfg
|
||||||
nodeInfo.nodeID = cfg.Node.ID
|
nodeInfo.nodeID = cfg.Node.ID
|
||||||
nodeInfo.nodeIP = cfg.Node.IP
|
nodeInfo.nodeIP = cfg.Node.IP
|
||||||
@@ -73,10 +73,10 @@ type Reporter struct {
|
|||||||
|
|
||||||
func NewReporter(cfg *config.Config) *Reporter {
|
func NewReporter(cfg *config.Config) *Reporter {
|
||||||
logger, _ := zap.NewProduction()
|
logger, _ := zap.NewProduction()
|
||||||
|
|
||||||
// 初始化节点信息(从配置文件读取)
|
// 初始化节点信息(从配置文件读取)
|
||||||
InitNodeInfo(cfg)
|
InitNodeInfo(cfg)
|
||||||
|
|
||||||
return &Reporter{
|
return &Reporter{
|
||||||
cfg: cfg,
|
cfg: cfg,
|
||||||
client: &http.Client{
|
client: &http.Client{
|
||||||
@@ -123,7 +123,7 @@ func RegisterNode(cfg *config.Config) error {
|
|||||||
client := &http.Client{Timeout: 10 * time.Second}
|
client := &http.Client{Timeout: 10 * time.Second}
|
||||||
resp, err := client.Do(req)
|
resp, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("发送心跳失败: %w", err)
|
return fmt.Errorf("发送心跳失败 (URL: %s): %w", url, err)
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
@@ -173,10 +173,21 @@ func RegisterNode(cfg *config.Config) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return fmt.Errorf("心跳响应格式无效或节点信息不完整")
|
return fmt.Errorf("心跳响应格式无效或节点信息不完整 (响应体: %s)", string(body))
|
||||||
}
|
}
|
||||||
|
|
||||||
return fmt.Errorf("心跳请求失败,状态码: %d", resp.StatusCode)
|
// 读取响应体以便记录错误详情
|
||||||
|
body, err := io.ReadAll(resp.Body)
|
||||||
|
bodyStr := ""
|
||||||
|
if err == nil && len(body) > 0 {
|
||||||
|
// 限制响应体长度,避免错误信息过长
|
||||||
|
if len(body) > 500 {
|
||||||
|
bodyStr = string(body[:500]) + "..."
|
||||||
|
} else {
|
||||||
|
bodyStr = string(body)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return fmt.Errorf("心跳请求失败,状态码: %d, URL: %s, 响应体: %s", resp.StatusCode, url, bodyStr)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Reporter) sendHeartbeat() {
|
func (r *Reporter) sendHeartbeat() {
|
||||||
@@ -192,7 +203,9 @@ func (r *Reporter) sendHeartbeat() {
|
|||||||
|
|
||||||
resp, err := r.client.Do(req)
|
resp, err := r.client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
r.logger.Warn("发送心跳失败", zap.Error(err))
|
r.logger.Warn("发送心跳失败",
|
||||||
|
zap.String("url", url),
|
||||||
|
zap.Error(err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
@@ -260,7 +273,21 @@ func (r *Reporter) sendHeartbeat() {
|
|||||||
}
|
}
|
||||||
r.logger.Debug("心跳发送成功")
|
r.logger.Debug("心跳发送成功")
|
||||||
} else {
|
} else {
|
||||||
r.logger.Warn("心跳发送失败", zap.Int("status", resp.StatusCode))
|
// 读取响应体以便记录错误详情
|
||||||
|
body, err := io.ReadAll(resp.Body)
|
||||||
|
bodyStr := ""
|
||||||
|
if err == nil && len(body) > 0 {
|
||||||
|
// 限制响应体长度,避免日志过长
|
||||||
|
if len(body) > 500 {
|
||||||
|
bodyStr = string(body[:500]) + "..."
|
||||||
|
} else {
|
||||||
|
bodyStr = string(body)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
r.logger.Warn("心跳发送失败",
|
||||||
|
zap.Int("status", resp.StatusCode),
|
||||||
|
zap.String("url", url),
|
||||||
|
zap.String("response_body", bodyStr))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user