perf(clients): batch client creation by destination node (#7367)

Group compatible client creates by destination and effective payload via the existing clients/add API. Resolve node-local inbound IDs, preserve distinct flow/address/credential payloads, bound delivery concurrency and use a shared four-second remote delivery budget after local commits.

Advance reconciliation fingerprints only for acknowledged intended settings, retaining previous-fingerprint CAS rather than stamping unrelated rebased changes. Preserve FIFO turns, node-wide barriers, dirty generations, durable delete supersession, partial and uncertain delivery recovery and downstream outcomes. Protect later reattachments in final Delete cleanup and complete Create metadata before remote delivery.

Validation: all 12 CI jobs passed on 63e70cda38 (run 46935177853). PostgreSQL logs confirm 21 ordering/grouped-create tests and prior reporter-lock, reconciliation and traffic contention gates; node E2E confirms all four version pairings. Author reports SQLite/PostgreSQL race tests, make verify and isolated full make race.

Author-reported paired PostgreSQL 18 benchmark: 11 -> 1 RPC, fixed 200 ms peer delay 11-target p95 1552.0 -> 731.9 ms, timeout probe 12.41–12.42 -> 4.41–4.50 s. Size-dependent peer cost can instead cause pending delivery (1.74 -> 4.51 s with 0.74 s recovery); four stalled nodes can defer a fifth. Monitor production convergence and pending rate. No general production latency or allocation improvement claimed.

Refs #7299
This commit is contained in:
Hamed
2026-12-03 00:08:12 +03:30
committed by GitHub
parent 32e4b8bcfe
commit 91d5ff400d
13 changed files with 1521 additions and 59 deletions
+74 -17
View File
@@ -473,19 +473,21 @@ func stripNodeInboundTagPrefix(nodeID int, tag string) string {
// that lives on only one of the two panels: the node may carry the bare tag
// while the central panel stores the prefixed form, or vice versa.
func (r *Remote) cacheGetTag(tag string) (int, bool) {
if id, ok := r.cacheGet(tag); ok {
r.mu.RLock()
defer r.mu.RUnlock()
return r.cachedTagID(tag)
}
func (r *Remote) cachedTagID(tag string) (int, bool) {
if id, ok := r.remoteIDByTag[tag]; ok {
return id, true
}
prefix := nodeInboundTagPrefix(r.node.Id)
if stripped, found := strings.CutPrefix(tag, prefix); found {
return r.cacheGet(stripped)
tag = stripped
} else {
tag = prefix + tag
}
return r.cacheGet(prefix + tag)
}
func (r *Remote) cacheGet(tag string) (int, bool) {
r.mu.RLock()
defer r.mu.RUnlock()
id, ok := r.remoteIDByTag[tag]
return id, ok
}
@@ -738,13 +740,21 @@ func (r *Remote) AdoptedInboundAliases() []string {
// node held the exact pre-edit state; otherwise the stale fingerprint stays and
// the next reconcile re-sends the full inbound.
func (r *Remote) AdvancePushedInbound(prevIb, ib *model.Inbound) {
r.PreparePushedInboundAdvance(prevIb, ib)()
}
// PreparePushedInboundAdvance keeps large settings blobs out of pending client batches.
func (r *Remote) PreparePushedInboundAdvance(prevIb, ib *model.Inbound) func() {
tag := ib.Tag
prevFP := wireFingerprint(wireInbound(prevIb, r.node.Id))
next := stampPushedInbound(ib, r.node.Id)
r.mu.Lock()
if r.pushedFP[ib.Tag].fingerprint == prevFP {
r.pushedFP[ib.Tag] = next
return func() {
r.mu.Lock()
defer r.mu.Unlock()
if r.pushedFP[tag].fingerprint == prevFP {
r.pushedFP[tag] = next
}
}
r.mu.Unlock()
}
type pushedInboundClient struct {
@@ -813,12 +823,59 @@ func (r *Remote) AddClient(ctx context.Context, ib *model.Inbound, client model.
if err != nil {
return ClientApplyResult{}, fmt.Errorf("remote AddClient: resolve tag %q: %w", ib.Tag, err)
}
payload := map[string]any{
"client": client,
"inboundIds": []int{id},
return remoteClientApplyResult(r.postClientAdd(ctx, []int{id}, client))
}
// AddClientGroup returns the subset of tags acknowledged by one existing add RPC.
func (r *Remote) AddClientGroup(ctx context.Context, tags []string, client json.RawMessage) (ClientApplyResult, []bool, error) {
var refreshErr error
for _, tag := range tags {
if id, ok := r.cacheGetTag(tag); !ok || id <= 0 {
refreshErr = r.refreshRemoteIDs(ctx)
break
}
}
env, err := r.do(ctx, http.MethodPost, "panel/api/clients/add", payload)
return remoteClientApplyResult(env, err)
ids := make([]int, 0, len(tags))
acknowledged := make([]bool, len(tags))
seen := make(map[int]struct{}, len(tags))
var missing []error
r.mu.RLock()
for i, tag := range tags {
id, ok := r.cachedTagID(tag)
if !ok || id <= 0 {
missing = append(missing, fmt.Errorf("%w: tag %q on node %s", errRemoteInboundNotFound, tag, r.node.Name))
continue
}
acknowledged[i] = true
if _, duplicate := seen[id]; !duplicate {
ids = append(ids, id)
seen[id] = struct{}{}
}
}
r.mu.RUnlock()
unresolved := errors.Join(refreshErr, errors.Join(missing...))
if len(ids) == 0 {
return ClientApplyResult{}, nil, unresolved
}
env, err := r.postClientAdd(ctx, ids, client)
if err != nil {
return ClientApplyResult{}, nil, errors.Join(err, unresolved)
}
var ack struct {
NodePending bool `json:"nodePending"`
}
// Legacy success replies omit obj or send null.
if len(env.Obj) != 0 {
if err := json.Unmarshal(env.Obj, &ack); err != nil {
return ClientApplyResult{}, nil, errors.Join(fmt.Errorf("decode grouped client add acknowledgment: %w", err), unresolved)
}
}
return ClientApplyResult{DownstreamPending: ack.NodePending}, acknowledged, unresolved
}
func (r *Remote) postClientAdd(ctx context.Context, ids []int, client any) (*envelope, error) {
payload := map[string]any{"client": client, "inboundIds": ids}
return r.do(ctx, http.MethodPost, "panel/api/clients/add", payload)
}
func (r *Remote) DeleteUser(ctx context.Context, ib *model.Inbound, email string) (ClientApplyResult, error) {