mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-12-04 00:15:09 +03:00
6f59a79bc2
Avoid repeatedly loading and decoding full inbound settings on the slim UI list path. Read scoped inbound metadata without Settings, reuse immutable projections keyed by database handle, inbound ID and structural revision, and fetch cold or changed rows in bounded batches. Preserve row-version consistency on misses, exclude deleted rows, and bound the LRU to 2,048 entries and 32 MiB of settings content. Existing SQLite/PostgreSQL revision triggers cover structural/direct SQL edits; counter-only changes retain cache reuse. Keep shared-client stats, fallback annotations, global traffic overlays and full-list/detail contracts intact. Add an exact-PASS PostgreSQL regression gate and a production-shaped benchmark with 30 inbounds, 7,100 clients and 115,000 actual membership rows. Validation: [final review](https://github.com/MHSanaei/3x-ui/pull/7287#issuecomment-7360333998), [CI 12/12](https://github.com/MHSanaei/3x-ui/actions/runs/46976935406), PostgreSQL 16 regression PASS, race, frontend 2,311 tests and four node-version pairings. Independent Lenovo SQLite/PG18 tests and [benchmark](https://github.com/MHSanaei/3x-ui/pull/7287#issuecomment-7359646826): stable JSON identical (39,246,921 bytes); warm p95 2670 -> 1031 ms, settings 46,122,401 -> 0 bytes, allocations -36.2%; concurrent structural edits p95 2673 -> 1148 ms, settings bytes -97.1%. Cold full reads remain and allocations increase 1.8%. Eight samples, same-binary baseline, synthetic topology, no production latency guarantee. The reviewed head is one commit behind main solely due to nonoverlapping #7277 dashboard/docs changes; both trees passed their own CI, but combined-tree CI remains to be verified after merge. No schema or wire-format change. Refs #7009
121 lines
3.0 KiB
Go
121 lines
3.0 KiB
Go
package service
|
|
|
|
import (
|
|
"container/list"
|
|
"sync"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
|
|
)
|
|
|
|
const (
|
|
slimSettingsMaxEntries = 2048
|
|
slimSettingsMaxBytes = 32 << 20
|
|
)
|
|
|
|
type slimSettingsKey struct {
|
|
db *gorm.DB
|
|
id int
|
|
revision string
|
|
}
|
|
|
|
type slimSettingsEntry struct {
|
|
key slimSettingsKey
|
|
settings string
|
|
}
|
|
|
|
var slimSettingsCache = struct {
|
|
sync.Mutex
|
|
recency list.List
|
|
entries map[slimSettingsKey]*list.Element
|
|
bytes int
|
|
}{entries: make(map[slimSettingsKey]*list.Element)}
|
|
|
|
func cachedSlimSettings(key slimSettingsKey) (string, bool) {
|
|
if key.revision == "" {
|
|
return "", false
|
|
}
|
|
c := &slimSettingsCache
|
|
c.Lock()
|
|
defer c.Unlock()
|
|
entry := c.entries[key]
|
|
if entry == nil {
|
|
return "", false
|
|
}
|
|
c.recency.MoveToFront(entry)
|
|
return entry.Value.(slimSettingsEntry).settings, true
|
|
}
|
|
|
|
func rememberSlimSettings(key slimSettingsKey, settings string) {
|
|
if key.revision == "" || len(settings) > slimSettingsMaxBytes {
|
|
return
|
|
}
|
|
c := &slimSettingsCache
|
|
c.Lock()
|
|
defer c.Unlock()
|
|
if entry := c.entries[key]; entry != nil {
|
|
c.recency.MoveToFront(entry)
|
|
return
|
|
}
|
|
c.entries[key] = c.recency.PushFront(slimSettingsEntry{key, settings})
|
|
c.bytes += len(settings)
|
|
for len(c.entries) > slimSettingsMaxEntries || c.bytes > slimSettingsMaxBytes {
|
|
oldest := c.recency.Back()
|
|
entry := c.recency.Remove(oldest).(slimSettingsEntry)
|
|
delete(c.entries, entry.key)
|
|
c.bytes -= len(entry.settings)
|
|
}
|
|
}
|
|
|
|
// Structural revision triggers invalidate settings projections even for direct SQL edits.
|
|
// Counters and other row metadata are still read fresh on every request.
|
|
func loadSlimInboundRows(db *gorm.DB, userID int) ([]*model.Inbound, error) {
|
|
var rows []*model.Inbound
|
|
userRows := db.Model(&model.Inbound{}).Where("user_id = ?", userID)
|
|
if err := userRows.Session(&gorm.Session{}).Omit("Settings").Order("id ASC").Find(&rows).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var missing []int
|
|
for _, row := range rows {
|
|
settings, ok := cachedSlimSettings(slimSettingsKey{db, row.Id, row.SnapshotRevision})
|
|
if ok {
|
|
row.Settings = settings
|
|
} else {
|
|
missing = append(missing, row.Id)
|
|
}
|
|
}
|
|
if len(missing) == 0 {
|
|
return rows, nil
|
|
}
|
|
|
|
resolved := make(map[int]*model.Inbound, len(missing))
|
|
for _, ids := range chunkInts(missing, sqlInChunk) {
|
|
var full []*model.Inbound
|
|
if err := userRows.Session(&gorm.Session{}).Where("id IN ?", ids).Find(&full).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
for _, row := range full {
|
|
row.Settings = slimSettingsClients(row.Settings)
|
|
rememberSlimSettings(slimSettingsKey{db, row.Id, row.SnapshotRevision}, row.Settings)
|
|
resolved[row.Id] = row
|
|
}
|
|
}
|
|
|
|
// A miss is replaced with the whole row from its later read, so its metadata
|
|
// and projection share a revision. Exclude rows removed between the reads.
|
|
next := rows[:0]
|
|
miss := 0
|
|
for _, row := range rows {
|
|
if miss < len(missing) && row.Id == missing[miss] {
|
|
miss++
|
|
row = resolved[row.Id]
|
|
}
|
|
if row != nil {
|
|
next = append(next, row)
|
|
}
|
|
}
|
|
return next, nil
|
|
}
|