fix(traffic): push depletion changes to nodes off the serial writer

Node I/O on the traffic-accounting path must never stall accounting; the
serial writer states it ("Keep network I/O (node pushes) OUT of fn").

AddTraffic still applied the depletion UpdateInbound for every node
inbound inside the writer closure, one at a time with context.Background.
One hanging node held the single writer for each push, freezing traffic
polls, node snapshot merges and every client edit for the whole wave; a
client shared by 150 nodes expiring could hold it for tens of minutes.
The opt-in restart on client disable then ran node by node on the same
traffic job.

Remote plans now leave the writer and go through nodePushPlan and the 4s
nodePushContext, fanned out like client pushes: an offline or slow node
defers to the reconcile its dirty flag already schedules. The node restart
runs in its own goroutine, since nothing replays or waits on it.

TestTrafficDisableImmediatelyUpdatesNodeRuntime called addTrafficLocked
directly, which pinned the push inside the writer; it now calls AddTraffic
and still requires the push to have landed on return.
This commit is contained in:
Sanaei committed 2026-09-15 19:04:55 +02:00 Unverified
1 parent cfa8350d10
commit ea66aa4971
5 files changed
+215 -31

No files matched your search

+14 -10
View File
@@ -12,6 +12,7 @@ import (
"github.com/mhsanaei/3x-ui/v3/internal/database"
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
"github.com/mhsanaei/3x-ui/v3/internal/logger"
"github.com/mhsanaei/3x-ui/v3/internal/util/common"
"github.com/mhsanaei/3x-ui/v3/internal/web/runtime"
"github.com/mhsanaei/3x-ui/v3/internal/xray"
@@ -1376,17 +1377,20 @@ func (s *InboundService) restartRemoteNodesOnDisable(nodeIDs []int) {
if !restartOnDisable {
return
}
for _, nodeID := range nodeIDs {
nodeIDCopy := nodeID
rt, rtErr := runtime.GetManager().RuntimeFor(&nodeIDCopy)
if rtErr != nil {
logger.Warning("disableInvalidClients: get runtime for node", nodeID, "failed:", rtErr)
continue
// Best-effort and never replayed: a hanging node must not hold the traffic poll.
common.GoRecover("restart-nodes-on-client-disable", func() {
for _, nodeID := range nodeIDs {
nodeIDCopy := nodeID
rt, rtErr := runtime.GetManager().RuntimeFor(&nodeIDCopy)
if rtErr != nil {
logger.Warning("disableInvalidClients: get runtime for node", nodeID, "failed:", rtErr)
continue
}
if rtErr = rt.RestartXray(context.Background()); rtErr != nil {
logger.Warning("disableInvalidClients: restart xray on node", nodeID, "failed:", rtErr)
}
}
if rtErr = rt.RestartXray(context.Background()); rtErr != nil {
logger.Warning("disableInvalidClients: restart xray on node", nodeID, "failed:", rtErr)
}
}
})
}
func (s *InboundService) GetOnlineClients() []string {
+12 -6
View File
@@ -27,18 +27,24 @@ const depletedClientsClause = "reset = 0 and reset_day = 0 and ((total > 0 and u
func (s *InboundService) AddTraffic(inboundTraffics []*xray.Traffic, clientTraffics []*xray.ClientTraffic) (needRestart bool, clientsDisabled bool, err error) {
var disabledNodeIDs []int
var remotePlans []trafficInboundUpdatePlan
err = submitTrafficWrite(func() error {
var inner error
needRestart, clientsDisabled, disabledNodeIDs, inner = s.addTrafficLocked(inboundTraffics, clientTraffics)
needRestart, clientsDisabled, disabledNodeIDs, remotePlans, inner = s.addTrafficLocked(inboundTraffics, clientTraffics)
return inner
})
if err == nil && len(disabledNodeIDs) > 0 {
if err != nil {
return
}
// Off the serial writer: a hanging node must not stall traffic accounting.
needRestart = s.applyTrafficRemotePlans(remotePlans) || needRestart
if len(disabledNodeIDs) > 0 {
s.restartRemoteNodesOnDisable(disabledNodeIDs)
}
return
}
func (s *InboundService) addTrafficLocked(inboundTraffics []*xray.Traffic, clientTraffics []*xray.ClientTraffic) (bool, bool, []int, error) {
func (s *InboundService) addTrafficLocked(inboundTraffics []*xray.Traffic, clientTraffics []*xray.ClientTraffic) (bool, bool, []int, []trafficInboundUpdatePlan, error) {
db := database.GetDB()
// Commit durable traffic before best-effort lifecycle maintenance so helper
// failures cannot discard usage already reported by Xray.
@@ -48,7 +54,7 @@ func (s *InboundService) addTrafficLocked(inboundTraffics []*xray.Traffic, clien
}
return s.addClientTraffic(tx, clientTraffics)
}); err != nil {
return false, false, nil, err
return false, false, nil, nil, err
}
var (
@@ -93,10 +99,10 @@ func (s *InboundService) addTrafficLocked(inboundTraffics []*xray.Traffic, clien
})
if err != nil {
logger.Warning("traffic lifecycle maintenance failed after traffic commit:", err)
return false, false, nil, nil
return false, false, nil, nil, nil
}
needRestart = needRestart || s.applyTrafficMutationBatch(batch)
return needRestart, clientsDisabled, disabledNodeIDs, nil
return needRestart, clientsDisabled, disabledNodeIDs, batch.remotePlans, nil
}
func (s *InboundService) addInboundTraffic(tx *gorm.DB, traffics []*xray.Traffic) error {
+26 -11
View File
@@ -57,22 +57,37 @@ func (b *trafficMutationBatch) markNodesTx(tx *gorm.DB) error {
return nil
}
// applyTrafficRemotePlans is bounded like every per-client node push: the nodes are
// already dirty, so an offline or slow one defers to the reconcile.
func (s *InboundService) applyTrafficRemotePlans(plans []trafficInboundUpdatePlan) bool {
ids := make([]int, len(plans))
for i := range plans {
ids[i] = plans[i].newInbound.Id
}
failed, panics := fanoutInboundResults(ids, inboundFanoutConcurrency, func(i int) bool {
rt, push, _, err := s.nodePushPlan(&plans[i].newInbound)
if err == nil && push {
ctx, cancel := nodePushContext()
err = rt.UpdateInbound(ctx, &plans[i].oldInbound, &plans[i].newInbound)
cancel()
}
if err != nil {
logger.Debug("traffic post-commit remote apply failed:", err)
}
return err != nil
})
needRestart := false
for i := range failed {
needRestart = needRestart || failed[i] || panics[i] != nil
}
return needRestart
}
func (s *InboundService) applyTrafficMutationBatch(b *trafficMutationBatch) bool {
if b == nil {
return false
}
needRestart := false
for i := range b.remotePlans {
plan := &b.remotePlans[i]
rt, err := s.runtimeFor(&plan.newInbound)
if err == nil {
err = rt.UpdateInbound(context.Background(), &plan.oldInbound, &plan.newInbound)
}
if err != nil {
logger.Debug("traffic post-commit remote apply failed:", err)
needRestart = true
}
}
for i := range b.localPlans {
plan := &b.localPlans[i]
if plan.inbound.Protocol == model.MTProto {
Loaded 3 of 5 files, more files were not shown because too many files have changed in this diff. Show more