mirror of
https://github.com/taigrr/go-fastping
synced 2025-01-18 05:03:15 -08:00
Call procRecv directly from sendICMP
sendICMP is now multiplexed so it doesn't need to pass its receiving packets via a channel. This changes to call procRecv directly from sendICMP for efficiency.
This commit is contained in:
parent
1f74bfd6d1
commit
d7610ec331
28
fastping.go
28
fastping.go
@ -421,7 +421,6 @@ func (p *Pinger) run(once bool) {
|
|||||||
defer conn6.Close()
|
defer conn6.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
recv := make(chan *packet, len(p.addrs))
|
|
||||||
recvCtx := newContext()
|
recvCtx := newContext()
|
||||||
wg := new(sync.WaitGroup)
|
wg := new(sync.WaitGroup)
|
||||||
|
|
||||||
@ -430,7 +429,7 @@ func (p *Pinger) run(once bool) {
|
|||||||
routines := runtime.NumCPU()
|
routines := runtime.NumCPU()
|
||||||
wg.Add(routines)
|
wg.Add(routines)
|
||||||
for i := 0; i < routines; i++ {
|
for i := 0; i < routines; i++ {
|
||||||
go p.recvICMP(conn, recv, recvCtx, wg)
|
go p.recvICMP(conn, recvCtx, wg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -438,7 +437,7 @@ func (p *Pinger) run(once bool) {
|
|||||||
routines := runtime.NumCPU()
|
routines := runtime.NumCPU()
|
||||||
wg.Add(routines)
|
wg.Add(routines)
|
||||||
for i := 0; i < routines; i++ {
|
for i := 0; i < routines; i++ {
|
||||||
go p.recvICMP(conn6, recv, recvCtx, wg)
|
go p.recvICMP(conn6, recvCtx, wg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -471,9 +470,6 @@ mainloop:
|
|||||||
}
|
}
|
||||||
p.debugln("Run(): call sendICMP()")
|
p.debugln("Run(): call sendICMP()")
|
||||||
err = p.sendICMP(conn, conn6)
|
err = p.sendICMP(conn, conn6)
|
||||||
case r := <-recv:
|
|
||||||
p.debugln("Run(): <-recv")
|
|
||||||
p.procRecv(r)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -484,13 +480,6 @@ mainloop:
|
|||||||
p.debugln("Run(): wait recvICMP()")
|
p.debugln("Run(): wait recvICMP()")
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
|
|
||||||
// process all queued results
|
|
||||||
close(recv)
|
|
||||||
for r := range recv {
|
|
||||||
p.debugln("Run(): <-recv")
|
|
||||||
p.procRecv(r)
|
|
||||||
}
|
|
||||||
|
|
||||||
p.mu.Lock()
|
p.mu.Lock()
|
||||||
p.ctx.err = err
|
p.ctx.err = err
|
||||||
p.mu.Unlock()
|
p.mu.Unlock()
|
||||||
@ -618,7 +607,7 @@ func (p *Pinger) sendICMP(conn, conn6 *icmp.PacketConn) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Pinger) recvICMP(conn *icmp.PacketConn, recv chan<- *packet, ctx *context, wg *sync.WaitGroup) {
|
func (p *Pinger) recvICMP(conn *icmp.PacketConn, ctx *context, wg *sync.WaitGroup) {
|
||||||
p.debugln("recvICMP(): Start")
|
p.debugln("recvICMP(): Start")
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
@ -653,16 +642,7 @@ func (p *Pinger) recvICMP(conn *icmp.PacketConn, recv chan<- *packet, ctx *conte
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
p.debugln("recvICMP(): p.recv <- packet")
|
p.procRecv(&packet{bytes: bytes, addr: ra})
|
||||||
|
|
||||||
select {
|
|
||||||
case recv <- &packet{bytes: bytes, addr: ra}:
|
|
||||||
case <-ctx.stop:
|
|
||||||
p.debugln("recvICMP(): <-ctx.stop")
|
|
||||||
wg.Done()
|
|
||||||
p.debugln("recvICMP(): wg.Done()")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user