go-ping / ping

ICMP Ping library for Go
MIT License
1.31k stars 344 forks source link

If the host is down ping blocks until the timeout is reached #209

Closed stanimirivanovde closed 2 years ago

stanimirivanovde commented 2 years ago

The pinger doesn't behave the same way as Linux/Windows/Mac ping does. If the host is down (or doesn't exist) the ping command will block indefinitely. It can be forced to quit by setting a very small timeout equal to the number of pings for example:

You can reproduce this by running the command:

go run cmd/ping/ping.go -c 1 -t 10s 1.2.3.4

The way linux/windows/mac works is the timeout of the socket is the interval itself. Ping will try to do another ping every interval number of seconds regardless if it succeeded before or not.

ping 1.2.3.4
PING 1.2.3.4 (1.2.3.4): 56 data bytes
Request timeout for icmp_seq 0
Request timeout for icmp_seq 1
Request timeout for icmp_seq 2
Request timeout for icmp_seq 3
Request timeout for icmp_seq 4

If I configure ping with count=5 and interval=1 I expect it to finish in 5 seconds regardless if the host is up or down. It should try to do 5 pings every interval number of seconds and report back the results. The timeout setting is confusing as it controls the overall runtime of the ping. But we need to be able to control the individual pings as well.