mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-10-26 15:55:02 +03:00
perf(ip-sync): avoid redundant client IP reads and writes (#6930)
The access-log scanner and the cross-node merge stored the same IP list in opposite orders, so neither saw a row as unchanged, and the merge re-read every preloaded row and re-upserted every attribution row each sync cycle. All writers now share one canonical encoder (timestamp, then IP ascending), the first CAS is seeded from the preload, and identical attribution rows are skipped. A create race between the scanner and node sync now merges into the winning row instead of dropping the loser's IPs, which on PostgreSQL also aborted the scanner's whole scan transaction. Closes #6927
This commit is contained in:
@@ -20,6 +20,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/mhsanaei/3x-ui/v3/internal/database"
|
||||
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
|
||||
)
|
||||
|
||||
const settleTimeout = 30 * time.Second
|
||||
@@ -196,6 +197,32 @@ func (p *panel) deleteInboundRow(id int) {
|
||||
}
|
||||
}
|
||||
|
||||
func (p *panel) seedClientIpAttribution(email, ips string) string {
|
||||
p.t.Helper()
|
||||
if p.cmd != nil {
|
||||
p.t.Fatalf("%s: seedClientIpAttribution on a running panel", p.name)
|
||||
}
|
||||
sharedDBMu.Lock()
|
||||
defer sharedDBMu.Unlock()
|
||||
if err := database.InitDB(filepath.Join(p.dir, "db", "x-ui.db")); err != nil {
|
||||
p.t.Fatalf("%s: open db: %v", p.name, err)
|
||||
}
|
||||
defer func() { _ = database.CloseDB() }()
|
||||
db := database.GetDB()
|
||||
var setting model.Setting
|
||||
if err := db.Where("key = ?", "panelGuid").First(&setting).Error; err != nil {
|
||||
p.t.Fatalf("%s: read panelGuid: %v", p.name, err)
|
||||
}
|
||||
if setting.Value == "" {
|
||||
p.t.Fatalf("%s: empty panelGuid", p.name)
|
||||
}
|
||||
row := model.NodeClientIp{NodeGuid: setting.Value, Email: email, Ips: ips}
|
||||
if err := db.Create(&row).Error; err != nil {
|
||||
p.t.Fatalf("%s: seed attribution: %v", p.name, err)
|
||||
}
|
||||
return setting.Value
|
||||
}
|
||||
|
||||
func (p *panel) url() string { return "http://127.0.0.1:" + strconv.Itoa(p.port) }
|
||||
|
||||
type envelope struct {
|
||||
|
||||
Reference in New Issue
Block a user