Skip to content

Commit c7a5dcf

Browse files
authored
fix: pass flow field in CreateUser and prevent NaN JSON crash (Ehco1996#427)
1 parent 1c580b5 commit c7a5dcf

3 files changed

Lines changed: 16 additions & 7 deletions

File tree

pkg/metric_reader/node.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,15 @@ func (b *readerImpl) processLoadMetrics(metricMap map[string]*dto.MetricFamily,
130130

131131
func (b *readerImpl) calculateFinalMetrics(nm *NodeMetrics, cpu *cpuStats) {
132132
nm.CpuCoreCount = cpu.cores
133-
nm.CpuUsagePercent = 100 * (cpu.totalTime - cpu.idleTime) / cpu.totalTime
134-
nm.MemoryUsagePercent = 100 * float64(nm.MemoryUsageBytes) / float64(nm.MemoryTotalBytes)
135-
nm.DiskUsagePercent = 100 * float64(nm.DiskUsageBytes) / float64(nm.DiskTotalBytes)
133+
if cpu.totalTime > 0 {
134+
nm.CpuUsagePercent = 100 * (cpu.totalTime - cpu.idleTime) / cpu.totalTime
135+
}
136+
if nm.MemoryTotalBytes > 0 {
137+
nm.MemoryUsagePercent = 100 * float64(nm.MemoryUsageBytes) / float64(nm.MemoryTotalBytes)
138+
}
139+
if nm.DiskTotalBytes > 0 {
140+
nm.DiskUsagePercent = 100 * float64(nm.DiskUsageBytes) / float64(nm.DiskTotalBytes)
141+
}
136142

137143
nm.CpuUsagePercent = math.Round(nm.CpuUsagePercent*100) / 100
138144
nm.MemoryUsagePercent = math.Round(nm.MemoryUsagePercent*100) / 100

pkg/xray/bandwidth_recorder.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,10 @@ func (b *bandwidthRecorder) RecordOnce(ctx context.Context) (uploadIncr float64,
7575
elapsed := now.Sub(b.lastRecordTime).Seconds()
7676
uploadIncr = (send - b.currentSendBytes)
7777
downloadIncr = (recv - b.currentRecvBytes)
78-
b.uploadBandwidthBytes = uploadIncr / elapsed
79-
b.downloadBandwidthBytes = downloadIncr / elapsed
78+
if elapsed > 0 {
79+
b.uploadBandwidthBytes = uploadIncr / elapsed
80+
b.downloadBandwidthBytes = downloadIncr / elapsed
81+
}
8082
}
8183
b.lastRecordTime = now
8284
b.currentRecvBytes = recv

pkg/xray/user.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ func NewUserPool(grpcEndPoint, remoteConfigURL, metricURL string, proxyTags []st
146146
return up
147147
}
148148

149-
func (up *UserPool) CreateUser(userId, level int, password, method, protocol string, enable bool) *User {
149+
func (up *UserPool) CreateUser(userId, level int, password, method, protocol, flow string, enable bool) *User {
150150
up.Lock()
151151
defer up.Unlock()
152152
u := &User{
@@ -157,6 +157,7 @@ func (up *UserPool) CreateUser(userId, level int, password, method, protocol str
157157
Enable: enable,
158158
Method: method,
159159
Protocol: protocol,
160+
Flow: flow,
160161
}
161162
up.users[u.ID] = u
162163
return u
@@ -269,7 +270,7 @@ func (up *UserPool) syncUserConfigsFromServer(ctx context.Context, proxyTag stri
269270
oldUser, found := up.GetUser(newUser.ID)
270271
if !found {
271272
newUser := up.CreateUser(
272-
newUser.ID, newUser.Level, newUser.Password, newUser.Method, newUser.Protocol, newUser.Enable)
273+
newUser.ID, newUser.Level, newUser.Password, newUser.Method, newUser.Protocol, newUser.Flow, newUser.Enable)
273274
if newUser.Enable {
274275
if err := AddInboundUser(ctx, up.proxyClient, proxyTag, newUser); err != nil {
275276
return err

0 commit comments

Comments
 (0)