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

Noway to handle timeouts #138

Closed
techgarage-ir opened this issue Dec 28, 2024 · 1 comment
Closed

Noway to handle timeouts #138

techgarage-ir opened this issue Dec 28, 2024 · 1 comment

Comments

@techgarage-ir
Copy link

There's no way to handle the timeouts and when destination doesn't send any answers, it still seems like a success ping.
example code:

		pinger, err := probing.NewPinger(hostname)
		pinger.SetPrivileged(true)
		pinger.OnSetup = func() {
			fmt.Println("Setup")
		}
		pinger.OnSend = func(packet *probing.Packet) {
			fmt.Println("Sent: ", packet)
		}
		pinger.OnSendError = func(packet *probing.Packet, err error) {
			fmt.Errorf("Error sending packet: %v", err)
		}
		pinger.OnRecv = func(packet *probing.Packet) {
			fmt.Println("Received: ", packet)
		}
		pinger.OnRecvError = func(err error) {
			fmt.Errorf("Error receiving packet: %v", err)
		}
		pinger.OnFinish = func(stats *probing.Statistics) {
			fmt.Printf("Ping statistics for %s: rtt -> %.2f / %.2f / %.2f ms packet loss: %.2f%%\n", hostname, stats.MinRtt.Seconds()*1000, stats.AvgRtt.Seconds()*1000, stats.MaxRtt.Seconds()*1000, stats.PacketLoss)
		}
		pinger.OnDuplicateRecv = func(packet *probing.Packet) {
			fmt.Println("Duplicate: ", packet)
		}
		pinger.Count = 4
		pinger.Timeout = 4 * time.Second
		bg := context.Background()
		ct, _ := context.WithTimeout(bg, 4*time.Second)
		err = pinger.RunWithContext(ct) // Blocks until finished.
		if err != nil {
			fmt.Println("ERROR PINGING..")
			fmt.Errorf("Error running pinger: %v", err)
			return false, err.Error(), err
		}
		fmt.Println("DONE!")

it triggers Setup and Recv events but no errors appear in failed pings

@SuperQ
Copy link
Contributor

SuperQ commented Dec 28, 2024

/duplicate #19

@SuperQ SuperQ closed this as completed Dec 28, 2024
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

2 participants