Files
3x-ui/internal/web/service/global_traffic_postgres_test.go
T
Hamed 64a7697f56 perf(traffic): skip unchanged global writes between heartbeats (#6931)
* perf(traffic): skip unchanged global writes between heartbeats

Persist directional usage changes immediately, including lower resets, while
refreshing identical global totals only at an inclusive one-hour heartbeat.
Use the GORM clock consistently for the existing 24-hour freshness window and
preserve local-only snapshots, quota enforcement, and downstream forwarding.

Fixes #6928

* docs(traffic): describe persisted global heartbeat timestamps

Replace the stale every-push model comment with the change-or-hourly-heartbeat
rule. Keep the local-counter separation, delta accounting, MAX fold and reset
semantics explicit without treating UpdatedAt as a last-push timestamp.

Address the nonblocking review feedback on PR #6931 without rewriting
published history.

Refs #6928
2026-10-25 08:44:05 +01:00

40 lines
1011 B
Go

package service
import (
"os"
"testing"
"gorm.io/gorm"
"github.com/mhsanaei/3x-ui/v3/internal/testpg"
)
func initGlobalHeartbeatDB(t *testing.T) *gorm.DB {
t.Helper()
cleanup, err := testpg.IsolatePackage("global_heartbeat")
if err != nil {
t.Fatal(err)
}
t.Cleanup(cleanup)
return initTrafficTestDB(t)
}
func TestGlobalTrafficHeartbeatPostgres(t *testing.T) {
if os.Getenv("XUI_DB_TYPE") != "postgres" || os.Getenv("XUI_DB_DSN") == "" {
t.Skip("set XUI_DB_TYPE=postgres and XUI_DB_DSN")
}
for _, tc := range []struct {
name string
run func(*testing.T)
}{
{"bounds", TestGlobalTrafficHeartbeatBounds},
{"quota and lifecycle", TestGlobalTrafficHeartbeatQuotaAndLifecycle},
{"forwarding", TestGlobalTrafficHeartbeatForwarding},
{"nullable and batches", TestGlobalTrafficHeartbeatNullableAndBatch},
{"long idle and recovery", TestGlobalTrafficHeartbeatLongIdleAndRecovery},
{"concurrent pairs", TestGlobalTrafficHeartbeatConcurrentPairs},
} {
t.Run(tc.name, tc.run)
}
}