Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The effect of the timeout option might surprise users #3

Open
djeikyb opened this issue Jun 21, 2017 · 6 comments
Open

The effect of the timeout option might surprise users #3

djeikyb opened this issue Jun 21, 2017 · 6 comments

Comments

@djeikyb
Copy link

djeikyb commented Jun 21, 2017

The timeout is implemented by doing N attempts and waiting one second between each attempt. If the attempt isn't instantaneous, the effective timeout balloons. Some versions of netcat allow you to control the tcp connection timeout. The version of netcat that shipped with my copy of macos sierra takes 75 seconds to timeout by default:

18:00:40]~% time nc google.com 807
nc google.com 807  0.00s user 0.00s system 0% cpu 1:15.25 total
18:02:01]~% time nc google.com 807
nc google.com 807  0.00s user 0.00s system 0% cpu 1:15.08 total
@bgehman
Copy link

bgehman commented Jan 20, 2018

I concur -- using alpine:3.6 docker image:

/app # time nc -z www.google.com 81
Command exited with non-zero status 1
real	2m 7.38s
user	0m 0.00s
sys	0m 0.00s

So, 2m7s multiple by 15 iterations and you are looking at ~30min "hang" with zero output being logged.

@ianfixes
Copy link

What do you get with time nc -w 1 -z www.google.com 81 ?

@bgehman
Copy link

bgehman commented Oct 25, 2018

@ianfixes Easy to test:

$ time docker run --rm -it alpine:3.6 nc -w 1 -z www.google.com 81

0.06s user 0.13s system 4% cpu 4.175 total

@ianfixes
Copy link

Aah, that's a nice one-liner! I put the time command in a different spot and the measurement looks more accurate:

$ docker run --rm -it alpine:3.6 time nc -w 1 -z www.google.com 81
Command exited with non-zero status 1
real	0m 1.00s
user	0m 0.00s
sys	0m 0.00s

@bgehman
Copy link

bgehman commented Oct 25, 2018

FYI, feel free to use my solution:

wait_for() {
  # $1 hostname    $2 port
  n=0
  until [ $n -ge 15 ]; do
    nc -z -w 10 "$1" "$2" && break
    n=$(($n+1))
    echo "`date`: Waiting for $1:$2 to be available... n=$n"
    sleep 10
  done

  if [ $n -ge 15 ]; then
    echo "`date`: ERROR: Timeout out waiting for $1:$2."
    exit 1
  else
    echo "`date`: Dependency $1:$2 is online."
  fi
}

@ianfixes
Copy link

ianfixes commented Oct 25, 2018

The problem is that the nc version that ships with OSX splits the timeout setting into 2 different parameters, the -G one being almost more important (via stackoverflow). -G is unfortunately not very standard.

$ echo $OSTYPE
darwin17
$ time nc -w 1 www.google.com 81

real	1m6.857s
user	0m0.003s
sys	0m0.004s
$ time nc -w 1 -G 1 www.google.com 81

real	0m1.011s
user	0m0.003s
sys	0m0.004s

There's also the timeout command which works great but is not a built-in.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants