diff --git a/CubeProxy/lua/init_worker_phase.lua b/CubeProxy/lua/init_worker_phase.lua index 83392f6e..1eb3170f 100644 --- a/CubeProxy/lua/init_worker_phase.lua +++ b/CubeProxy/lua/init_worker_phase.lua @@ -1,3 +1,10 @@ +-- In OpenResty, math.randomseed should be called in the init_worker phase. +-- Without this, all worker processes would start with the same seed (typically 1), +-- causing math.random() to return the same sequence of values across all workers. +-- This is critical for cache TTL jitter and other randomized behaviors to ensure +-- they are truly distributed and don't lead to synchronized stampedes. +math.randomseed(ngx.now() * 1000 + ngx.worker.id()) + local function monitor_cache_usage() local cache_free_space = ngx.shared.local_cache:free_space() ngx.shared.local_cache:set("cache_free_space", cache_free_space)