Skip to content

Commit 8e7ae21

Browse files
authored
fix: DB tunnel fails with high frequency (#893)
1 parent f7e94f0 commit 8e7ae21

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

scripts/tunnel-create.sh

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ PORT="5432"
3939
TARGET_PORT="5432"
4040
TARGET_ACCOUNT=""
4141
DB_INSTANCE=""
42+
RETRY_INTERVAL=5
43+
MAX_RETRIES=12
4244

4345
display_usage() {
4446
printf "\nThis script creates a SSH tunnel between the local machine and a GCP database using a GCP instance deployed in the same VPC."
@@ -137,15 +139,21 @@ fi
137139
# Getting the first IP
138140
ip=$(echo $ips | sed "s/\['\([^']*\)'.*/\1/")
139141

140-
# Wait 15s to allow the machine to be up before creating the tunnel
141-
echo "Sleeping for 15s..."
142-
sleep 15
143-
echo "Compute engine should be ready to use now"
144-
145-
# Creating SSH tunnel
146-
ssh -o StrictHostKeyChecking=no -fN -L ${PORT}:${target_ip}:${TARGET_PORT} ${TARGET_ACCOUNT}@${ip}
142+
# Creating SSH tunnel with retry mechanism
143+
retry_count=0
144+
while [ $retry_count -lt $MAX_RETRIES ]; do
145+
ssh -o StrictHostKeyChecking=no -fN -L ${PORT}:${target_ip}:${TARGET_PORT} ${TARGET_ACCOUNT}@${ip}
146+
if [ $? -eq 0 ]; then
147+
echo "SSH tunnel created successfully"
148+
break
149+
else
150+
echo "Error creating SSH tunnel, retrying in ${RETRY_INTERVAL}s..."
151+
sleep ${RETRY_INTERVAL}
152+
retry_count=$((retry_count + 1))
153+
fi
154+
done
147155

148-
if [ $? -ne 0 ]; then
149-
printf "\nError creating SSH tunnel\n"
156+
if [ $retry_count -eq $MAX_RETRIES ]; then
157+
printf "\nError creating SSH tunnel after ${MAX_RETRIES} attempts\n"
150158
exit 1
151-
fi
159+
fi

0 commit comments

Comments
 (0)