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.
This commit is contained in:
Hamed Sanaei
2026-12-02 20:08:47 +03:30
parent 32e4b8bcfe
commit da015758c0
11 changed files with 610 additions and 45 deletions
+64 -15
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,10 +823,49 @@ 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 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
}
}
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
}
result, err := r.postClientAdd(ctx, ids, client)
if err != nil {
return result, nil, errors.Join(err, unresolved)
}
return result, acknowledged, unresolved
}
func (r *Remote) postClientAdd(ctx context.Context, ids []int, client any) (ClientApplyResult, error) {
payload := map[string]any{"client": client, "inboundIds": ids}
env, err := r.do(ctx, http.MethodPost, "panel/api/clients/add", payload)
return remoteClientApplyResult(env, err)
}