From d0646f180089d7c501cd675dc08d9ac859772b64 Mon Sep 17 00:00:00 2001 From: Harald Albrecht Date: Mon, 22 Apr 2024 16:31:25 +0200 Subject: [PATCH] chore: upgrade go-ping dep to prometheus-community/pro-bing, fix for changes in DNS resolution timeout handling Signed-off-by: Harald Albrecht --- go.mod | 1 + go.sum | 2 ++ ping/ping.go | 30 ++++++++---------------------- ping/ping_test.go | 9 +++++++-- 4 files changed, 18 insertions(+), 24 deletions(-) diff --git a/go.mod b/go.mod index cffadcd..a52f0bf 100644 --- a/go.mod +++ b/go.mod @@ -23,6 +23,7 @@ require ( github.com/felixge/httpsnoop v1.0.4 // indirect github.com/moby/docker-image-spec v1.3.1 // indirect github.com/moby/sys/user v0.1.0 // indirect + github.com/prometheus-community/pro-bing v0.4.0 // indirect github.com/prometheus/procfs v0.9.0 // indirect go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.47.0 // indirect go.opentelemetry.io/otel/metric v1.22.0 // indirect diff --git a/go.sum b/go.sum index 23b9a28..17f83c0 100644 --- a/go.sum +++ b/go.sum @@ -172,6 +172,8 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/prometheus-community/pro-bing v0.4.0 h1:YMbv+i08gQz97OZZBwLyvmmQEEzyfyrrjEaAchdy3R4= +github.com/prometheus-community/pro-bing v0.4.0/go.mod h1:b7wRYZtCcPmt4Sz319BykUU241rWLe1VFXyiyWK/dH4= github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= github.com/prometheus/procfs v0.9.0 h1:wzCHvIvM5SxWqYvwgVL7yJY8Lz3PKn49KQtpgMYJfhI= github.com/prometheus/procfs v0.9.0/go.mod h1:+pB4zwohETzFnmlpe6yd2lSc+0/46IYZRB/chUwxUZY= diff --git a/ping/ping.go b/ping/ping.go index 4a438a5..09bf04e 100644 --- a/ping/ping.go +++ b/ping/ping.go @@ -14,7 +14,7 @@ import ( "github.com/siemens/mobydig/types" "github.com/gammazero/workerpool" - "github.com/go-ping/ping" + ping "github.com/prometheus-community/pro-bing" "github.com/thediveo/lxkns/ops" "github.com/thediveo/lxkns/ops/relations" "github.com/thediveo/lxkns/species" @@ -251,34 +251,20 @@ func (p *Pinger) validate(ctx context.Context, verdict types.QualifiedAddress) { default: } - pinger, err := ping.NewPinger(verdict.Addr()) - if err != nil { - return err - } + pinger := ping.New(verdict.Addr()) // not! ping.NewPinger, would do an immediate resolve pinger.SetPrivileged(!p.unprivileged) pinger.Count = p.count pinger.Interval = p.interval // Always limit waiting for the last ping to get reflected (or not)! - pinger.Timeout = time.Duration(int64(p.interval) * int64(p.count+2)) - // While the ping will be running, we need to monitor the context in - // case it becomes "done" by either getting cancelled or reaching - // its deadline. The done channel here works "the other way round" - // in the sense that it terminated the concurrent context - // monitoring. - done := make(chan struct{}) - defer close(done) - go func() { - select { - case <-ctx.Done(): - pinger.Stop() - case <-done: - } - }() + timeout := time.Duration(int64(p.interval) * int64(p.count+2)) + pinger.ResolveTimeout = timeout + pingctx, cancel := context.WithTimeout(ctx, timeout) + defer cancel() // Now start making some noise... - if err = pinger.Run(); err != nil { + if err := pinger.RunWithContext(pingctx); err != nil { return err } - // Was the context done? + // Was the overall validation context done? if err := ctx.Err(); err != nil { return err } diff --git a/ping/ping_test.go b/ping/ping_test.go index dab2c9b..194f366 100644 --- a/ping/ping_test.go +++ b/ping/ping_test.go @@ -60,7 +60,12 @@ var _ = Describe("pinger", Ordered, func() { BeforeEach(func() { goodgos := Goroutines() DeferCleanup(func() { - Eventually(Goroutines).WithTimeout(3 * time.Second).WithPolling(250 * time.Millisecond). + // Newer implementations of Go's std IP address resolution got + // progressively more complex, doing more stuff in the background + // even after we've cancelled the resolution process. In order to + // avoid false positives, we need to wait longer for go background + // functions to wind down. + Eventually(Goroutines).WithTimeout(10 * time.Second).WithPolling(250 * time.Millisecond). ShouldNot(HaveLeaked(goodgos)) Expect(Tasks()).To(BeUniformlyNamespaced()) }) @@ -214,7 +219,7 @@ var _ = Describe("pinger", Ordered, func() { HaveValue(Equal(types.QualifiedAddressValue{ Address: addr, Quality: verdict, - }))), "waiting for the train that never comes: address should be %s", verdict) + }))), "waited for the train that never came: address should be %s", verdict) pinger.StopWait() Eventually(courtTV).Should(BeClosed()) },