From 265e7c64b33b696d39d2521caf46c792ef3d1199 Mon Sep 17 00:00:00 2001 From: rrcollier Date: Tue, 20 Oct 2020 23:19:59 +0100 Subject: [PATCH] Add option not to store all received RTTs (#115) This prevents memory bloat from a process that wants to do very long-running ping tests. --- ping.go | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/ping.go b/ping.go index 19fe1e7..c28e9ae 100644 --- a/ping.go +++ b/ping.go @@ -77,11 +77,12 @@ var ( func New(addr string) *Pinger { r := rand.New(rand.NewSource(time.Now().UnixNano())) return &Pinger{ - Count: -1, - Interval: time.Second, - Size: timeSliceLength, - Timeout: time.Second * 100000, - Tracker: r.Int63n(math.MaxInt64), + Count: -1, + Interval: time.Second, + RecordRtts: true, + Size: timeSliceLength, + Timeout: time.Second * 100000, + Tracker: r.Int63n(math.MaxInt64), addr: addr, done: make(chan bool), @@ -122,6 +123,10 @@ type Pinger struct { // Number of packets received PacketsRecv int + // If true, keep a record of rtts of all received packets. + // Set to false to avoid memory bloat for long running pings. + RecordRtts bool + // rtts is all of the Rtts rtts []time.Duration @@ -538,7 +543,9 @@ func (p *Pinger) processPacket(recv *packet) error { return fmt.Errorf("invalid ICMP echo reply; type: '%T', '%v'", pkt, pkt) } - p.rtts = append(p.rtts, outPkt.Rtt) + if p.RecordRtts { + p.rtts = append(p.rtts, outPkt.Rtt) + } handler := p.OnRecv if handler != nil { handler(outPkt)