go-ping / ping

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

Reuse Pinger feature #199

Closed SHA65536 closed 2 years ago

SHA65536 commented 2 years ago

Hey I recently needed to ping a list of addresses and I think that instead of needing to create a new pinger each time, it would be easier to just use the same pinger. As I understood it's not currently possible since the internal state of the pinger does not change when using SetAddr.

Problem and replication described in #198 I saw someone in the past tried to solve this issue, not sure why the pull request didn't get merged: #117

659841dbad04df285d05946f9439118f1fcf8729 + 7613ef775df355ab0163a96a66aff5184ed8c270 I added a Reset function to be able to manually reset the internal state of the pinger, specifically packets received and sent, and minimum and maximum Rtt.

2741d0bb576648eee3ec9568d9ce49345f95834b Added the Reset function to SetAddr so the statistics won't get mixed after changing the address. I don't think there's a use case for SetAddr unless you also want to re-use the pinger.

I'm not well versed in github so I apologize if something about this PR is wrong! Cheers! 😄

SuperQ commented 2 years ago

Sorry, but this is not what you want to do. You want to re-create the pinger each time with ping.New(). This is fine and normal to do in Go. Just move the ping.New(...) inside your for loop.