fix ip report bug

This commit is contained in:
yuzuki999 2022-08-19 22:42:51 +08:00
parent 27b97927c1
commit ca2669a575

View File

@ -108,9 +108,11 @@ func (c *Node) Start() error {
time.Sleep(time.Duration(c.config.UpdatePeriodic) * time.Second) time.Sleep(time.Duration(c.config.UpdatePeriodic) * time.Second)
_ = c.userReportPeriodic.Start() _ = c.userReportPeriodic.Start()
}() }()
c.onlineIpReportPeriodic = &task.Periodic{ if c.config.EnableIpRecorder {
Interval: time.Duration(c.config.UpdatePeriodic) * 30, c.onlineIpReportPeriodic = &task.Periodic{
Execute: c.onlineIpReport, Interval: time.Duration(c.config.IpRecorderConfig.Periodic) * time.Second,
Execute: c.onlineIpReport,
}
} }
log.Printf("[%s: %d] Start report online ip", c.nodeInfo.NodeType, c.nodeInfo.NodeId) log.Printf("[%s: %d] Start report online ip", c.nodeInfo.NodeType, c.nodeInfo.NodeId)
// delay to start onlineIpReport // delay to start onlineIpReport
@ -381,42 +383,41 @@ func (c *Node) userInfoMonitor() (err error) {
} }
} }
userTraffic = nil userTraffic = nil
if !c.config.EnableIpRecorder {
c.server.ClearOnlineIps(c.Tag)
}
runtime.GC() runtime.GC()
return nil return nil
} }
func (c *Node) onlineIpReport() (err error) { func (c *Node) onlineIpReport() (err error) {
if c.config.EnableIpRecorder { onlineIp, err := c.server.GetOnlineIps(c.Tag)
onlineIp, err := c.server.GetOnlineIps(c.Tag) if err != nil {
if err != nil { log.Print(err)
log.Print(err) return nil
return nil }
} rsp, err := resty.New().SetTimeout(time.Duration(c.config.IpRecorderConfig.Timeout) * time.Second).
rsp, err := resty.New().SetTimeout(time.Duration(c.config.IpRecorderConfig.Timeout) * time.Second). R().
R(). SetBody(onlineIp).
SetBody(onlineIp). Post(c.config.IpRecorderConfig.Url +
Post(c.config.IpRecorderConfig.Url + "/api/v1/SyncOnlineIp?token=" +
"/api/v1/SyncOnlineIp?token=" + c.config.IpRecorderConfig.Token)
c.config.IpRecorderConfig.Token) if err != nil {
log.Print(err)
c.server.ClearOnlineIps(c.Tag)
return nil
}
log.Printf("[Node: %d] Report %d online ip", c.nodeInfo.NodeId, len(onlineIp))
if rsp.StatusCode() == 200 {
onlineIp = []dispatcher.UserIp{}
err := json.Unmarshal(rsp.Body(), &onlineIp)
if err != nil { if err != nil {
log.Print(err) log.Print(err)
c.server.ClearOnlineIps(c.Tag) c.server.ClearOnlineIps(c.Tag)
return nil return nil
} }
log.Printf("[Node: %d] Report %d online ip", c.nodeInfo.NodeId, len(onlineIp)) c.server.UpdateOnlineIps(c.Tag, onlineIp)
if rsp.StatusCode() == 200 { log.Printf("[Node: %d] Updated %d online ip", c.nodeInfo.NodeId, len(onlineIp))
onlineIp = []dispatcher.UserIp{}
err := json.Unmarshal(rsp.Body(), &onlineIp)
if err != nil {
log.Print(err)
c.server.ClearOnlineIps(c.Tag)
return nil
}
c.server.UpdateOnlineIps(c.Tag, onlineIp)
log.Printf("[Node: %d] Updated %d online ip", c.nodeInfo.NodeId, len(onlineIp))
} else {
c.server.ClearOnlineIps(c.Tag)
}
} else { } else {
c.server.ClearOnlineIps(c.Tag) c.server.ClearOnlineIps(c.Tag)
} }