Skip to content

Commit f8ca034

Browse files
committed
Try to wait for geoip download to fix startup proxy race condition
1 parent 8570df1 commit f8ca034

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

src/api-umbrella/cli/setup.lua

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ local geoip_download_if_missing_or_old = require("api-umbrella.utils.geoip").dow
44
local shell_blocking_capture_combined = require("shell-games").capture_combined
55
local unistd = require "posix.unistd"
66

7+
local sleep = ngx.sleep
8+
79
local function permission_check()
810
local effective_uid = unistd.geteuid()
911
if config["user"] then
@@ -57,9 +59,20 @@ local function ensure_geoip_db()
5759
config["geoip"]["_enabled"] = false
5860
config["geoip"]["_auto_updater_enabled"] = false
5961

60-
local _, err = geoip_download_if_missing_or_old(config)
61-
if err then
62-
ngx.log(ngx.ERR, "geoip database download failed: ", err)
62+
-- Try to wait for geoip to download, since there may be race conditions on
63+
-- startup in downloading via http proxy that's also starting up.
64+
local _
65+
local geoip_err
66+
local timeout_at = ngx.now() + 60
67+
repeat
68+
_, geoip_err = geoip_download_if_missing_or_old(config)
69+
if geoip_err then
70+
sleep(1)
71+
end
72+
until not geoip_err or ngx.now() > timeout_at
73+
74+
if geoip_err then
75+
ngx.log(ngx.ERR, "geoip database download failed: ", geoip_err)
6376
else
6477
config["geoip"]["_enabled"] = true
6578

0 commit comments

Comments
 (0)