mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-09-16 15:17:14 +00:00
perf(clients): apply a multi-inbound client create concurrently
Creating or attaching a client across N inbounds called AddInboundClient once per inbound, strictly one after another. When those inbounds live on different nodes each call is a full node round-trip bounded by the 10s remote timeout, so the request cost the SUM of every node's latency: two nodes felt instant, three took ~13s and timed out bot callers, which is how it surfaced as "two out of four account creations fail". Split the per-inbound preparation from the apply. Preparation stays ordered and single-threaded because fillProtocolDefaults mints the shared credentials on the first inbound and every later one reuses them; the applies then run concurrently, capped at inboundFanoutConcurrency. A 4-node create measured 1.205s -> 0.307s with peak overlap 1 -> 4. Consequences of no longer aborting at the first failing inbound: - Every apply error is tagged with its inbound and the failures are joined, so all of them reach the caller instead of just the first. - The fanout goroutines recover their own panics. Off the request goroutine gin's Recovery no longer covers them, and an unrecovered panic would kill the panel rather than fail one inbound. - A partly-applied call commits clients on the inbounds that succeeded, so the controller and the LDAP job now read needRestart before the error check; otherwise Xray was never flagged for the work that landed. - limitHwid is applied only when every inbound succeeded. Applying it after a failure rewrites limit_hwid and trims the registered devices of an email that already existed, which is silent data loss on an operation the panel reported as failed. Update the API docs for the new partial-application contract and the inbound-tagged error strings.
This commit is contained in:
@@ -80,15 +80,38 @@ func (f *fakeNodeRuntime) ResetClientTraffic(context.Context, *model.Inbound, st
|
||||
func (f *fakeNodeRuntime) ResetInboundTraffic(context.Context, *model.Inbound) error { return nil }
|
||||
func (f *fakeNodeRuntime) ResetAllTraffics(context.Context) error { return nil }
|
||||
|
||||
// setupNodeRuntime wires an online node + a fake runtime override and returns the
|
||||
// node id and the fake so a test can drive the service node-dispatch path without
|
||||
// a network node.
|
||||
func setupNodeRuntime(t *testing.T) (int, *fakeNodeRuntime) {
|
||||
// startSerializedWriter runs the single traffic-writer goroutine for the test, so
|
||||
// concurrent service writes take the serialized path production uses.
|
||||
func startSerializedWriter(t *testing.T) {
|
||||
t.Helper()
|
||||
resetTrafficWriterForTest(t)
|
||||
StartTrafficWriter()
|
||||
}
|
||||
|
||||
// useTestRuntimeManager swaps in a fresh runtime.Manager for the test and puts
|
||||
// the previous one back afterwards, so overrides can't leak between tests.
|
||||
func useTestRuntimeManager(t *testing.T) *runtime.Manager {
|
||||
t.Helper()
|
||||
prev := runtime.GetManager()
|
||||
mgr := runtime.NewManager(runtime.LocalDeps{APIPort: func() int { return 0 }, SetNeedRestart: func() {}})
|
||||
runtime.SetManager(mgr)
|
||||
t.Cleanup(func() { runtime.SetManager(prev) })
|
||||
return mgr
|
||||
}
|
||||
|
||||
// panicNodeRuntime panics on the per-client push, standing in for a bug in the
|
||||
// apply path that would otherwise unwind straight out of a fanout goroutine.
|
||||
type panicNodeRuntime struct{ fakeNodeRuntime }
|
||||
|
||||
func (p *panicNodeRuntime) AddClient(context.Context, *model.Inbound, model.Client) error {
|
||||
panic("boom from node runtime")
|
||||
}
|
||||
|
||||
// setupNodeRuntime wires an online node + a fake runtime override so a test can
|
||||
// drive the service node-dispatch path without a network node.
|
||||
func setupNodeRuntime(t *testing.T) (int, *fakeNodeRuntime) {
|
||||
t.Helper()
|
||||
mgr := useTestRuntimeManager(t)
|
||||
|
||||
node := &model.Node{Name: "n1-" + t.Name(), Address: "127.0.0.1", Port: 2096, ApiToken: "tok", Enable: true, Status: "online"}
|
||||
if err := database.GetDB().Create(node).Error; err != nil {
|
||||
|
||||
Reference in New Issue
Block a user