Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions build/nginx/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down Expand Up @@ -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
Expand Down
20 changes: 1 addition & 19 deletions build/nginx/lua/valkey_handler.lua.template
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand Down