diff --git a/build/nginx/entrypoint.sh b/build/nginx/entrypoint.sh index 594f658e..ef418327 100644 --- a/build/nginx/entrypoint.sh +++ b/build/nginx/entrypoint.sh @@ -10,8 +10,6 @@ set -o pipefail 2>/dev/null || true : "${NGINX_MULTI_ACCEPT:=true}" # Set defaults for rate limiting variables if not provided -: "${BLIZZARD_RATE_LIMIT_KEY:=blizzard-rate-limit}" -: "${BLIZZARD_RATE_LIMIT_RETRY_AFTER:=5}" : "${RETRY_AFTER_HEADER:=Retry-After}" : "${UNKNOWN_PLAYER_COOLDOWN_KEY_PREFIX:=unknown-player:cooldown}" : "${UNKNOWN_PLAYERS_CACHE_ENABLED:=true}" @@ -57,7 +55,7 @@ envsubst '${NGINX_WORKER_PROCESSES_VALUE} ${NGINX_WORKER_CONNECTIONS} ${NGINX_MU # Replace placeholders and generate config and lua script from templates envsubst '${RATE_LIMIT_PER_SECOND_PER_IP} ${RATE_LIMIT_PER_IP_BURST} ${MAX_CONNECTIONS_PER_IP} ${RETRY_AFTER_HEADER} ${PROMETHEUS_LUA_SHARED_DICT} ${PROMETHEUS_INIT_WORKER} ${PROMETHEUS_LOG_BY_LUA} ${PROMETHEUS_METRICS_SERVER}' < /etc/nginx/conf.d/default.conf.template > /etc/nginx/conf.d/default.conf -envsubst '${VALKEY_HOST} ${VALKEY_PORT} ${CACHE_TTL_HEADER} ${BLIZZARD_RATE_LIMIT_KEY} ${BLIZZARD_RATE_LIMIT_RETRY_AFTER} ${RETRY_AFTER_HEADER} ${UNKNOWN_PLAYER_COOLDOWN_KEY_PREFIX} ${UNKNOWN_PLAYERS_CACHE_ENABLED}' < /usr/local/openresty/lualib/valkey_handler.lua.template > /usr/local/openresty/lualib/valkey_handler.lua +envsubst '${VALKEY_HOST} ${VALKEY_PORT} ${CACHE_TTL_HEADER} ${RETRY_AFTER_HEADER} ${UNKNOWN_PLAYER_COOLDOWN_KEY_PREFIX} ${UNKNOWN_PLAYERS_CACHE_ENABLED}' < /usr/local/openresty/lualib/valkey_handler.lua.template > /usr/local/openresty/lualib/valkey_handler.lua # Check OpenResty config before starting openresty -t diff --git a/build/nginx/lua/valkey_handler.lua.template b/build/nginx/lua/valkey_handler.lua.template index a8414b72..15a899d0 100644 --- a/build/nginx/lua/valkey_handler.lua.template +++ b/build/nginx/lua/valkey_handler.lua.template @@ -44,24 +44,6 @@ local function check_unknown_player(valk, uri) return true end -local function check_rate_limit(valk) - local exists, err = valk:exists("${BLIZZARD_RATE_LIMIT_KEY}") - if err then - ngx.log(ngx.ERR, "Failed to check rate limit key: ", err) - return false - end - if exists ~= 1 then return false end - - local ttl = valk:ttl("${BLIZZARD_RATE_LIMIT_KEY}") - if not ttl or ttl < 0 then ttl = ${BLIZZARD_RATE_LIMIT_RETRY_AFTER} end - - ngx.log(ngx.WARN, "Blizzard rate limit active, returning 429 from nginx") - ngx.header["${RETRY_AFTER_HEADER}"] = ttl - ngx.status = ngx.HTTP_TOO_MANY_REQUESTS - ngx.say('{"error":"API has been rate limited by Blizzard, please wait for ' .. ttl .. ' seconds before retrying"}') - return true -end - local function handle_valkey_request() if EXCLUDED_PATHS[ngx.var.uri] then return ngx.exec("@fallback") @@ -89,7 +71,7 @@ local function handle_valkey_request() local compressed_value, cache_ttl = res[1], res[2] if not compressed_value or compressed_value == ngx.null then - if (UNKNOWN_PLAYERS_CACHE_ENABLED and check_unknown_player(valk, ngx.var.uri)) or check_rate_limit(valk) then + if UNKNOWN_PLAYERS_CACHE_ENABLED and check_unknown_player(valk, ngx.var.uri) then release(valk) return end