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
+57 -1
View File
@@ -3,6 +3,7 @@ package nodee2e
import (
"bytes"
"compress/gzip"
"context"
"encoding/json"
"fmt"
"io"
@@ -11,9 +12,14 @@ import (
"net/http/httputil"
"net/url"
"os"
"slices"
"strconv"
"sync/atomic"
"testing"
"time"
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
"github.com/mhsanaei/3x-ui/v3/internal/web/runtime"
)
func TestNodeSnapshotVersionCompatibility(t *testing.T) {
@@ -39,7 +45,7 @@ func TestNodeSnapshotVersionCompatibility(t *testing.T) {
token := node.mintToken("snapshot-link", "node-sync")
master.start()
node.start()
var lists, compact, full, unchanged atomic.Int32
var lists, compact, full, unchanged, clientAdds atomic.Int32
target, err := url.Parse(node.url())
if err != nil {
t.Fatal(err)
@@ -88,6 +94,8 @@ func TestNodeSnapshotVersionCompatibility(t *testing.T) {
lists.Add(1)
case "/panel/api/inbounds/trafficSnapshot":
compact.Add(1)
case "/panel/api/clients/add":
clientAdds.Add(1)
}
proxy.ServeHTTP(w, req)
}))
@@ -101,10 +109,14 @@ func TestNodeSnapshotVersionCompatibility(t *testing.T) {
t.Fatal(err)
}
inboundPort := freePort(t)
secondaryPort := freePort(t)
var source inboundView
if err := json.Unmarshal(node.call(http.MethodPost, "/panel/api/inbounds/add", vlessInbound("matrix", inboundPort, nil, vlessClient("matrix-first"))), &source); err != nil {
t.Fatal(err)
}
if version.master == current {
node.call(http.MethodPost, "/panel/api/inbounds/add", vlessInbound("matrix-secondary", secondaryPort, nil))
}
master.call(http.MethodPost, "/panel/api/nodes/add", map[string]any{"name": "matrix-node", "scheme": "http", "address": address.Hostname(), "port": port, "basePath": "/", "apiToken": token, "enable": true, "allowPrivateAddress": true})
eventually(t, settleTimeout, "version pair synchronizes and reaches steady state", func() (bool, string) {
ib, ok := master.inboundOnPort(inboundPort)
@@ -136,6 +148,50 @@ func TestNodeSnapshotVersionCompatibility(t *testing.T) {
master.start()
eventually(t, settleTimeout, "master restart reacquires complete structure", func() (bool, string) { return full.Load() > before, fmt.Sprint(full.Load()) })
}
if version.master == current {
first, hasFirst := master.inboundOnPort(inboundPort)
second, hasSecond := master.inboundOnPort(secondaryPort)
if !hasFirst || !hasSecond {
t.Fatal("master did not adopt both inbounds")
}
const email = "matrix-shared-create"
master.call(http.MethodPost, "/panel/api/clients/add", map[string]any{
"client": map[string]any{"email": email, "enable": true}, "inboundIds": []int{first.Id, second.Id},
})
if got := clientAdds.Load(); got != 1 {
t.Fatalf("node-sync received %d add requests, want one", got)
}
var tags []string
for _, port := range []int{inboundPort, secondaryPort} {
if ib, ok := node.inboundOnPort(port); !ok || ib.client(email) == nil {
t.Fatalf("node did not apply the grouped client on port %d", port)
} else {
tags = append(tags, ib.Tag)
}
}
admin := runtime.NewRemote(&model.Node{
Name: "matrix-admin", Scheme: "http", Address: address.Hostname(), Port: port,
BasePath: "/", ApiToken: node.token, Enable: true, AllowPrivateAddress: true,
}, nil)
const adminEmail = "matrix-admin-create"
raw, err := json.Marshal(vlessClient(adminEmail))
if err != nil {
t.Fatal(err)
}
ctx, cancel := context.WithTimeout(t.Context(), 5*time.Second)
defer cancel()
if _, ack, err := admin.AddClientGroup(ctx, tags, raw); err != nil || !slices.Equal(ack, []bool{true, true}) {
t.Fatalf("admin grouped add acknowledged %v, err=%v", ack, err)
}
if got := clientAdds.Load(); got != 2 {
t.Fatalf("admin sent %d add requests, want one", got-1)
}
for _, port := range []int{inboundPort, secondaryPort} {
if ib, ok := node.inboundOnPort(port); !ok || ib.client(adminEmail) == nil {
t.Fatalf("admin grouped client absent on port %d", port)
}
}
}
})
}
}