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