Skip to content

Commit 8952c84

Browse files
author
wanzh
committed
fix(inputs.ping): honor timeout parameter for native method
1 parent 67f41a1 commit 8952c84

File tree

2 files changed

+84
-4
lines changed

2 files changed

+84
-4
lines changed

plugins/inputs/ping/ping.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,7 @@ func (p *Ping) nativePing(destination string) (*pingStats, error) {
160160

161161
pinger.Source = p.sourceAddress
162162
pinger.Interval = p.calcInterval
163-
164-
if p.Deadline > 0 {
165-
pinger.Timeout = time.Duration(p.Deadline) * time.Second
166-
}
163+
pinger.Timeout = p.nativeRunTimeout()
167164

168165
// Get Time to live (TTL) of first response, matching original implementation
169166
once := &sync.Once{}
@@ -191,6 +188,24 @@ func (p *Ping) nativePing(destination string) (*pingStats, error) {
191188
return ps, nil
192189
}
193190

191+
func (p *Ping) nativeRunTimeout() time.Duration {
192+
timeout := p.calcTimeout
193+
if p.Count > 1 {
194+
// With native ping, timeout is global. Approximate per-ping timeout
195+
// semantics by adding the delay before the final packet is sent.
196+
timeout += p.calcInterval * time.Duration(p.Count-1)
197+
}
198+
199+
if p.Deadline > 0 {
200+
deadline := time.Duration(p.Deadline) * time.Second
201+
if deadline < timeout {
202+
return deadline
203+
}
204+
}
205+
206+
return timeout
207+
}
208+
194209
func (p *Ping) pingToURLNative(destination string, acc telegraf.Accumulator) {
195210
tags := map[string]string{"url": destination}
196211

plugins/inputs/ping/ping_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,71 @@ func TestPingGatherNative(t *testing.T) {
480480
}
481481
}
482482

483+
func TestNativeRunTimeout(t *testing.T) {
484+
tests := []struct {
485+
name string
486+
count int
487+
pingInterval float64
488+
timeout float64
489+
deadline int
490+
expected time.Duration
491+
}{
492+
{
493+
name: "single packet prefers timeout over larger deadline",
494+
count: 1,
495+
pingInterval: 1.0,
496+
timeout: 1.0,
497+
deadline: 10,
498+
expected: time.Second,
499+
},
500+
{
501+
name: "multiple packets include send interval before final packet",
502+
count: 5,
503+
pingInterval: 1.0,
504+
timeout: 1.0,
505+
deadline: 10,
506+
expected: 5 * time.Second,
507+
},
508+
{
509+
name: "no deadline uses timeout based duration",
510+
count: 5,
511+
pingInterval: 1.0,
512+
timeout: 2.0,
513+
deadline: 0,
514+
expected: 6 * time.Second,
515+
},
516+
{
517+
name: "deadline caps computed timeout",
518+
count: 5,
519+
pingInterval: 1.0,
520+
timeout: 2.0,
521+
deadline: 4,
522+
expected: 4 * time.Second,
523+
},
524+
{
525+
name: "minimum native interval is applied",
526+
count: 3,
527+
pingInterval: 0.1,
528+
timeout: 1.0,
529+
deadline: 0,
530+
expected: 1400 * time.Millisecond,
531+
},
532+
}
533+
534+
for _, tc := range tests {
535+
t.Run(tc.name, func(t *testing.T) {
536+
p := &Ping{
537+
Count: tc.count,
538+
PingInterval: tc.pingInterval,
539+
Timeout: tc.timeout,
540+
Deadline: tc.deadline,
541+
}
542+
require.NoError(t, p.Init())
543+
require.Equal(t, tc.expected, p.nativeRunTimeout())
544+
})
545+
}
546+
}
547+
483548
func TestNoPacketsSent(t *testing.T) {
484549
p := &Ping{
485550
Log: testutil.Logger{},

0 commit comments

Comments
 (0)