Files
mirror-3x-ui/internal/web/service/client_create_dispatch.go
T
Hamed Sanaei da015758c0 perf(clients): batch client creation by destination node
Group compatible committed adds into the existing multi-inbound request. Keep lane ordering, preserve target-specific payloads, and bound remote delivery across the create so unavailable nodes can converge through reconciliation.
2026-12-02 20:08:47 +03:30

142 lines
4.1 KiB
Go

package service
import (
"context"
"encoding/json"
"time"
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
"github.com/mhsanaei/3x-ui/v3/internal/logger"
"github.com/mhsanaei/3x-ui/v3/internal/web/runtime"
)
type queuedClientCreate struct {
turn *nodeConfigTurn
remote *runtime.Remote
tag string
client string
advance func()
}
func (q *queuedClientCreate) prepare(turn *nodeConfigTurn, remote *runtime.Remote, previous, inbound *model.Inbound, client string, prunedOrphans bool) {
q.turn, q.remote, q.tag, q.client = turn, remote, inbound.Tag, client
if !prunedOrphans {
q.advance = remote.PreparePushedInboundAdvance(previous, inbound)
}
}
func (s *ClientService) pushClientCreates(parent context.Context, pushes []queuedClientCreate) (bool, error) {
ctx, cancel := context.WithTimeout(parent, nodeClientPushTimeout)
defer cancel()
byNode := make(map[int][]*queuedClientCreate)
var nodeOrder []int
for i := range pushes {
push := &pushes[i]
if push.turn == nil {
continue
}
nodeID := push.turn.nodeID
if len(byNode[nodeID]) == 0 {
nodeOrder = append(nodeOrder, nodeID)
}
byNode[nodeID] = append(byNode[nodeID], push)
}
applies := make([]inboundApply, 0, len(byNode))
for _, nodeID := range nodeOrder {
group := byNode[nodeID]
applies = append(applies, inboundApply{id: group[0].turn.inboundID, nodeID: &nodeID, run: func() (bool, error) {
s.pushClientCreatesOnNode(ctx, group)
return false, nil
}})
}
return fanoutInboundApplies(applies)
}
func (s *ClientService) pushClientCreatesOnNode(ctx context.Context, pending []*queuedClientCreate) {
queue := pending[0].turn.queue
nodeID := pending[0].turn.nodeID
defer func() {
for _, push := range pending {
push.turn.release()
}
}()
for len(pending) > 0 {
if err := ctx.Err(); err != nil {
s.recordClientNodePush(nodeID, true, runtime.ClientApplyResult{}, err)
return
}
changed := queue.changes()
type batchKey struct {
remote *runtime.Remote
client string
}
var batches [][]*queuedClientCreate
positions := make(map[batchKey]int)
var waiting []*queuedClientCreate
for _, push := range pending {
select {
case <-push.turn.ready:
key := batchKey{remote: push.remote, client: push.client}
i, found := positions[key]
if !found {
i = len(batches)
positions[key] = i
batches = append(batches, nil)
}
batches[i] = append(batches[i], push)
default:
waiting = append(waiting, push)
}
}
if len(batches) == 0 {
select {
case <-changed:
case <-ctx.Done():
}
continue
}
// Release ready lanes before waiting for lanes behind a node-wide turn.
for _, batch := range batches {
s.applyClientCreateBatch(ctx, nodeID, queue, batch)
for _, push := range batch {
push.turn.release()
}
}
pending = waiting
}
}
func (s *ClientService) applyClientCreateBatch(ctx context.Context, nodeID int, queue *nodeConfigQueue, batch []*queuedClientCreate) {
if err := ctx.Err(); err != nil {
s.recordClientNodePush(nodeID, true, runtime.ClientApplyResult{}, err)
return
}
if !queue.acquireLive() {
s.recordClientNodePush(nodeID, true, runtime.ClientApplyResult{}, errClientNodePushBusy)
return
}
defer queue.releaseLive()
tags := make([]string, len(batch))
for i, push := range batch {
tags[i] = push.tag
}
remote := batch[0].remote
started := time.Now()
result, acknowledged, err := remote.AddClientGroup(ctx, tags, json.RawMessage(batch[0].client))
if err != nil {
s.recordClientNodePush(nodeID, true, runtime.ClientApplyResult{}, err)
logger.Warning("Error applying clients on", remote.Name(), ":", err)
}
for i, push := range batch {
if i >= len(acknowledged) || !acknowledged[i] || push.advance == nil {
remote.ForgetPushedInbound(push.tag)
s.recordClientNodePush(nodeID, i < len(acknowledged) && acknowledged[i] && result.DownstreamPending, result, nil)
continue
}
push.advance()
s.recordClientNodePush(nodeID, true, result, nil)
}
logger.Debugf("client create node apply: node_id=%d inbounds=%d rpc_ms=%d", nodeID, len(tags), time.Since(started).Milliseconds())
}