mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-11-25 20:15:01 +03:00
perf(iplimit): stop scanning after the first limited client (#7200)
The IP-limit job asks every 10 seconds whether any client carries an IP limit. The probe was a COUNT with LIMIT 1, and the limit applies to the one-row aggregate, so the database still read the whole clients table on every run. Ask with SELECT EXISTS instead, so the scan stops at the first client with limit_ip > 0. The predicate and the fail-to-false behaviour are unchanged; the probe test now also runs in the PostgreSQL scanner suite. Closes #7199
This commit is contained in:
@@ -147,15 +147,11 @@ func (j *CheckClientIpJob) collectFromOnlineAPI() (map[string]map[string]int64,
|
||||
return observed, true
|
||||
}
|
||||
|
||||
// hasLimitIp reports whether any client carries an IP limit. It probes the
|
||||
// normalized clients table (limit_ip is synced there by SyncInbound and the
|
||||
// legacy seeder), replacing the old `settings LIKE '%limitIp%'` scan that
|
||||
// loaded and JSON-parsed every inbound's settings blob on each 10s run.
|
||||
// hasLimitIp stops at the first positive limit in the normalized clients table.
|
||||
func (j *CheckClientIpJob) hasLimitIp() bool {
|
||||
db := database.GetDB()
|
||||
var probe int64
|
||||
err := db.Model(&model.ClientRecord{}).Where("limit_ip > 0").Limit(1).Count(&probe).Error
|
||||
return err == nil && probe > 0
|
||||
var exists bool
|
||||
err := database.GetDB().Raw("SELECT EXISTS (SELECT 1 FROM clients WHERE limit_ip > 0)").Scan(&exists).Error
|
||||
return err == nil && exists
|
||||
}
|
||||
|
||||
// loadAllowlist reads the operator's trusted addresses once per scan; a bad
|
||||
|
||||
@@ -457,29 +457,54 @@ func TestGetInboundByEmailRejectsSubstringFallbackMatch(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// hasLimitIp gates every 10s scan on the normalized clients table: a bare
|
||||
// "limitIp":0 in settings JSON (which the old LIKE scan matched and parsed)
|
||||
// must not enable enforcement, while a single clients.limit_ip > 0 row must.
|
||||
func TestHasLimitIp_ProbesClientRecords(t *testing.T) {
|
||||
setupIntegrationDB(t)
|
||||
j := &CheckClientIpJob{}
|
||||
checkLimitIpProbe(t)
|
||||
}
|
||||
|
||||
if j.hasLimitIp() {
|
||||
t.Fatal("hasLimitIp = true on an empty database")
|
||||
func checkLimitIpProbe(t *testing.T) {
|
||||
t.Helper()
|
||||
j := &CheckClientIpJob{}
|
||||
db := database.GetDB()
|
||||
|
||||
expect := func(state string, want bool) {
|
||||
t.Helper()
|
||||
if got := j.hasLimitIp(); got != want {
|
||||
t.Fatalf("%s: hasLimitIp = %t, want %t", state, got, want)
|
||||
}
|
||||
}
|
||||
expect("empty clients table", false)
|
||||
|
||||
seedLinkedInboundWithClient(t, "no-limit", "nolimit@example.com", 0)
|
||||
if j.hasLimitIp() {
|
||||
t.Fatal("hasLimitIp = true with only limit_ip=0 clients")
|
||||
}
|
||||
expect("only zero limits", false)
|
||||
|
||||
limited := &model.ClientRecord{Email: "limited@example.com", LimitIP: 2}
|
||||
if err := database.GetDB().Create(limited).Error; err != nil {
|
||||
t.Fatalf("seed limited client: %v", err)
|
||||
const email = "disabled@example.com"
|
||||
if err := db.Model(&model.ClientRecord{}).Create(map[string]any{
|
||||
"email": email, "limit_ip": -1, "enable": false,
|
||||
}).Error; err != nil {
|
||||
t.Fatalf("seed disabled client: %v", err)
|
||||
}
|
||||
if !j.hasLimitIp() {
|
||||
t.Fatal("hasLimitIp = false with a limit_ip=2 client present")
|
||||
expect("zero and negative limits", false)
|
||||
|
||||
setLimit := func(limit any) {
|
||||
t.Helper()
|
||||
if err := db.Model(&model.ClientRecord{}).Where("email = ?", email).Update("limit_ip", limit).Error; err != nil {
|
||||
t.Fatalf("set limit to %v: %v", limit, err)
|
||||
}
|
||||
}
|
||||
setLimit(nil)
|
||||
expect("NULL and zero limits", false)
|
||||
setLimit(2)
|
||||
expect("positive limit for a disabled, unattached client", true)
|
||||
setLimit(0)
|
||||
expect("last positive limit removed", false)
|
||||
setLimit(2)
|
||||
expect("positive limit restored", true)
|
||||
|
||||
if err := database.CloseDB(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
expect("database read error after a positive result", false)
|
||||
}
|
||||
|
||||
// The mirror of TestUpdateInboundClientIps_ExcessLiveIpIsStillBanned: with the
|
||||
|
||||
@@ -169,6 +169,7 @@ func TestClientIpSyncPostgresScanner(t *testing.T) {
|
||||
{"CanonicalWriters", clientIpWritersPreserveCanonicalBytes},
|
||||
{"CreateConflict", processObservedCreateConflictPreservesUnion},
|
||||
{"ConcurrentWriter", processObservedKeepsNodeSyncWrite},
|
||||
{"LimitProbe", checkLimitIpProbe},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
cleanup, err := testpg.IsolatePackage("job_ip_sync")
|
||||
|
||||
Reference in New Issue
Block a user