From ccc75a38ee992eebc982a100b571cae3e8f809f0 Mon Sep 17 00:00:00 2001 From: thatmattlove Date: Thu, 17 Feb 2022 16:30:58 -0700 Subject: [PATCH] refactor `CheckInFlightPackets` to use packet UUID --- ping.go | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/ping.go b/ping.go index cb68ab1..36507c0 100644 --- a/ping.go +++ b/ping.go @@ -92,13 +92,12 @@ func New(addr string) *Pinger { var firstSequence = map[uuid.UUID]map[int]InFlightPacket{} firstSequence[firstUUID] = make(map[int]InFlightPacket) return &Pinger{ - Count: -1, - Interval: time.Second, - RecordRtts: true, - Size: timeSliceLength + trackerLength, - Timeout: time.Duration(math.MaxInt64), - PacketTimeout: time.Duration(math.MaxInt64), - Tracker: r.Uint64(), + Count: -1, + Interval: time.Second, + RecordRtts: true, + Size: timeSliceLength + trackerLength, + Timeout: time.Duration(math.MaxInt64), + Tracker: r.Uint64(), addr: addr, done: make(chan interface{}), @@ -129,10 +128,6 @@ type Pinger struct { // packets have been received. Timeout time.Duration - // PacketTimeout specifies a timeout before the OnTimeout function is called - // for a packet not being received - PacketTimeout time.Duration - // Count tells pinger to stop after sending (and receiving) Count echo // packets. If this option is not specified, pinger will operate until // interrupted. @@ -522,15 +517,17 @@ func (p *Pinger) runLoop( func (p *Pinger) CheckInFlightPackets() { // Loop through each item in map - if p.PacketTimeout == maxPacketTimeout { + if time.Duration(p.TTL) == maxPacketTimeout { return } currentTime := time.Now() - for seq, pkt := range p.InFlightPackets { - if pkt.DispatchedTime.Add(p.PacketTimeout).Before(currentTime) { - delete(p.InFlightPackets, seq) - if p.OnTimeout != nil { - p.OnTimeout(seq, &pkt) + for id, inflight := range p.InFlightPackets { + for seq, pkt := range inflight { + if pkt.DispatchedTime.Add(time.Duration(p.TTL)).Before(currentTime) { + delete(p.InFlightPackets[id], seq) + if p.OnTimeout != nil { + p.OnTimeout(seq, &pkt) + } } } }