package service import ( "container/list" "sync" "gorm.io/gorm" "github.com/mhsanaei/3x-ui/v3/internal/database/model" "github.com/mhsanaei/3x-ui/v3/internal/web/runtime" ) var ( nodeConfigDispatchMu sync.Mutex nodeConfigDispatch = map[int]*nodeConfigQueue{} ) type nodeConfigQueue struct { turns list.List reconciles int } type nodeConfigTurn struct { nodeID int queue *nodeConfigQueue position *list.Element ready chan struct{} work nodeConfigWork behindReconcile bool } type nodeConfigWork uint8 const ( nodeClientDelta nodeConfigWork = iota nodeFullClientDelete nodeFullReconcile ) // Per-node ordering also covers full client deletes, which can affect sibling inbounds. func reserveNodeConfigTurn(nodeID int, work nodeConfigWork) *nodeConfigTurn { nodeConfigDispatchMu.Lock() defer nodeConfigDispatchMu.Unlock() q := nodeConfigDispatch[nodeID] if q == nil { q = &nodeConfigQueue{} nodeConfigDispatch[nodeID] = q } // A reconcile can see newer commits than its position in the queue. Let it // converge overlapping deltas, retaining node-wide deletes it cannot replay. if q.reconciles > 0 && work == nodeClientDelta { return nil } turn := &nodeConfigTurn{ nodeID: nodeID, queue: q, ready: make(chan struct{}), work: work, behindReconcile: q.reconciles > 0, } turn.position = q.turns.PushBack(turn) if work == nodeFullReconcile { q.reconciles++ } if q.turns.Front() == turn.position { close(turn.ready) } return turn } func (t *nodeConfigTurn) release() { nodeConfigDispatchMu.Lock() defer nodeConfigDispatchMu.Unlock() if t.position == nil { return } q := t.queue wasFirst := t.position == q.turns.Front() q.turns.Remove(t.position) t.position = nil if t.work == nodeFullReconcile { q.reconciles-- } if next := q.turns.Front(); next == nil { delete(nodeConfigDispatch, t.nodeID) } else if wasFirst { close(next.Value.(*nodeConfigTurn).ready) } } type inboundClientMutation struct { lock *inboundMutationLock turn *nodeConfigTurn } func beginInboundClientMutation(inboundID int) *inboundClientMutation { return &inboundClientMutation{lock: lockInbound(inboundID)} } func (m *inboundClientMutation) unlock() { if m.lock != nil { m.lock.Unlock() m.lock = nil } } func (m *inboundClientMutation) close() { m.unlock() if m.turn != nil { m.turn.release() } } func (m *inboundClientMutation) commit(inbound *model.Inbound, work nodeConfigWork, write func(*gorm.DB) error) error { if inbound.NodeID == nil { return runSerializedTx(write) } nodeID := *inbound.NodeID return runSerializedTxThen(func(tx *gorm.DB) error { if err := write(tx); err != nil { return err } return (&NodeService{}).MarkNodeDirtyTx(tx, nodeID) }, func() { m.turn = reserveNodeConfigTurn(nodeID, work) }) } func (m *inboundClientMutation) push(rt runtime.Runtime, inbound *model.Inbound, apply func() bool) { m.unlock() if m.turn == nil { return } <-m.turn.ready complete := false defer func() { if remote, ok := rt.(*runtime.Remote); ok { // A node-wide delete may invalidate newer settings sent by an overlapping // reconcile, including sibling inbounds that this turn did not edit. if m.turn.work == nodeFullClientDelete && (!complete || m.turn.behindReconcile) { remote.ForgetAllPushedInbounds() } else if !complete { remote.ForgetPushedInbound(inbound.Tag) } } }() complete = apply() }