fix(traffic): report a poll again when its deltas could not be stored

GetTraffic advances StatsLastValues while it reads, so a failed AddTraffic left the baseline at the new counter values and the interval's traffic was gone for good - the failure repeats every tick and the deltas stay lost. The job now snapshots the baseline before polling and rewinds it when the inbound write fails, so the next poll reports the same interval.

The rewind edits the shared map in place: a caller's XrayService copy still points at the same map, so assigning a new one would not take effect.
This commit is contained in:
Leland Jin 2026-11-10 22:45:57 +08:00
parent 8ea641a2cc
commit 31e723fa0d
4 changed files with 84 additions and 0 deletions

View file

@ -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 {

View file

@ -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 —

View file

@ -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 {

View file

@ -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)
}
}