diff --git a/internal/web/job/xray_traffic_job.go b/internal/web/job/xray_traffic_job.go index c6051bf7f..77f48e3c0 100644 --- a/internal/web/job/xray_traffic_job.go +++ b/internal/web/job/xray_traffic_job.go @@ -75,13 +75,18 @@ func (j *XrayTrafficJob) Run() { if !j.xrayService.IsXrayRunning() { return } + // The poll advances its delta baseline as it reads, so a failed write has to + // rewind it or this interval's traffic is gone for good. + baseline := j.xrayService.SnapshotTrafficBaseline() traffics, clientTraffics, err := j.xrayService.GetXrayTraffic() if err != nil { return } needRestart0, clientsDisabled, err := j.inboundService.AddTraffic(traffics, clientTraffics) if err != nil { + j.xrayService.RestoreTrafficBaseline(baseline) logger.Warning("add inbound traffic failed:", err) + return } err, needRestart1 := j.outboundService.AddTraffic(traffics, clientTraffics) if err != nil { diff --git a/internal/web/service/xray.go b/internal/web/service/xray.go index 7bc5c5069..baade13fc 100644 --- a/internal/web/service/xray.go +++ b/internal/web/service/xray.go @@ -1260,6 +1260,19 @@ func (s *XrayService) GetXrayTraffic() ([]*xray.Traffic, []*xray.ClientTraffic, return traffic, clientTraffic, nil } +// SnapshotTrafficBaseline captures the delta baseline before a poll so a failed +// write can rewind it; see RestoreTrafficBaseline. +func (s *XrayService) SnapshotTrafficBaseline() map[string]int64 { + return s.xrayAPI.SnapshotStatsBaseline() +} + +// RestoreTrafficBaseline rewinds the delta baseline after a poll whose deltas +// could not be persisted, so the traffic is reported again next tick instead of +// being lost with the baseline that had already advanced. +func (s *XrayService) RestoreTrafficBaseline(baseline map[string]int64) { + s.xrayAPI.RestoreStatsBaseline(baseline) +} + // GetOnlineUsers returns connection-based online users (email + source IPs) // from the running core's online-stats API. ok=false means the API is not // available — xray isn't running or the core predates the online-stats RPCs — diff --git a/internal/xray/api.go b/internal/xray/api.go index 22a866320..6d912085d 100644 --- a/internal/xray/api.go +++ b/internal/xray/api.go @@ -874,6 +874,32 @@ func (x *XrayAPI) GetTraffic() ([]*Traffic, []*ClientTraffic, error) { return mapToSlice(tagTrafficMap), mapToSlice(emailTrafficMap), nil } +// SnapshotStatsBaseline copies the delta baseline. A caller that cannot persist +// the deltas GetTraffic just reported restores this copy, so the next poll +// reports the same interval instead of dropping it. +func (x *XrayAPI) SnapshotStatsBaseline() map[string]int64 { + out := make(map[string]int64, len(x.StatsLastValues)) + for name, value := range x.StatsLastValues { + out[name] = value + } + return out +} + +// RestoreStatsBaseline rewinds the delta baseline in place. It edits the map +// rather than assigning a new one because a caller's copy of the owning struct +// still shares this map. +func (x *XrayAPI) RestoreStatsBaseline(baseline map[string]int64) { + if x.StatsLastValues == nil { + x.StatsLastValues = make(map[string]int64, len(baseline)) + } + for name := range x.StatsLastValues { + delete(x.StatsLastValues, name) + } + for name, value := range baseline { + x.StatsLastValues[name] = value + } +} + // OnlineIP is one source address of a live connection, with the unix time (seconds) // the core last dispatched a link from it. type OnlineIP struct { diff --git a/internal/xray/api_traffic_baseline_test.go b/internal/xray/api_traffic_baseline_test.go new file mode 100644 index 000000000..bb2957466 --- /dev/null +++ b/internal/xray/api_traffic_baseline_test.go @@ -0,0 +1,40 @@ +package xray + +import ( + "testing" + + statsService "github.com/xtls/xray-core/app/stats/command" +) + +// GetTraffic advances its baseline as it reads. When the caller cannot persist +// the deltas (a failed database write), restoring the snapshot has to make the +// next poll report the same interval instead of losing it. +func TestRestoreStatsBaselineReReportsDeltas(t *testing.T) { + api := startFakeStats(t, [][]*statsService.Stat{ + {stat("user>>>alice>>>traffic>>>uplink", 100)}, + {stat("user>>>alice>>>traffic>>>uplink", 180)}, + {stat("user>>>alice>>>traffic>>>uplink", 180)}, + }) + + if _, _, err := api.GetTraffic(); err != nil { + t.Fatalf("baseline poll: %v", err) + } + snapshot := api.SnapshotStatsBaseline() + + _, clients, err := api.GetTraffic() + if err != nil { + t.Fatalf("second poll: %v", err) + } + if got := clientTrafficByEmail(t, clients)["alice"].Up; got != 80 { + t.Fatalf("first delta = %d, want 80", got) + } + + api.RestoreStatsBaseline(snapshot) + _, replayed, err := api.GetTraffic() + if err != nil { + t.Fatalf("replay poll: %v", err) + } + if got := clientTrafficByEmail(t, replayed)["alice"].Up; got != 80 { + t.Fatalf("replayed delta = %d, want the lost 80 to be reported again", got) + } +}