Retry Telebit client connections

This commit is contained in:
Elias Renman
2026-06-26 14:01:58 +02:00
parent a6e37151ab
commit 3a8a8a50c8
2 changed files with 50 additions and 1 deletions

View File

@@ -7,6 +7,8 @@ CLIENT_ENV="${TELEBIT_CLIENT_ENV:-$ROOT_DIR/telebit-client.env}"
CLIENT_ENV_DIR="${TELEBIT_CLIENT_ENV_DIR:-$ROOT_DIR/.telebit-clients}"
TELEBIT_BIN="${TELEBIT_BIN:-$ROOT_DIR/bin/telebit}"
SIGNJWT_BIN="${SIGNJWT_BIN:-$ROOT_DIR/bin/signjwt}"
TELEBIT_RETRY_DELAY="${TELEBIT_RETRY_DELAY:-5}"
TELEBIT_RETRY_MAX="${TELEBIT_RETRY_MAX:-0}"
usage() {
cat <<'USAGE'
@@ -190,6 +192,46 @@ refresh_auth() {
printf 'Bound %s.%s\n' "$slug" "$DOMAIN"
}
run_client_with_retries() {
env_file="$1"
attempts=0
stop_requested=false
child_pid=""
case "$TELEBIT_RETRY_DELAY" in
""|*[!0-9]*) die "TELEBIT_RETRY_DELAY must be a non-negative integer" ;;
esac
case "$TELEBIT_RETRY_MAX" in
""|*[!0-9]*) die "TELEBIT_RETRY_MAX must be a non-negative integer" ;;
esac
trap 'stop_requested=true; if [ -n "$child_pid" ]; then kill "$child_pid" 2>/dev/null || true; fi' INT TERM
while :; do
"$TELEBIT_BIN" --env "$env_file" &
child_pid=$!
if wait "$child_pid"; then
status=0
else
status=$?
fi
child_pid=""
if [ "$stop_requested" = "true" ]; then
exit "$status"
fi
attempts=$((attempts + 1))
if [ "$TELEBIT_RETRY_MAX" != "0" ] && [ "$attempts" -ge "$TELEBIT_RETRY_MAX" ]; then
printf 'Telebit exited with status %s after %s attempt(s); not retrying.\n' "$status" "$attempts" >&2
exit "$status"
fi
printf 'Telebit exited with status %s; retrying in %ss.\n' "$status" "$TELEBIT_RETRY_DELAY" >&2
sleep "$TELEBIT_RETRY_DELAY" || exit "$?"
done
}
up_client() {
load_server_env
need_bin "$TELEBIT_BIN"
@@ -206,7 +248,7 @@ up_client() {
write_client_env "$slug" "$key" "$port" "$env_file" >/dev/null
unset SECRET DOMAIN TUNNEL_DOMAIN MGMT_URL POSTGRES_PASSWORD TELEBIT_PORT MGMT_PORT VENDOR_ID
cd "$HOME"
exec "$TELEBIT_BIN" --env "$env_file"
run_client_with_retries "$env_file"
}
build_client() {