chore: refactor
Some checks are pending
CodeQL / Analyze (go) (push) Waiting to run
CodeQL / Analyze (javascript) (push) Waiting to run
Contributors / contributors (push) Waiting to run
Sync / sync-to-jihulab (push) Waiting to run
Run Tests / tests (macos) (push) Waiting to run
Run Tests / tests (ubuntu) (push) Waiting to run
Run Tests / tests (windows) (push) Waiting to run

This commit is contained in:
naiba 2025-01-04 12:32:29 +08:00
parent 81a08a38da
commit 3999d1f99a
3 changed files with 12 additions and 8 deletions

View File

@ -160,15 +160,13 @@ func listServerWithServices(c *gin.Context) ([]uint64, error) {
for _, id := range serverIdsWithService {
singleton.ServerLock.RLock()
server, ok := singleton.ServerList[id]
if !ok {
singleton.ServerLock.RUnlock()
if !ok || server == nil {
return nil, singleton.Localizer.ErrorT("server not found")
}
if !server.HideForGuest || authorized {
ret = append(ret, id)
}
singleton.ServerLock.RUnlock()
}
return ret, nil

View File

@ -75,6 +75,7 @@ func (a *authHandler) Check(ctx context.Context) (uint64, error) {
singleton.ServerList[s.ID] = &s
singleton.ServerUUIDToID[clientUUID] = s.ID
singleton.ServerLock.Unlock()
singleton.ReSortServer()
clientID = s.ID

View File

@ -133,17 +133,22 @@ func (s *NezhaHandler) onReportSystemInfo(c context.Context, r *pb.Host) error {
singleton.ServerLock.RLock()
defer singleton.ServerLock.RUnlock()
server, ok := singleton.ServerList[clientID]
if !ok || server == nil {
return fmt.Errorf("server not found")
}
/**
* 这里的 singleton 中的数据都是关机前的旧数据
* agent 重启时bootTime 变大agent 端会先上报 host 信息然后上报 state 信息
* 这是可以借助上报顺序的空档将停机前的流量统计数据标记下来加到下一个小时的数据点上
*/
if singleton.ServerList[clientID].Host != nil && singleton.ServerList[clientID].Host.BootTime < host.BootTime {
singleton.ServerList[clientID].PrevTransferInSnapshot = singleton.ServerList[clientID].PrevTransferInSnapshot - int64(singleton.ServerList[clientID].State.NetInTransfer)
singleton.ServerList[clientID].PrevTransferOutSnapshot = singleton.ServerList[clientID].PrevTransferOutSnapshot - int64(singleton.ServerList[clientID].State.NetOutTransfer)
if server.Host != nil && server.Host.BootTime < host.BootTime {
server.PrevTransferInSnapshot = server.PrevTransferInSnapshot - int64(server.State.NetInTransfer)
server.PrevTransferOutSnapshot = server.PrevTransferOutSnapshot - int64(server.State.NetOutTransfer)
}
singleton.ServerList[clientID].Host = &host
server.Host = &host
return nil
}