package service import ( "container/list" "context" "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 { groups list.List reconciles int } // Inbounds in the same group can push concurrently. A node-wide operation // waits for the preceding group to drain before the next group can run. type nodeConfigGroup struct { position *list.Element exclusive bool inbounds map[int]*list.List } type nodeConfigTurn struct { nodeID int inboundID int queue *nodeConfigQueue group *nodeConfigGroup position *list.Element ready chan struct{} work nodeConfigWork } type nodeConfigWork uint8 const ( nodeClientDelta nodeConfigWork = iota nodeFullClientDelete nodeFullReconcile ) func reserveNodeConfigTurn(nodeID, inboundID int, work nodeConfigWork) *nodeConfigTurn { nodeConfigDispatchMu.Lock() defer nodeConfigDispatchMu.Unlock() q := nodeConfigDispatch[nodeID] if q == nil { q = &nodeConfigQueue{} nodeConfigDispatch[nodeID] = q } // A reconcile can see later commits. It converges overlapping deltas, // while node-wide deletes still run because they also remove client records. if q.reconciles > 0 && work == nodeClientDelta { return nil } exclusive := work != nodeClientDelta if exclusive { inboundID = 0 } var group *nodeConfigGroup if last := q.groups.Back(); last != nil { group = last.Value.(*nodeConfigGroup) } if group == nil || group.exclusive != exclusive { group = &nodeConfigGroup{exclusive: exclusive, inbounds: make(map[int]*list.List)} group.position = q.groups.PushBack(group) } lane := group.inbounds[inboundID] if lane == nil { lane = &list.List{} group.inbounds[inboundID] = lane } turn := &nodeConfigTurn{ nodeID: nodeID, inboundID: inboundID, queue: q, group: group, ready: make(chan struct{}), work: work, } turn.position = lane.PushBack(turn) if work == nodeFullReconcile { q.reconciles++ } if q.groups.Front() == group.position && lane.Front() == turn.position { close(turn.ready) } return turn } func (t *nodeConfigTurn) wait(ctx context.Context) bool { select { case <-t.ready: return ctx.Err() == nil case <-ctx.Done(): return false } } func (t *nodeConfigTurn) release() { nodeConfigDispatchMu.Lock() defer nodeConfigDispatchMu.Unlock() if t.position == nil { return } q, group := t.queue, t.group lane := group.inbounds[t.inboundID] wasReady := q.groups.Front() == group.position && lane.Front() == t.position lane.Remove(t.position) t.position = nil if t.work == nodeFullReconcile { q.reconciles-- } if front := lane.Front(); front != nil { if wasReady { close(front.Value.(*nodeConfigTurn).ready) } return } delete(group.inbounds, t.inboundID) if len(group.inbounds) > 0 { return } q.groups.Remove(group.position) if next := q.groups.Front(); next == nil { delete(nodeConfigDispatch, t.nodeID) } else if wasReady { for _, lane := range next.Value.(*nodeConfigGroup).inbounds { close(lane.Front().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, inbound.Id, work) }) } func (m *inboundClientMutation) push(rt runtime.Runtime, inbound *model.Inbound, apply func(context.Context) bool) { m.unlock() if m.turn == nil { return } waitCtx, stopWaiting := nodePushContext() ready := m.turn.wait(waitCtx) stopWaiting() if !ready { return } ctx, cancel := nodePushContext() defer cancel() complete := false defer func() { if remote, ok := rt.(*runtime.Remote); ok && !complete && m.turn.work != nodeFullClientDelete { remote.ForgetPushedInbound(inbound.Tag) } }() complete = apply(ctx) }