perf+coverage: optimisation pass + coverage push to ≥70%

Performance / resource usage:
- circuit_breaker_metrics: fix data race on failCounters map (RWMutex + double-checked locking)
- server.go: drop user_id and op_name metric labels (Prometheus cardinality bound); de-duplicate extractUserInfo
- graphql.go: gate runtime.ReadMemStats per-request behind ENABLE_ALLOCATION_TRACKING flag (default off)
- graphql.go: collapse two-pass AST scan into single pass; lower-case once
- sanitization.go: cache compiled redaction regexes per pattern via sync.Map; hoist inner constants to pkg vars
- proxy.go: hoist connection/timeout substrings to pkg vars; sentinel errors for static error paths; drop dead Headers map alloc
- metrics_aggregator.go: log-field allocation guarded by Logger.IsLevelEnabled
- logging/logger.go: add IsLevelEnabled helper
- lru_cache.go: 16-shard sharding, FNV-1a routing (concurrent throughput +22%)
- cache/memory/lru_memory_cache.go: gzip compress/decompress moved outside mu.Lock
- rps_tracker.go: RWMutex+uint64 -> atomic.Uint64
- retry_budget.go: drop unused mutex
- api.go: bannedUsersIDs map+RWMutex -> sync.Map (+ snapshot/replace helpers)
- tracing/tracing.go: pkg-level constSpanAttrs, copy-then-append in StartSpanWithAttributes
- admin_dashboard.go: handleStatsWebSocket reuses bytes.Buffer + json.Encoder per connection

Build / runtime:
- Makefile: -ldflags="-s -w" -trimpath, CGO_ENABLED=0 for build (=1 for test recipes)
- Dockerfile + Dockerfile.goreleaser: ENV GOMEMLIMIT=512MiB
- main.go: blank import go.uber.org/automaxprocs (cgroup-aware GOMAXPROCS)
- main.go: PPROF_PORT env var wires net/http/pprof on 127.0.0.1 only with full server timeouts
- README.md: env-var docs + metric-label docs updated; cardinality note

Test coverage push (per package):
- main 51.2% -> 74.7%
- cache 66.3% -> 93.7%
- cache/redis 45.5% -> 98.2%
- tracing 66.7% -> 72.9%
- (cache/memory 91.6%, logging 91.9%, monitoring 77.6%, pkg/pools 100% unchanged)

New test files: coverage_micro_test, coverage_extras_test, server_handlers_test,
api_health_test, admin_dashboard_cluster_test, metrics_aggregator_test, concerns_test,
cache/cache_coverage_test, cache/redis/redis_coverage_test, tracing/tracing_coverage_test.

Bug fix: connection_resilience_test.go TestIntegratedHealthManagement.health_manager_startup
was sync.Once-coupled to InitializeBackendHealth and panicked when another test (e.g. via
parseConfig) had already triggered Once. Use NewBackendHealthManager directly.
This commit is contained in:
2026-04-19 19:49:24 +01:00
parent 65fa936b60
commit c2c75d69c0
36 changed files with 4322 additions and 381 deletions
+30 -51
View File
@@ -18,9 +18,7 @@ func (suite *Tests) Test_PeriodicallyReloadBannedUsers() {
cfg.Api.BannedUsersFile = filepath.Join(os.TempDir(), "banned_users_reload_test.json")
// Initial empty banned users
bannedUsersIDsMutex.Lock()
bannedUsersIDs = make(map[string]string)
bannedUsersIDsMutex.Unlock()
replaceBannedUsers(map[string]string{})
// Create a test version of periodicallyReloadBannedUsers that executes once and signals completion
done := make(chan bool)
@@ -37,9 +35,7 @@ func (suite *Tests) Test_PeriodicallyReloadBannedUsers() {
_ = os.Remove(fmt.Sprintf("%s.lock", cfg.Api.BannedUsersFile))
// Ensure banned users map is empty
bannedUsersIDsMutex.Lock()
bannedUsersIDs = make(map[string]string)
bannedUsersIDsMutex.Unlock()
replaceBannedUsers(map[string]string{})
// Execute reloader once
go testPeriodicallyReloadBannedUsers()
@@ -50,9 +46,7 @@ func (suite *Tests) Test_PeriodicallyReloadBannedUsers() {
assert.NoError(suite.T(), err)
// Safely check the map
bannedUsersIDsMutex.RLock()
mapSize := len(bannedUsersIDs)
bannedUsersIDsMutex.RUnlock()
mapSize := len(snapshotBannedUsers())
// Verify map is still empty
assert.Equal(suite.T(), 0, mapSize)
@@ -70,20 +64,17 @@ func (suite *Tests) Test_PeriodicallyReloadBannedUsers() {
assert.NoError(suite.T(), err)
// Clear the banned users map
bannedUsersIDsMutex.Lock()
bannedUsersIDs = make(map[string]string)
bannedUsersIDsMutex.Unlock()
replaceBannedUsers(map[string]string{})
// Execute reloader once
go testPeriodicallyReloadBannedUsers()
<-done
// Safely check the map
bannedUsersIDsMutex.RLock()
mapSize := len(bannedUsersIDs)
value1 := bannedUsersIDs["test-user-reload-1"]
value2 := bannedUsersIDs["test-user-reload-2"]
bannedUsersIDsMutex.RUnlock()
snap := snapshotBannedUsers()
mapSize := len(snap)
value1 := snap["test-user-reload-1"]
value2 := snap["test-user-reload-2"]
// Verify banned users map was loaded
assert.Equal(suite.T(), 2, mapSize)
@@ -102,19 +93,16 @@ func (suite *Tests) Test_PeriodicallyReloadBannedUsers() {
assert.NoError(suite.T(), err)
// Clear the banned users map
bannedUsersIDsMutex.Lock()
bannedUsersIDs = make(map[string]string)
bannedUsersIDsMutex.Unlock()
replaceBannedUsers(map[string]string{})
// Execute reloader once to load initial data
go testPeriodicallyReloadBannedUsers()
<-done
// Safely check the map
bannedUsersIDsMutex.RLock()
mapSize := len(bannedUsersIDs)
initialValue := bannedUsersIDs["test-user-initial"]
bannedUsersIDsMutex.RUnlock()
snap := snapshotBannedUsers()
mapSize := len(snap)
initialValue := snap["test-user-initial"]
// Verify initial data was loaded
assert.Equal(suite.T(), 1, mapSize)
@@ -134,12 +122,11 @@ func (suite *Tests) Test_PeriodicallyReloadBannedUsers() {
<-done
// Safely check the map
bannedUsersIDsMutex.RLock()
mapSize = len(bannedUsersIDs)
value1 := bannedUsersIDs["test-user-updated-1"]
value2 := bannedUsersIDs["test-user-updated-2"]
_, exists := bannedUsersIDs["test-user-initial"]
bannedUsersIDsMutex.RUnlock()
snap = snapshotBannedUsers()
mapSize = len(snap)
value1 := snap["test-user-updated-1"]
value2 := snap["test-user-updated-2"]
_, exists := snap["test-user-initial"]
// Verify updated data was loaded
assert.Equal(suite.T(), 2, mapSize)
@@ -175,19 +162,16 @@ func (suite *Tests) Test_LoadUnloadBannedUsers() {
// Test loading banned users
suite.Run("load banned users", func() {
// Clear the banned users map
bannedUsersIDsMutex.Lock()
bannedUsersIDs = make(map[string]string)
bannedUsersIDsMutex.Unlock()
replaceBannedUsers(map[string]string{})
// Load banned users
loadBannedUsers()
// Check the banned users map
bannedUsersIDsMutex.RLock()
count := len(bannedUsersIDs)
reason1 := bannedUsersIDs["user1"]
reason2 := bannedUsersIDs["user2"]
bannedUsersIDsMutex.RUnlock()
snap := snapshotBannedUsers()
count := len(snap)
reason1 := snap["user1"]
reason2 := snap["user2"]
assert.Equal(suite.T(), 2, count)
assert.Equal(suite.T(), "reason1", reason1)
@@ -197,32 +181,27 @@ func (suite *Tests) Test_LoadUnloadBannedUsers() {
// Test updating banned users
suite.Run("update banned users", func() {
// Update the banned users map
bannedUsersIDsMutex.Lock()
bannedUsersIDs = map[string]string{
replaceBannedUsers(map[string]string{
"user3": "reason3",
"user4": "reason4",
}
bannedUsersIDsMutex.Unlock()
})
// Store the updated banned users
err := storeBannedUsers()
assert.NoError(suite.T(), err)
// Clear the banned users map
bannedUsersIDsMutex.Lock()
bannedUsersIDs = make(map[string]string)
bannedUsersIDsMutex.Unlock()
replaceBannedUsers(map[string]string{})
// Load banned users again
loadBannedUsers()
// Check the banned users map
bannedUsersIDsMutex.RLock()
count := len(bannedUsersIDs)
reason3 := bannedUsersIDs["user3"]
reason4 := bannedUsersIDs["user4"]
_, user1Exists := bannedUsersIDs["user1"]
bannedUsersIDsMutex.RUnlock()
snap := snapshotBannedUsers()
count := len(snap)
reason3 := snap["user3"]
reason4 := snap["user4"]
_, user1Exists := snap["user1"]
assert.Equal(suite.T(), 2, count)
assert.Equal(suite.T(), "reason3", reason3)