Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Protect stats with RWMutex #151

Merged
merged 2 commits into from
Mar 12, 2021
Merged

Protect stats with RWMutex #151

merged 2 commits into from
Mar 12, 2021

Conversation

jraby
Copy link
Contributor

@jraby jraby commented Mar 10, 2021

This is more of a request for comments than a proper PR (and it is branched off #150 so only the last commit should be reviewed )

A RWMutex is added to protect access to the Pinger's internal statistics.
Updates to rtts is moved to updateStats so they happen under the lock.

There's (at least) one issue however: PacketsSent and PacketsRecvDuplicates can still be updated while
Statistics() is running, and thus could yield strange results (sent<recv)

To fix this, atomics could be used, but it would require changing the types of the fields from int to int64, thus changing the public api of Pinger. (One could argue that those fields should probably not be exported to begin with, but that would break the api too)

Any thoughts?

@SuperQ
Copy link
Contributor

SuperQ commented Mar 10, 2021

Yessssssssssssssssssss.

@jraby
Copy link
Contributor Author

jraby commented Mar 11, 2021

Revisiting this, I think it could be possible to add a unexported uint64 (or int64) for packetsSent and duplicatesrecv.
Updatng them using atomic.AddUint64 and accessing them in the stats with atomic.LoadUint64.

The exported fields can stay there and be updated as they are right now, but they wouldn't be used in Statistics().
With that, I think Statistics would be safe, or at least safer.

Copy link
Member

@CHTJonas CHTJonas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice one! It's a 👍 from me for a squash-and-merge.

Edit: looks like there's an issue with CircleCI so I can't see why the build is broken but I suspect it's #148.

updateStats() now also updates rtts so it is protected by the
lock.
Statistics() should now be callable from other goroutines.

PacketsSent and PacketsRecvDuplicates can still be updated while
Statistics() is running, and thus could yield strange results
(sent<recv)

To fix this, atomics could be used, but it would require changing the
types of the fields from int to int64, thus changing the public api.

Signed-off-by: Jean Raby <jean@raby.sh>
@jraby
Copy link
Contributor Author

jraby commented Mar 11, 2021

Rebased

ping.go Outdated Show resolved Hide resolved
Copy link
Contributor

@SuperQ SuperQ left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@SuperQ SuperQ merged commit d90f377 into go-ping:master Mar 12, 2021
@jraby jraby deleted the statsmutex branch March 12, 2021 12:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants