re-add PacketTimeout field from #176

This commit is contained in:
thatmattlove 2022-02-18 07:00:23 -07:00
parent ccc75a38ee
commit 7d19def034
2 changed files with 14 additions and 8 deletions

View File

@ -92,6 +92,7 @@ func main() {
pinger.Size = *size pinger.Size = *size
pinger.Interval = *interval pinger.Interval = *interval
pinger.Timeout = *timeout pinger.Timeout = *timeout
pinger.PacketTimeout = time.Second * 10
pinger.TTL = *ttl pinger.TTL = *ttl
pinger.SetPrivileged(*privileged) pinger.SetPrivileged(*privileged)

View File

@ -97,6 +97,7 @@ func New(addr string) *Pinger {
RecordRtts: true, RecordRtts: true,
Size: timeSliceLength + trackerLength, Size: timeSliceLength + trackerLength,
Timeout: time.Duration(math.MaxInt64), Timeout: time.Duration(math.MaxInt64),
PacketTimeout: time.Duration(math.MaxInt64),
Tracker: r.Uint64(), Tracker: r.Uint64(),
addr: addr, addr: addr,
@ -128,6 +129,10 @@ type Pinger struct {
// packets have been received. // packets have been received.
Timeout time.Duration 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 // Count tells pinger to stop after sending (and receiving) Count echo
// packets. If this option is not specified, pinger will operate until // packets. If this option is not specified, pinger will operate until
// interrupted. // interrupted.
@ -517,13 +522,13 @@ func (p *Pinger) runLoop(
func (p *Pinger) CheckInFlightPackets() { func (p *Pinger) CheckInFlightPackets() {
// Loop through each item in map // Loop through each item in map
if time.Duration(p.TTL) == maxPacketTimeout { if p.PacketTimeout == maxPacketTimeout {
return return
} }
currentTime := time.Now() currentTime := time.Now()
for id, inflight := range p.InFlightPackets { for id, inflight := range p.InFlightPackets {
for seq, pkt := range inflight { for seq, pkt := range inflight {
if pkt.DispatchedTime.Add(time.Duration(p.TTL)).Before(currentTime) { if pkt.DispatchedTime.Add(p.PacketTimeout).Before(currentTime) {
delete(p.InFlightPackets[id], seq) delete(p.InFlightPackets[id], seq)
if p.OnTimeout != nil { if p.OnTimeout != nil {
p.OnTimeout(seq, &pkt) p.OnTimeout(seq, &pkt)