Skip to content

Commit 951b1ef

Browse files
authored
fix(race-condition): use cycle with bigger interval to check FRP Server availability (#55)
* fix: deterministically wait for Python agent and FRP server to start Signed-off-by: Oleksander Piskun <oleksandr2088@icloud.com>
1 parent 7303d6d commit 951b1ef

1 file changed

Lines changed: 50 additions & 2 deletions

File tree

start.sh

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,52 @@ set -e
2424
# We do not generate /certs/cert.pem file, as for HaProxy it is admin task to mount generated cert if needed.
2525
# ----------------------------------------------------------------------------
2626

27+
HP_WAIT_AGENT_HTTP=${HP_WAIT_AGENT_HTTP:-60}
28+
HP_WAIT_SPOA=${HP_WAIT_SPOA:-60}
29+
HP_WAIT_FRP=${HP_WAIT_FRP:-30}
30+
HP_WAIT_INTERVAL=${HP_WAIT_INTERVAL:-0.5}
31+
2732
HP_VERBOSE_START=${HP_VERBOSE_START:-1}
2833
log() {
2934
if [ "$HP_VERBOSE_START" -eq 1 ]; then
3035
echo "$@"
3136
fi
3237
}
3338

39+
wait_for_tcp() {
40+
# $1 host, $2 port, $3 timeout(s), $4 interval(s)
41+
host="$1"; port="$2"; timeout="${3:-30}"; interval="${4:-0.5}"
42+
start_ts="$(date +%s)"
43+
while :; do
44+
if nc -z -w 1 "$host" "$port" >/dev/null 2>&1; then
45+
return 0
46+
fi
47+
now="$(date +%s)"; elapsed=$(( now - start_ts ))
48+
if [ "$elapsed" -ge "$timeout" ]; then
49+
echo "ERROR: Timeout waiting for TCP $host:$port to become ready (after ${timeout}s)."
50+
return 1
51+
fi
52+
sleep "$interval"
53+
done
54+
}
55+
56+
wait_for_http() {
57+
# $1 url, $2 timeout(s), $3 interval(s)
58+
url="$1"; timeout="${2:-60}"; interval="${3:-0.5}"
59+
start_ts="$(date +%s)"
60+
while :; do
61+
if curl -fsS --max-time 2 "$url" >/dev/null 2>&1; then
62+
return 0
63+
fi
64+
now="$(date +%s)"; elapsed=$(( now - start_ts ))
65+
if [ "$elapsed" -ge "$timeout" ]; then
66+
echo "ERROR: Timeout waiting for HTTP $url to become ready (after ${timeout}s)."
67+
return 1
68+
fi
69+
sleep "$interval"
70+
done
71+
}
72+
3473
# Check if the required environment variables are set
3574
if [ -z "$HP_FRP_ADDRESS" ]; then
3675
echo "ERROR: HP_FRP_ADDRESS is not set."
@@ -306,12 +345,21 @@ fi
306345
log "INFO: Starting Python HaProxy Agent on 127.0.0.1:8200 and 127.0.0.1:9600..."
307346
nohup python3 /usr/local/bin/haproxy_agent.py &
308347

309-
sleep 1s
348+
# Wait deterministically for the agent to be ready (HTTP) and for SPOA (TCP)
349+
log "INFO: Waiting for HaRP Agent HTTP (GET http://127.0.0.1:8200/info) to be ready..."
350+
wait_for_http "http://127.0.0.1:8200/info" "$HP_WAIT_AGENT_HTTP" "$HP_WAIT_INTERVAL"
351+
352+
log "INFO: Waiting for SPOA port 127.0.0.1:9600..."
353+
wait_for_tcp "127.0.0.1" "9600" "$HP_WAIT_SPOA" "$HP_WAIT_INTERVAL"
310354

311355
log "INFO: Starting FRP server on ${HP_FRP_ADDRESS}..."
312356
frps -c /frps.toml &
313357

314-
sleep 1s
358+
# Wait for FRP port to be listening before starting frpc
359+
LOCAL_FRP_HOST="$FRP_HOST"
360+
[ "$LOCAL_FRP_HOST" = "0.0.0.0" ] && LOCAL_FRP_HOST="127.0.0.1"
361+
log "INFO: Waiting for FRP server port ${LOCAL_FRP_HOST}:${FRP_PORT}..."
362+
wait_for_tcp "$LOCAL_FRP_HOST" "$FRP_PORT" "$HP_WAIT_FRP" "$HP_WAIT_INTERVAL"
315363

316364
if [ -e "/var/run/docker.sock" ]; then
317365
log "INFO: Starting FRP client for Docker Engine..."

0 commit comments

Comments
 (0)