Comments (7)
Forgot to write, that in my code OnIdle function calls p.Stop() after N idle calls.
Regards,
Uros
from go-fastping.
I use RunLoop in the same way and p.Stop() don't stop the loop.
Here a little example:
p := fastping.NewPinger()
ra, err := net.ResolveIPAddr("ip4:icmp", "127.0.0.1")
if err != nil {
fmt.Println(err)
}
p.AddIPAddr(ra)
p.OnRecv = func(addr *net.IPAddr, rtt time.Duration) {
fmt.Println("receive")
p.Stop()
}
p.OnIdle = func() {
fmt.Println("idle")
p.Stop()
}
p.RunLoop()
ticker := time.NewTicker(time.Second * 2)
select {
case <-p.Done():
fmt.Println("loop done")
case <-ticker.C:
fmt.Println("timeout done")
break
}
ticker.Stop()
Stdout result is :
receive
timeout done
I just ping loopback with 2 sec of "timeout" and "loop done" was never printed.
from go-fastping.
I'm facing the same issue, but I see in mainloop this check:
mainloop:
for {
select {
case <-p.ctx.stop:
p.debugln("Run(): <-p.ctx.stop")
So shouldn't
func (p *Pinger) Stop()
return something like
<-p.ctx.stop
instead of
<- p.ctx.done
?
from go-fastping.
cova-fe:
I am almost sure the little bug is fixed by changing line 377 in Stop() function of fastping.go from:
<-p.ctx.done
to:
p.ctx.done <- true
You see, the problem in my opinion is in the fact, that Done() function in fact returns done channel, which must contain a boolean true value if you want to stop the loop.
I think that your loop should look similar to bewiwi's, so checking the p.Done() and not the p.ctx.stop itself.
This patch is tested and the code is running in production.
Regards,
Uros
from go-fastping.
@asmpro Hi, I have a func here which has to run forever runloop and break whenever there is ping failure. The problem is it works fine till it can ping and goes to idle but doesn't stop or exit. Here is the code any help is appreciated. Thanks
func pingIP(ipadd string) {
pinger := fastping.NewPinger()
err := pinger.AddIP(ipadd)
if err != nil {
fmt.Printf("Error adding IP Address")
panic(err)
}
pinger.OnRecv = func(addr *net.IPAddr, rtt time.Duration) {
fmt.Printf("IP Addr: %s receive, RTT: %v\n", addr.String(), rtt)
}
// pinger.MaxRTT = time.Second
// ticker := time.NewTicker(time.Second * 10)
pinger.RunLoop()
select {
case <-pinger.Done():
if err := pinger.Err(); err != nil {
fmt.Printf("Ping failed: %v", err)
}
// case <-ticker.C:
// break
}
// ticker.Stop()
pinger.Stop()
os.Exit(1)
}
from go-fastping.
@suvvari8 Hi,
First patch fastping.go as I mentioned in my first comment.
Then use RunLoop this way:
type PingData struct {
PingReplies uint32
LastPingRTT time.Duration
}
type PingResult map[string]*PingData
p := fastping.NewPinger()
pingRes := PingResult{}
// Add ips
p.AddIPAddr(ip)
pingRes[ip] = &PingData{}
// ... more ips (do not forget to set pingRes for each IP)
// Set nping to number of pings per IP, you wish to process, for example 3
npings := 3
p.OnRecv = func(addr *net.IPAddr, rtt time.Duration) {
addrStr := addr.String()
ips[addrStr].PingReplies++
ips[addrStr].LastPingRTT = rtt
}
pings := 0
p.OnIdle = func() {
pings++
if pings >= npings {
p.Stop()
}
}
p.RunLoop()
<-p.Done()
if err = p.Err(); err != nil {
panic(err)
}
from go-fastping.
I seem to have found the cause of deadlock.
https://github.com/tatsushid/go-fastping/blob/master/fastping.go#L454
Since the code is executed in parallel, the operation time.NewTicker(p.MaxRTT) conflicts with "close (p.ctx.stop)" and when processing anything in
OnIdle = func () {
}
we get deadlock
Solution: do not use blocking operations in OnIdle
pings := 0
p.OnIdle = func() {
pings++
if pings >= npings {
- p.Stop()
+ go p.Stop()
}
}
or not use OnIdle
at all.
from go-fastping.
Related Issues (20)
- Pinger.run() should return immediately when packet recieved HOT 4
- lost return ip
- Is it possible to stop the pinger after OnRecv?
- Getting "socket: permission denied" when running in UDP mode in linux HOT 6
- Is there a plan to add jitter and packet loss calculation?
- tips
- Is there a plan to add MTU discovery?
- AddIP/RemoveIP() AddIPAddr/RemoveIPAddr() could make a panic in concurrent environment
- What is the meaning of UDP ping? HOT 1
- Anyone using this library use this command HOT 6
- dep ensure Solve(): No versions of github.com/tatsushid/go-fastping met constraints
- I can't ping same ip at once time HOT 2
- go-fastping randomly crash with panic: close of closed channel
- icmp AddIPAddr ping lots of ip , return unstable HOT 1
- Ping failed when size is more then 512 on Windows
- there's not output when ip unreachable
- runtime error: invalid memory address or nil pointer dereference
- Support for error callback function
- How to get network interface from the ping result?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from go-fastping.