-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_network_functions
48 lines (41 loc) · 1.28 KB
/
_network_functions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# Determine if a listener is active (useful for non-http endpoints)
function _listener_ready() {
local endpoint=$1
if ! shift 1; then echo "ERROR: ${FUNCNAME[0]}() Missing argument"; return 1; fi
curl -sf --max-time 1 "gopher://${endpoint}"
# '28 Operation timeout' means that curl WAS able to connect
[[ $? == 28 ]]
}
# Sleep until listener becomes active
function _wait_for_listener() {
local endpoint=$1
if ! shift 1; then echo "ERROR: ${FUNCNAME[0]}() Missing argument"; return 1; fi
echo -n "Waiting for ${endpoint} "
local -i sec=0
while ! _listener_ready ${endpoint}; do
echo -n "."
sleep 1
sec+=1
done
echo "(${sec}s)"
}
function _endpoint_ready() {
local url=$1
if ! shift 1; then echo "ERROR: ${FUNCNAME[0]}() Missing argument"; return 1; fi
curl -sf --max-time 1 "${url}" > /dev/null 2>&1
}
# Sleep until listener becomes active
# Usage: _wait_for_endpoint <uri> [<timeout>]
function _wait_for_endpoint() {
local url=$1
if ! shift 1; then echo "ERROR: ${FUNCNAME[0]}() Missing argument"; return 1; fi
local -i timeout=$2
local -i sec=0
echo -n "Waiting for ${url} "
while ! _endpoint_ready ${url}; do
echo -n "."
sleep 1
sec+=1
done
echo "(${sec}s)"
}