mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-09-24 18:56:36 +08:00
fix(sub): resolve subscription clients and stats from normalized tables
A subscription fetch inside a large inbound cost seconds because every layer re-parsed the inbound's full settings JSON: getInboundsBySubId preloaded the whole client_traffics table of each matched inbound, matchingClients parsed all clients to filter by subId, and then every per-protocol generator (raw links, JSON outbounds, Clash proxies) parsed the blob again per link — once to find the client by email and once for inbound-level fields like encryption or method. At 500k clients in one inbound that was 13s per raw fetch and 8.5s per JSON fetch; at 100k, 2.6s/1.7s. After this change both cost ~70ms at 100k. matchingClients now resolves through the indexed clients/client_inbounds tables (ListForInboundBySubId, ordered by clients.id like ListForInbound — the same source the running Xray users are built from), and the per-request SubService carries two caches: clientsByInbound, primed by matchingClients/inboundLinks so clientForLink resolves a client without parsing settings (with the old full-parse as fallback, which also fixes the export-all-links path that re-parsed the blob once per client), and settingsByInbound, a once-per-request shallow decode that skips materializing the clients array entirely. The ClientStats preload is replaced by loading only the subscriber's traffic rows (indexed clients.sub_id); statsForClient's per-email DB fallback (#5567) covers any miss, and the case-insensitive email dedupe keeps the #5134 guarantee for case-differing duplicate rows.
This commit is contained in:
@@ -69,19 +69,35 @@ func TestGetSubs_DuplicateSettingsClients_Deduped(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestMatchingClients_DedupsCaseInsensitiveEmail pins the dedup KEY, not just the count:
|
||||
// the two entries differ only by email case, so dropping strings.ToLower (or keying on
|
||||
// another field) yields two clients. The byte-identical dupes above can't catch that.
|
||||
// TestMatchingClients_DedupsCaseInsensitiveEmail pins the dedup KEY, not just the count.
|
||||
// clients.email is unique but case-sensitively so: two rows differing only by email case
|
||||
// can coexist (import drift), and dropping strings.ToLower (or keying on another field)
|
||||
// would emit both. The first row by id must win, matching the old settings-JSON order.
|
||||
func TestMatchingClients_DedupsCaseInsensitiveEmail(t *testing.T) {
|
||||
dbDir := t.TempDir()
|
||||
t.Setenv("XUI_DB_FOLDER", dbDir)
|
||||
if err := database.InitDB(filepath.Join(dbDir, "x-ui.db")); err != nil {
|
||||
t.Fatalf("InitDB: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { _ = database.CloseDB() })
|
||||
|
||||
const subId = "s1"
|
||||
const uuid = "11111111-2222-4333-8444-555555555555"
|
||||
ib := &model.Inbound{
|
||||
Protocol: model.VLESS,
|
||||
Settings: `{"clients":[
|
||||
{"id":"` + uuid + `","email":"Dup@Example.com","subId":"` + subId + `","enable":true},
|
||||
{"id":"` + uuid + `","email":"dup@example.com","subId":"` + subId + `","enable":true}
|
||||
]}`,
|
||||
db := database.GetDB()
|
||||
ib := &model.Inbound{Protocol: model.VLESS, Enable: true, Port: 42002, Tag: "dedup-ci", Settings: `{"clients":[]}`}
|
||||
if err := db.Create(ib).Error; err != nil {
|
||||
t.Fatalf("seed inbound: %v", err)
|
||||
}
|
||||
for _, email := range []string{"Dup@Example.com", "dup@example.com"} {
|
||||
c := &model.ClientRecord{Email: email, SubID: subId, UUID: uuid, Enable: true}
|
||||
if err := db.Create(c).Error; err != nil {
|
||||
t.Fatalf("seed client %q: %v", email, err)
|
||||
}
|
||||
if err := db.Create(&model.ClientInbound{ClientId: c.Id, InboundId: ib.Id}).Error; err != nil {
|
||||
t.Fatalf("seed client_inbound %q: %v", email, err)
|
||||
}
|
||||
}
|
||||
|
||||
s := &SubService{}
|
||||
got := s.matchingClients(ib, subId)
|
||||
if len(got) != 1 {
|
||||
@@ -90,7 +106,7 @@ func TestMatchingClients_DedupsCaseInsensitiveEmail(t *testing.T) {
|
||||
if got[0].Email != "Dup@Example.com" {
|
||||
t.Fatalf("first occurrence must be kept, got %q", got[0].Email)
|
||||
}
|
||||
// A wrong subId must still be excluded (guards the subId filter at service.go:127).
|
||||
// A wrong subId must still be excluded (guards the SQL subId filter).
|
||||
if other := s.matchingClients(ib, "nope"); len(other) != 0 {
|
||||
t.Fatalf("non-matching subId must yield 0 clients, got %d", len(other))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user