1
0
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:
Tatsushi Demachi 2015-10-25 14:26:39 +09:00
parent 1f74bfd6d1
commit d7610ec331

View File

@ -421,7 +421,6 @@ func (p *Pinger) run(once bool) {
defer conn6.Close()
}
recv := make(chan *packet, len(p.addrs))
recvCtx := newContext()
wg := new(sync.WaitGroup)
@ -430,7 +429,7 @@ func (p *Pinger) run(once bool) {
routines := runtime.NumCPU()
wg.Add(routines)
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()
wg.Add(routines)
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()")
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()")
wg.Wait()
// process all queued results
close(recv)
for r := range recv {
p.debugln("Run(): <-recv")
p.procRecv(r)
}
p.mu.Lock()
p.ctx.err = err
p.mu.Unlock()
@ -618,7 +607,7 @@ func (p *Pinger) sendICMP(conn, conn6 *icmp.PacketConn) error {
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")
for {
select {
@ -653,16 +642,7 @@ func (p *Pinger) recvICMP(conn *icmp.PacketConn, recv chan<- *packet, ctx *conte
}
}
}
p.debugln("recvICMP(): p.recv <- packet")
select {
case recv <- &packet{bytes: bytes, addr: ra}:
case <-ctx.stop:
p.debugln("recvICMP(): <-ctx.stop")
wg.Done()
p.debugln("recvICMP(): wg.Done()")
return
}
p.procRecv(&packet{bytes: bytes, addr: ra})
}
}