mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-12-03 16:04:50 +03:00
91d5ff400d
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
636 lines
22 KiB
Go
636 lines
22 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
goruntime "runtime"
|
|
"slices"
|
|
"strings"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/mhsanaei/3x-ui/v3/internal/database"
|
|
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
|
|
"github.com/mhsanaei/3x-ui/v3/internal/web/runtime"
|
|
)
|
|
|
|
func TestCreateAcrossManyInboundsUsesOneEmailSnapshot(t *testing.T) {
|
|
setupBulkDB(t)
|
|
svc := &ClientService{}
|
|
inboundSvc := &InboundService{}
|
|
|
|
const uuid = "bbbbbbbb-1111-2222-3333-555555555555"
|
|
ids := make([]int, 0, 6)
|
|
for i := range 6 {
|
|
ib := mkInbound(t, 23001+i, model.VLESS, `{"clients":[]}`)
|
|
ids = append(ids, ib.Id)
|
|
}
|
|
|
|
if _, err := svc.Create(inboundSvc, &ClientCreatePayload{
|
|
Client: model.Client{Email: "fan@x", ID: uuid, SubID: "sub-fan", Enable: true},
|
|
InboundIds: ids,
|
|
}); err != nil {
|
|
t.Fatalf("Create across %d inbounds: %v", len(ids), err)
|
|
}
|
|
|
|
if n := countClientRecords(t); n != 1 {
|
|
t.Fatalf("client records = %d, want 1", n)
|
|
}
|
|
rec := lookupClientRecord(t, "fan@x")
|
|
if rec.UUID != uuid || rec.SubID != "sub-fan" {
|
|
t.Fatalf("record = {uuid:%q sub:%q}, want {%q sub-fan}", rec.UUID, rec.SubID, uuid)
|
|
}
|
|
for _, id := range ids {
|
|
if !settingsHoldUUID(t, inboundSvc, id, uuid) {
|
|
t.Fatalf("inbound %d settings missing the client", id)
|
|
}
|
|
}
|
|
|
|
linked, err := svc.GetInboundIdsForRecord(rec.Id)
|
|
if err != nil {
|
|
t.Fatalf("GetInboundIdsForRecord: %v", err)
|
|
}
|
|
if len(linked) != len(ids) {
|
|
t.Fatalf("linked inbounds = %d, want %d", len(linked), len(ids))
|
|
}
|
|
}
|
|
|
|
func TestCreateGroupsCompatibleInboundsOnOneRemote(t *testing.T) {
|
|
setupClientNodeOrderDB(t)
|
|
useTestRuntimeManager(t)
|
|
fake := newFakeNodeHTTP(t)
|
|
first := realNodeInbound(t, fake, 40301, nil)
|
|
ids := []int{first.Id}
|
|
remoteIDs := []int{101}
|
|
for i := 1; i < 11; i++ {
|
|
ib := nodeInbound(t, *first.NodeID, 40301+i, nil)
|
|
ids = append(ids, ib.Id)
|
|
remoteIDs = append(remoteIDs, 101+i)
|
|
fake.tags[ib.Tag] = 101 + i
|
|
}
|
|
|
|
svc := (&ClientService{}).TrackNodeApplies()
|
|
const email = "grouped@x"
|
|
const uuid = "bbbbbbbb-1111-2222-3333-555555555555"
|
|
for range 2 {
|
|
_, err := svc.Create(&InboundService{}, &ClientCreatePayload{
|
|
Client: model.Client{Email: email, ID: uuid, SubID: "sub-grouped", Enable: true}, InboundIds: ids,
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
fake.mu.Lock()
|
|
pushes := slices.Clone(fake.clientAdds)
|
|
fake.mu.Unlock()
|
|
if len(pushes) != 1 {
|
|
t.Fatalf("client add RPCs = %d, want one for all 11 inbounds and none on retry", len(pushes))
|
|
}
|
|
slices.Sort(pushes[0].InboundIds)
|
|
if !slices.Equal(pushes[0].InboundIds, remoteIDs) || pushes[0].Client.ID != uuid {
|
|
t.Fatalf("node received wrong IDs or credentials: %+v", pushes[0])
|
|
}
|
|
rec := lookupClientRecord(t, email)
|
|
attached, err := svc.GetInboundIdsForRecord(rec.Id)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
slices.Sort(attached)
|
|
if !slices.Equal(attached, ids) {
|
|
t.Fatalf("local attachments = %v, want %v", attached, ids)
|
|
}
|
|
if r := svc.NodeApplyResponse(); r == nil || r.NodePending || !slices.Equal(r.NodeResults, []ClientNodeApplyOutcome{
|
|
{NodeID: *first.NodeID, Status: "applied"},
|
|
}) {
|
|
t.Fatalf("node outcome = %+v, want applied", r)
|
|
}
|
|
}
|
|
|
|
func TestCreateSplitsFlowAndSendsResolvedInbounds(t *testing.T) {
|
|
setupClientNodeOrderDB(t)
|
|
useTestRuntimeManager(t)
|
|
fake := newFakeNodeHTTP(t)
|
|
raw := realNodeInbound(t, fake, 40321, nil)
|
|
tls := nodeInbound(t, *raw.NodeID, 40322, nil)
|
|
missing := nodeInbound(t, *raw.NodeID, 40323, nil)
|
|
if err := database.GetDB().Model(tls).Update("stream_settings", `{"network":"tcp","security":"tls"}`).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
fake.tags[tls.Tag] = 122
|
|
|
|
svc := (&ClientService{}).TrackNodeApplies()
|
|
const flow = "xtls-rprx-vision"
|
|
_, err := svc.Create(&InboundService{}, &ClientCreatePayload{
|
|
Client: model.Client{Email: "split@x", ID: "bbbbbbbb-1111-2222-3333-555555555555", Flow: flow, Enable: true},
|
|
InboundIds: []int{raw.Id, tls.Id, missing.Id},
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
fake.mu.Lock()
|
|
pushes := slices.Clone(fake.clientAdds)
|
|
fake.mu.Unlock()
|
|
slices.SortFunc(pushes, func(a, b ClientCreatePayload) int { return a.InboundIds[0] - b.InboundIds[0] })
|
|
if len(pushes) != 2 || !slices.Equal(pushes[0].InboundIds, []int{121}) || !slices.Equal(pushes[1].InboundIds, []int{122}) ||
|
|
pushes[0].Client.Flow != "" || pushes[1].Client.Flow != flow {
|
|
t.Fatalf("wrong delivery for mixed flow and missing tag: %+v", pushes)
|
|
}
|
|
res := svc.NodeApplyResponse()
|
|
want := []ClientNodeApplyOutcome{{NodeID: *raw.NodeID, Status: "pending", Reason: "push_failed"}}
|
|
if res == nil || !res.NodePending || !slices.Equal(res.NodeResults, want) {
|
|
t.Fatalf("node outcome = %+v, want %v", res, want)
|
|
}
|
|
_, _, dirty, _, err := (&NodeService{}).NodeSyncState(*raw.NodeID)
|
|
if err != nil || !dirty {
|
|
t.Fatalf("missing inbound must leave a durable reconcile: dirty=%t err=%v", dirty, err)
|
|
}
|
|
}
|
|
|
|
func TestCreateUsesEffectiveAllowedIPsOnRemote(t *testing.T) {
|
|
setupClientNodeOrderDB(t)
|
|
useTestRuntimeManager(t)
|
|
fake := newFakeNodeHTTP(t)
|
|
wg := realNodeInbound(t, fake, 40331, nil)
|
|
awg := nodeInbound(t, *wg.NodeID, 40332, nil)
|
|
for _, inbound := range []struct {
|
|
id int
|
|
protocol model.Protocol
|
|
settings string
|
|
}{
|
|
{id: wg.Id, protocol: model.WireGuard, settings: wgServerSettings()},
|
|
{id: awg.Id, protocol: model.AmneziaWG, settings: amneziawgClientTestSettings},
|
|
} {
|
|
if err := database.GetDB().Model(&model.Inbound{}).Where("id = ?", inbound.id).
|
|
Updates(map[string]any{"protocol": inbound.protocol, "settings": inbound.settings}).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
fake.tags[awg.Tag] = 132
|
|
|
|
_, err := (&ClientService{}).Create(&InboundService{}, &ClientCreatePayload{
|
|
Client: model.Client{
|
|
Email: "split-tunnel@x", Enable: true,
|
|
AllowedIPsByInbound: map[int][]string{wg.Id: {"10.0.0.9/32"}, awg.Id: {"10.8.1.9/32"}},
|
|
},
|
|
InboundIds: []int{wg.Id, awg.Id},
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
fake.mu.Lock()
|
|
pushes := slices.Clone(fake.clientAdds)
|
|
fake.mu.Unlock()
|
|
slices.SortFunc(pushes, func(a, b ClientCreatePayload) int { return a.InboundIds[0] - b.InboundIds[0] })
|
|
if len(pushes) != 2 {
|
|
t.Fatalf("node received %d add requests, want 2 distinct tunnel payloads", len(pushes))
|
|
}
|
|
for i, addr := range []string{"10.0.0.9/32", "10.8.1.9/32"} {
|
|
push := pushes[i]
|
|
if !slices.Equal(push.InboundIds, []int{131 + i}) || !slices.Equal(push.Client.AllowedIPs, []string{addr}) ||
|
|
len(push.Client.AllowedIPsByInbound) != 0 {
|
|
t.Fatalf("tunnel %d payload = %+v, want remote inbound %d and %s", i, push, 131+i, addr)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestCreateDispatchesReadyLaneBeforeBlockedLane(t *testing.T) {
|
|
setupClientNodeOrderDB(t)
|
|
useTestRuntimeManager(t)
|
|
fake := newFakeNodeHTTP(t)
|
|
first := realNodeInbound(t, fake, 40341, nil)
|
|
second := nodeInbound(t, *first.NodeID, 40342, nil)
|
|
fake.tags[second.Tag] = 142
|
|
predecessor := reserveNodeConfigTurn(*first.NodeID, first.Id, nodeClientDelta)
|
|
defer predecessor.release()
|
|
|
|
ctx, cancel := context.WithCancel(t.Context())
|
|
defer cancel()
|
|
done := make(chan struct{})
|
|
var saveErr error
|
|
go func() {
|
|
defer close(done)
|
|
_, saveErr = (&ClientService{}).CreateWithContext(ctx, &InboundService{}, &ClientCreatePayload{
|
|
Client: model.Client{Email: "ordered-add@x", ID: "bbbbbbbb-1111-2222-3333-555555555555", Enable: true},
|
|
InboundIds: []int{first.Id, second.Id},
|
|
})
|
|
}()
|
|
t.Cleanup(func() {
|
|
cancel()
|
|
<-done
|
|
})
|
|
wantRPC := func(remoteID int) {
|
|
t.Helper()
|
|
select {
|
|
case got := <-fake.clientAddEvents:
|
|
if !slices.Equal(got.InboundIds, []int{remoteID}) {
|
|
t.Fatalf("node received inbound IDs %v, want [%d]", got.InboundIds, remoteID)
|
|
}
|
|
case <-time.After(5 * time.Second):
|
|
t.Fatalf("ready inbound %d was not dispatched", remoteID)
|
|
}
|
|
}
|
|
wantRPC(142)
|
|
predecessor.release()
|
|
wantRPC(141)
|
|
select {
|
|
case <-done:
|
|
if saveErr != nil {
|
|
t.Fatal(saveErr)
|
|
}
|
|
case <-time.After(5 * time.Second):
|
|
t.Fatal("create waited after both requests were delivered")
|
|
}
|
|
}
|
|
|
|
func TestCreateCancellationStopsFurtherNodeWaves(t *testing.T) {
|
|
setupClientNodeOrderDB(t)
|
|
useTestRuntimeManager(t)
|
|
const nodeCount = inboundFanoutConcurrency + 1
|
|
fakes := make([]*fakeNodeHTTP, nodeCount)
|
|
var ids []int
|
|
for i := range nodeCount {
|
|
fakes[i] = newFakeNodeHTTP(t)
|
|
fakes[i].setHold(true)
|
|
ids = append(ids, realNodeInbound(t, fakes[i], 40351+i, nil).Id)
|
|
}
|
|
|
|
svc := (&ClientService{}).TrackNodeApplies()
|
|
ctx, cancel := context.WithCancel(t.Context())
|
|
done := make(chan struct{})
|
|
var saveErr error
|
|
go func() {
|
|
defer close(done)
|
|
_, saveErr = svc.CreateWithContext(ctx, &InboundService{}, &ClientCreatePayload{
|
|
Client: model.Client{Email: "cancel-add@x", ID: "bbbbbbbb-1111-2222-3333-555555555555", Enable: true},
|
|
InboundIds: ids,
|
|
})
|
|
}()
|
|
t.Cleanup(func() { cancel(); <-done })
|
|
deadline := time.After(5 * time.Second)
|
|
for _, fake := range fakes[:inboundFanoutConcurrency] {
|
|
select {
|
|
case <-fake.clientAddEvents:
|
|
case <-deadline:
|
|
t.Fatal("the first wave did not reach every admitted node")
|
|
}
|
|
}
|
|
cancel()
|
|
select {
|
|
case <-done:
|
|
if saveErr != nil {
|
|
t.Fatal(saveErr)
|
|
}
|
|
case <-time.After(3 * time.Second):
|
|
t.Fatal("cancelled pushes held up the create")
|
|
}
|
|
if got := fakes[nodeCount-1].hitCount("/clients/add"); got != 0 {
|
|
t.Fatalf("queued node received %d RPCs after request cancellation", got)
|
|
}
|
|
rec := lookupClientRecord(t, "cancel-add@x")
|
|
links, err := svc.GetInboundIdsForRecord(rec.Id)
|
|
if err != nil || len(links) != nodeCount {
|
|
t.Fatalf("cancellation lost a committed membership: %v, err=%v", links, err)
|
|
}
|
|
if res := svc.NodeApplyResponse(); res == nil || !res.NodePending || len(res.NodeResults) != nodeCount {
|
|
t.Fatalf("node outcome = %+v, want all %d pending", res, nodeCount)
|
|
}
|
|
}
|
|
|
|
func TestAttachAcrossManyInboundsUsesOneEmailSnapshot(t *testing.T) {
|
|
setupBulkDB(t)
|
|
svc := &ClientService{}
|
|
inboundSvc := &InboundService{}
|
|
|
|
first := mkInbound(t, 23101, model.VLESS, `{"clients":[]}`)
|
|
if _, err := svc.Create(inboundSvc, &ClientCreatePayload{
|
|
Client: model.Client{Email: "att@x", ID: "cccccccc-1111-2222-3333-666666666666", SubID: "sub-att", Enable: true},
|
|
InboundIds: []int{first.Id},
|
|
}); err != nil {
|
|
t.Fatalf("seed Create: %v", err)
|
|
}
|
|
rec := lookupClientRecord(t, "att@x")
|
|
|
|
ids := []int{first.Id}
|
|
for i := range 4 {
|
|
ib := mkInbound(t, 23102+i, model.VLESS, `{"clients":[]}`)
|
|
ids = append(ids, ib.Id)
|
|
}
|
|
|
|
if _, err := svc.Attach(inboundSvc, rec.Id, ids); err != nil {
|
|
t.Fatalf("Attach across %d inbounds: %v", len(ids), err)
|
|
}
|
|
|
|
if n := countClientRecords(t); n != 1 {
|
|
t.Fatalf("client records after attach = %d, want 1", n)
|
|
}
|
|
linked, err := svc.GetInboundIdsForRecord(rec.Id)
|
|
if err != nil {
|
|
t.Fatalf("GetInboundIdsForRecord: %v", err)
|
|
}
|
|
if len(linked) != len(ids) {
|
|
t.Fatalf("linked inbounds = %d, want %d", len(linked), len(ids))
|
|
}
|
|
}
|
|
|
|
// barrierNodeRuntime holds every AddClient until fanout of them are inside it at
|
|
// once, recording the peak overlap; a sequential caller only ever reaches one.
|
|
type barrierNodeRuntime struct {
|
|
fakeNodeRuntime
|
|
fanout int32
|
|
inFlight atomic.Int32
|
|
maxPar atomic.Int32
|
|
release chan struct{}
|
|
freed atomic.Bool
|
|
expired atomic.Bool
|
|
}
|
|
|
|
func (b *barrierNodeRuntime) free() {
|
|
if b.freed.CompareAndSwap(false, true) {
|
|
close(b.release)
|
|
}
|
|
}
|
|
|
|
func (b *barrierNodeRuntime) AddClient(ctx context.Context, ib *model.Inbound, c model.Client) (runtime.ClientApplyResult, error) {
|
|
n := b.inFlight.Add(1)
|
|
for {
|
|
peak := b.maxPar.Load()
|
|
if n <= peak || b.maxPar.CompareAndSwap(peak, n) {
|
|
break
|
|
}
|
|
}
|
|
if n == b.fanout {
|
|
b.free()
|
|
}
|
|
select {
|
|
case <-b.release:
|
|
case <-time.After(5 * time.Second):
|
|
// Release everyone on the first timeout so a sequential regression
|
|
// fails once instead of stalling for fanout x the wait.
|
|
b.expired.Store(true)
|
|
b.free()
|
|
}
|
|
b.inFlight.Add(-1)
|
|
return b.fakeNodeRuntime.AddClient(ctx, ib, c)
|
|
}
|
|
|
|
func fanoutNodeInbounds(t *testing.T, mgr *runtime.Manager, rt runtime.Runtime, n int, basePort int) []int {
|
|
t.Helper()
|
|
ids := make([]int, 0, n)
|
|
for i := range n {
|
|
node := &model.Node{
|
|
Name: fmt.Sprintf("%s-%d", t.Name(), i), Address: "127.0.0.1", Port: 2096 + i,
|
|
ApiToken: "tok", Enable: true, Status: "online",
|
|
}
|
|
if err := database.GetDB().Create(node).Error; err != nil {
|
|
t.Fatalf("create node %d: %v", i, err)
|
|
}
|
|
mgr.SetRuntimeOverride(node.Id, rt)
|
|
ids = append(ids, nodeInbound(t, node.Id, basePort+i, nil).Id)
|
|
}
|
|
return ids
|
|
}
|
|
|
|
// TestCreateAcrossNodesPushesConcurrently pins that a client spanning several
|
|
// node inbounds pushes to them at once, up to inboundFanoutConcurrency at a time.
|
|
func TestCreateAcrossNodesPushesConcurrently(t *testing.T) {
|
|
setupBulkDB(t)
|
|
startSerializedWriter(t)
|
|
mgr := useTestRuntimeManager(t)
|
|
|
|
const nodes = inboundFanoutConcurrency + 1
|
|
bar := &barrierNodeRuntime{fanout: inboundFanoutConcurrency, release: make(chan struct{})}
|
|
ids := fanoutNodeInbounds(t, mgr, bar, nodes, 40101)
|
|
|
|
if _, err := (&ClientService{}).Create(&InboundService{}, &ClientCreatePayload{
|
|
Client: model.Client{Email: "fanout@x", ID: "11111111-2222-3333-4444-555555555555", SubID: "sub-fanout", Enable: true},
|
|
InboundIds: ids,
|
|
}); err != nil {
|
|
t.Fatalf("Create across %d node inbounds: %v", nodes, err)
|
|
}
|
|
|
|
if got := bar.addClient.Load(); got != nodes {
|
|
t.Fatalf("AddClient pushes = %d, want %d", got, nodes)
|
|
}
|
|
if got := bar.maxPar.Load(); got < 2 || got != inboundFanoutConcurrency {
|
|
t.Fatalf("peak node pushes in flight = %d, want overlap at the %d cap (barrier timed out: %v)",
|
|
got, inboundFanoutConcurrency, bar.expired.Load())
|
|
}
|
|
}
|
|
|
|
// TestCreateRecoversPanicInOneInbound pins that a panicking inbound fails only
|
|
// itself: off the request goroutine nothing else would catch it.
|
|
func TestCreateRecoversPanicInOneInbound(t *testing.T) {
|
|
setupBulkDB(t)
|
|
startSerializedWriter(t)
|
|
mgr := useTestRuntimeManager(t)
|
|
|
|
node := &model.Node{
|
|
Name: t.Name(), Address: "127.0.0.1", Port: 2096,
|
|
ApiToken: "tok", Enable: true, Status: "online",
|
|
}
|
|
if err := database.GetDB().Create(node).Error; err != nil {
|
|
t.Fatalf("create node: %v", err)
|
|
}
|
|
mgr.SetRuntimeOverride(node.Id, &panicNodeRuntime{})
|
|
boom := nodeInbound(t, node.Id, 40201, nil)
|
|
healthy := mkInbound(t, 40202, model.VLESS, `{"clients":[]}`)
|
|
|
|
const uuid = "33333333-4444-5555-6666-777777777777"
|
|
_, err := (&ClientService{}).Create(&InboundService{}, &ClientCreatePayload{
|
|
Client: model.Client{Email: "panic@x", ID: uuid, SubID: "sub-panic", Enable: true},
|
|
InboundIds: []int{boom.Id, healthy.Id},
|
|
})
|
|
if err == nil {
|
|
t.Fatal("a panicking node runtime produced no error")
|
|
}
|
|
if want := fmt.Sprintf("inbound %d: panic:", boom.Id); !strings.Contains(err.Error(), want) {
|
|
t.Fatalf("error %q does not report %q", err, want)
|
|
}
|
|
if !settingsHoldUUID(t, &InboundService{}, healthy.Id, uuid) {
|
|
t.Fatalf("healthy inbound %d did not get the client", healthy.Id)
|
|
}
|
|
}
|
|
|
|
// TestCreateLeavesHwidLimitAloneWhenCreateFails pins that a create the panel
|
|
// reported as failed never rewrites a device cap, so it can never retrim one.
|
|
func TestCreateLeavesHwidLimitAloneWhenCreateFails(t *testing.T) {
|
|
setupBulkDB(t)
|
|
startSerializedWriter(t)
|
|
svc := &ClientService{}
|
|
inboundSvc := &InboundService{}
|
|
|
|
const vipUUID = "44444444-5555-6666-7777-888888888888"
|
|
seed := mkInbound(t, 41401, model.VLESS, `{"clients":[]}`)
|
|
if _, err := svc.Create(inboundSvc, &ClientCreatePayload{
|
|
Client: model.Client{Email: "vip@x", ID: vipUUID, SubID: "sub-vip", Enable: true},
|
|
InboundIds: []int{seed.Id},
|
|
LimitHwid: 3,
|
|
}); err != nil {
|
|
t.Fatalf("seed Create: %v", err)
|
|
}
|
|
|
|
broken := mkInbound(t, 41402, model.VLESS, `{"clients":`)
|
|
if _, err := svc.Create(inboundSvc, &ClientCreatePayload{
|
|
Client: model.Client{Email: "vip@x", ID: vipUUID, SubID: "sub-vip", Enable: true},
|
|
InboundIds: []int{broken.Id},
|
|
LimitHwid: 1,
|
|
}); err == nil {
|
|
t.Fatal("re-adding to an unparsable inbound returned no error")
|
|
}
|
|
|
|
if rec := lookupClientRecord(t, "vip@x"); rec.LimitHwid != 3 {
|
|
t.Fatalf("limit_hwid = %d, want the untouched 3: a failed create retrimmed a live client", rec.LimitHwid)
|
|
}
|
|
|
|
// Same failure with the seeded inbound alongside it: that one is a dedup
|
|
// no-op returning no error, which must not read as "an inbound took it".
|
|
if _, err := svc.Create(inboundSvc, &ClientCreatePayload{
|
|
Client: model.Client{Email: "vip@x", ID: vipUUID, SubID: "sub-vip", Enable: true},
|
|
InboundIds: []int{seed.Id, broken.Id},
|
|
LimitHwid: 1,
|
|
}); err == nil {
|
|
t.Fatal("re-adding over a no-op and an unparsable inbound returned no error")
|
|
}
|
|
if rec := lookupClientRecord(t, "vip@x"); rec.LimitHwid != 3 {
|
|
t.Fatalf("limit_hwid = %d, want the untouched 3: a no-op inbound counted as applied", rec.LimitHwid)
|
|
}
|
|
|
|
// A brand new identity that only partly applies is left uncapped rather than
|
|
// capped, the deliberate safe side: the operator saw the error and retries.
|
|
healthy := mkInbound(t, 41403, model.VLESS, `{"clients":[]}`)
|
|
if _, err := svc.Create(inboundSvc, &ClientCreatePayload{
|
|
Client: model.Client{Email: "fresh@x", ID: "55555555-6666-7777-8888-999999999999", SubID: "sub-fresh", Enable: true},
|
|
InboundIds: []int{healthy.Id, broken.Id},
|
|
LimitHwid: 5,
|
|
}); err == nil {
|
|
t.Fatal("creating over an unparsable inbound returned no error")
|
|
}
|
|
if rec := lookupClientRecord(t, "fresh@x"); rec.LimitHwid != 0 {
|
|
t.Fatalf("limit_hwid = %d, want 0 on a create that failed", rec.LimitHwid)
|
|
}
|
|
}
|
|
|
|
func assertNamesFailedInbounds(t *testing.T, err error, broken []*model.Inbound, healthy *model.Inbound) {
|
|
t.Helper()
|
|
if err == nil {
|
|
t.Fatalf("applying %d unparsable inbounds returned no error", len(broken))
|
|
}
|
|
for _, ib := range broken {
|
|
if want := fmt.Sprintf("inbound %d:", ib.Id); !strings.Contains(err.Error(), want) {
|
|
t.Fatalf("error %q does not name the failing %s", err, want)
|
|
}
|
|
}
|
|
if blamed := fmt.Sprintf("inbound %d:", healthy.Id); strings.Contains(err.Error(), blamed) {
|
|
t.Fatalf("error %q blames the healthy %s", err, blamed)
|
|
}
|
|
}
|
|
|
|
// TestFanoutReportsEveryFailingInbound pins that no inbound aborts the others:
|
|
// each failure names its own inbound, and the healthy ones still get the client.
|
|
func TestFanoutReportsEveryFailingInbound(t *testing.T) {
|
|
const halfBadUUID = "22222222-3333-4444-5555-666666666666"
|
|
|
|
t.Run("create", func(t *testing.T) {
|
|
setupBulkDB(t)
|
|
startSerializedWriter(t)
|
|
svc := &ClientService{}
|
|
inboundSvc := &InboundService{}
|
|
|
|
broken := []*model.Inbound{
|
|
mkInbound(t, 41201, model.VLESS, `{"clients":`),
|
|
mkInbound(t, 41202, model.VLESS, `{"clients":`),
|
|
}
|
|
healthy := mkInbound(t, 41203, model.VLESS, `{"clients":[]}`)
|
|
|
|
_, err := svc.Create(inboundSvc, &ClientCreatePayload{
|
|
Client: model.Client{Email: "halfbad@x", ID: halfBadUUID, SubID: "sub-halfbad", Enable: true},
|
|
InboundIds: []int{broken[0].Id, broken[1].Id, healthy.Id},
|
|
})
|
|
assertNamesFailedInbounds(t, err, broken, healthy)
|
|
if !settingsHoldUUID(t, inboundSvc, healthy.Id, halfBadUUID) {
|
|
t.Fatalf("healthy inbound %d did not get the client", healthy.Id)
|
|
}
|
|
})
|
|
|
|
t.Run("attach", func(t *testing.T) {
|
|
setupBulkDB(t)
|
|
startSerializedWriter(t)
|
|
svc := &ClientService{}
|
|
inboundSvc := &InboundService{}
|
|
|
|
seed := mkInbound(t, 41301, model.VLESS, `{"clients":[]}`)
|
|
if _, err := svc.Create(inboundSvc, &ClientCreatePayload{
|
|
Client: model.Client{Email: "halfbad@x", ID: halfBadUUID, SubID: "sub-halfbad", Enable: true},
|
|
InboundIds: []int{seed.Id},
|
|
}); err != nil {
|
|
t.Fatalf("seed Create: %v", err)
|
|
}
|
|
|
|
broken := []*model.Inbound{
|
|
mkInbound(t, 41302, model.VLESS, `{"clients":`),
|
|
mkInbound(t, 41303, model.VLESS, `{"clients":`),
|
|
}
|
|
healthy := mkInbound(t, 41304, model.VLESS, `{"clients":[]}`)
|
|
|
|
rec := lookupClientRecord(t, "halfbad@x")
|
|
_, err := svc.Attach(inboundSvc, rec.Id, []int{broken[0].Id, broken[1].Id, healthy.Id})
|
|
assertNamesFailedInbounds(t, err, broken, healthy)
|
|
if !settingsHoldUUID(t, inboundSvc, healthy.Id, halfBadUUID) {
|
|
t.Fatalf("healthy inbound %d did not get the client", healthy.Id)
|
|
}
|
|
})
|
|
}
|
|
|
|
// An inbound id costs two bytes of request body, so a repeated one must not cost
|
|
// the panel another copy of the client: 2000 ids x 8 MB of comment was 16 GB.
|
|
func TestRepeatedInboundIdCostsOneCopyOfTheClient(t *testing.T) {
|
|
const commentBytes = 1 << 20
|
|
client := func(email string) model.Client {
|
|
return model.Client{Email: email, Comment: strings.Repeat("x", commentBytes), Enable: true}
|
|
}
|
|
|
|
cases := map[string]func(svc *ClientService, inboundSvc *InboundService, spare int, ids []int) error{
|
|
"Create": func(svc *ClientService, inboundSvc *InboundService, _ int, ids []int) error {
|
|
_, err := svc.Create(inboundSvc, &ClientCreatePayload{Client: client("repeated@x"), InboundIds: ids})
|
|
return err
|
|
},
|
|
"BulkCreate": func(svc *ClientService, inboundSvc *InboundService, _ int, ids []int) error {
|
|
res, _, err := svc.BulkCreate(inboundSvc, []ClientCreatePayload{{Client: client("repeated@x"), InboundIds: ids}})
|
|
if err == nil && res.Created != 1 {
|
|
t.Fatalf("bulk create: created %d, skipped %+v", res.Created, res.Skipped)
|
|
}
|
|
return err
|
|
},
|
|
"Attach": func(svc *ClientService, inboundSvc *InboundService, spare int, ids []int) error {
|
|
if _, err := svc.CreateOne(inboundSvc, spare, client("repeated@x")); err != nil {
|
|
return err
|
|
}
|
|
_, err := svc.AttachByEmail(inboundSvc, "repeated@x", ids)
|
|
return err
|
|
},
|
|
}
|
|
for name, run := range cases {
|
|
t.Run(name, func(t *testing.T) {
|
|
setupBulkDB(t)
|
|
ib := mkInbound(t, 21011, model.VLESS, `{"clients":[]}`)
|
|
spare := mkInbound(t, 21012, model.VLESS, `{"clients":[]}`)
|
|
|
|
var before, after goruntime.MemStats
|
|
goruntime.ReadMemStats(&before)
|
|
if err := run(&ClientService{}, &InboundService{}, spare.Id, slices.Repeat([]int{ib.Id}, 300)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
goruntime.ReadMemStats(&after)
|
|
|
|
if allocated := after.TotalAlloc - before.TotalAlloc; allocated > 100*commentBytes {
|
|
t.Fatalf("allocated %d MiB for a 1 MiB client named on one inbound", allocated>>20)
|
|
}
|
|
if emails := settingsClientEmails(t, ib.Id); len(emails) != 1 {
|
|
t.Fatalf("clients on the inbound = %v, want the one", emails)
|
|
}
|
|
})
|
|
}
|
|
}
|