mirror of
https://github.com/go-ping/ping.git
synced 2025-06-29 07:26:59 +00:00
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.
This commit is contained in:
parent
5f9dc3248b
commit
265e7c64b3
7
ping.go
7
ping.go
@ -79,6 +79,7 @@ func New(addr string) *Pinger {
|
|||||||
return &Pinger{
|
return &Pinger{
|
||||||
Count: -1,
|
Count: -1,
|
||||||
Interval: time.Second,
|
Interval: time.Second,
|
||||||
|
RecordRtts: true,
|
||||||
Size: timeSliceLength,
|
Size: timeSliceLength,
|
||||||
Timeout: time.Second * 100000,
|
Timeout: time.Second * 100000,
|
||||||
Tracker: r.Int63n(math.MaxInt64),
|
Tracker: r.Int63n(math.MaxInt64),
|
||||||
@ -122,6 +123,10 @@ type Pinger struct {
|
|||||||
// Number of packets received
|
// Number of packets received
|
||||||
PacketsRecv int
|
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 is all of the Rtts
|
||||||
rtts []time.Duration
|
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)
|
return fmt.Errorf("invalid ICMP echo reply; type: '%T', '%v'", pkt, pkt)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if p.RecordRtts {
|
||||||
p.rtts = append(p.rtts, outPkt.Rtt)
|
p.rtts = append(p.rtts, outPkt.Rtt)
|
||||||
|
}
|
||||||
handler := p.OnRecv
|
handler := p.OnRecv
|
||||||
if handler != nil {
|
if handler != nil {
|
||||||
handler(outPkt)
|
handler(outPkt)
|
||||||
|
Loading…
Reference in New Issue
Block a user