Remove unnecessary handler allocations (#47)

Cleanup unnecessary creation of `handler` vars for various event
handlers.

Signed-off-by: SuperQ <superq@gmail.com>
This commit is contained in:
Ben Kochie 2023-06-06 13:47:12 +02:00 committed by Kaj Niemi
parent 7b2aa2bf3d
commit 28dee17585

20
ping.go
View File

@ -482,8 +482,8 @@ func (p *Pinger) run(ctx context.Context, conn packetConn) error {
recv := make(chan *packet, 5) recv := make(chan *packet, 5)
defer close(recv) defer close(recv)
if handler := p.OnSetup; handler != nil { if p.OnSetup != nil {
handler() p.OnSetup()
} }
g, ctx := errgroup.WithContext(ctx) g, ctx := errgroup.WithContext(ctx)
@ -578,10 +578,8 @@ func (p *Pinger) Stop() {
} }
func (p *Pinger) finish() { func (p *Pinger) finish() {
handler := p.OnFinish if p.OnFinish != nil {
if handler != nil { p.OnFinish(p.Statistics())
s := p.Statistics()
handler(s)
} }
} }
@ -764,9 +762,8 @@ func (p *Pinger) processPacket(recv *packet) error {
return fmt.Errorf("invalid ICMP echo reply; type: '%T', '%v'", pkt, pkt) return fmt.Errorf("invalid ICMP echo reply; type: '%T', '%v'", pkt, pkt)
} }
handler := p.OnRecv if p.OnRecv != nil {
if handler != nil { p.OnRecv(inPkt)
handler(inPkt)
} }
return nil return nil
@ -814,8 +811,7 @@ func (p *Pinger) sendICMP(conn packetConn) error {
} }
return err return err
} }
handler := p.OnSend if p.OnSend != nil {
if handler != nil {
outPkt := &Packet{ outPkt := &Packet{
Nbytes: len(msgBytes), Nbytes: len(msgBytes),
IPAddr: p.ipaddr, IPAddr: p.ipaddr,
@ -823,7 +819,7 @@ func (p *Pinger) sendICMP(conn packetConn) error {
Seq: p.sequence, Seq: p.sequence,
ID: p.id, ID: p.id,
} }
handler(outPkt) p.OnSend(outPkt)
} }
// mark this sequence as in-flight // mark this sequence as in-flight
p.awaitingSequences[currentUUID][p.sequence] = struct{}{} p.awaitingSequences[currentUUID][p.sequence] = struct{}{}