go-ping / ping

ICMP Ping library for Go
MIT License
1.32k stars 344 forks source link

Feature request: context aware `RunWithContext` method #182

Closed qdm12 closed 1 year ago

qdm12 commented 3 years ago

It would be easier to use rather than using the .Stop method with some select block to support contexts. Thanks!

For reference, that's what I use for now:

ctx := context.Background()

pinger := ping.New("1.1.1.1")

errCh := make(chan error)
go func() {
    errCh <- pinger.Run()
}()

select {
case <-ctx.Done():
    pinger.Stop()
    <-errCh
    return ctx.Err()
case err = <-errCh:
    return err
}
CHTJonas commented 3 years ago

Would you be interested in working on a PR for this?

qdm12 commented 3 years ago

@CHTJonas Done in #185 although I'm not sure how to go around testing it without running as root on the CI (to use icmp packets to localhost). Would you have any idea?

I also did #184 since I felt like reading the code and fix lint errors 😉 Take it or leave it!

tarndt commented 2 years ago

You can get this functionality today with no upstream changes if needed: https://github.com/go-ping/ping/pull/185#issuecomment-1146671786