mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-07-08 13:46:08 +00:00
7c12700c7d
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.
114 lines
4.0 KiB
Go
114 lines
4.0 KiB
Go
package sub
|
|
|
|
import (
|
|
"fmt"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/mhsanaei/3x-ui/v3/internal/database"
|
|
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
|
|
)
|
|
|
|
// TestGetSubs_DuplicateSettingsClients_Deduped reproduces #5134: multi-node
|
|
// sync/import drift can leave the same client twice inside an inbound's
|
|
// legacy settings.clients JSON while the normalized client_inbounds table
|
|
// stays clean. The subscription output must still contain one profile per
|
|
// (inbound, client).
|
|
func TestGetSubs_DuplicateSettingsClients_Deduped(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 = "sub-dup"
|
|
const email = "dup@example.com"
|
|
const uuid = "f1b9265f-26a8-4b75-9be2-c64a94b15de1"
|
|
|
|
db := database.GetDB()
|
|
settings := fmt.Sprintf(`{"clients": [
|
|
{"id": %q, "email": %q, "subId": %q, "enable": true},
|
|
{"id": %q, "email": %q, "subId": %q, "enable": true}
|
|
]}`, uuid, email, subId, uuid, email, subId)
|
|
ib := &model.Inbound{
|
|
UserId: 1,
|
|
Tag: "dup-in",
|
|
Enable: true,
|
|
Port: 42001,
|
|
Protocol: model.VLESS,
|
|
Settings: settings,
|
|
StreamSettings: `{"network": "tcp", "security": "none"}`,
|
|
}
|
|
if err := db.Create(ib).Error; err != nil {
|
|
t.Fatalf("seed inbound: %v", err)
|
|
}
|
|
client := &model.ClientRecord{Email: email, SubID: subId, UUID: uuid, Enable: true}
|
|
if err := db.Create(client).Error; err != nil {
|
|
t.Fatalf("seed client: %v", err)
|
|
}
|
|
if err := db.Create(&model.ClientInbound{ClientId: client.Id, InboundId: ib.Id}).Error; err != nil {
|
|
t.Fatalf("seed client_inbound: %v", err)
|
|
}
|
|
|
|
s := NewSubService("")
|
|
links, emails, _, _, err := s.GetSubs(subId, "sub.example.com")
|
|
if err != nil {
|
|
t.Fatalf("GetSubs: %v", err)
|
|
}
|
|
if len(links) != 1 {
|
|
t.Fatalf("links = %d, want 1 (duplicate settings.clients entries must collapse)", len(links))
|
|
}
|
|
if len(emails) != 1 {
|
|
t.Fatalf("emails = %d, want 1, got %v", len(emails), emails)
|
|
}
|
|
// Identity, not just count: the single surviving link must be for this client.
|
|
if !strings.Contains(links[0], uuid) {
|
|
t.Fatalf("surviving link must carry the client uuid %q, got %q", uuid, links[0])
|
|
}
|
|
}
|
|
|
|
// 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"
|
|
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 {
|
|
t.Fatalf("case-differing duplicate emails must dedup to 1 client, got %d", len(got))
|
|
}
|
|
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 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))
|
|
}
|
|
}
|