Skip to content

Commit

Permalink
chore: upgrade go-ping dep to prometheus-community/pro-bing, fix for …
Browse files Browse the repository at this point in the history
…changes in DNS resolution timeout handling

Signed-off-by: Harald Albrecht <[email protected]>
  • Loading branch information
halb0 committed Apr 22, 2024
1 parent 6458330 commit d0646f1
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 24 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
30 changes: 8 additions & 22 deletions ping/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down
9 changes: 7 additions & 2 deletions ping/ping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
})
Expand Down Expand Up @@ -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())
},
Expand Down

0 comments on commit d0646f1

Please sign in to comment.