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

Refactor metrics to simplify updating the values #187

Merged
merged 11 commits into from
Feb 12, 2024
Merged

Conversation

DFINITYManu
Copy link
Contributor

Use a private data structure to track per-network metrics, then use that data structure as data source for the metrics callbacks. The once-added callbacks can then serve the metrics for registered definitions.

In addition to that, when a definition ends, we deregister it from the per- network metrics data structure, so we stop serving metrics about it.

We are no longer using Tokio mutexes since this would require async closures for the callbacks, and async closures are unstable. std::sync::Mutex works perfectly OK for this use and, in fact, Tokio documentation states that standard mutexes can work fine with Tokio code, and should be preferred in most cases.

https://docs.rs/tokio/1.24.2/tokio/sync/struct.Mutex.html#which-kind-of-mutex-should-you-use indicates we should use the standard mutex when we are not going to hold the lock across await points. Since that is exactly what we are doing (there are no awaits involved in the set() and get() code for the data structure being locked), then we can simply use the simple blocking mutex, which is what we have done here.

Use a private data structure to track per-network metrics, then use that data
structure as data source for the metrics callbacks.  The once-added callbacks
can then serve the metrics for registered definitions.

In addition to that, when a definition ends, we deregister it from the per-
network metrics data structure, so we stop serving metrics about it.

We are no longer using Tokio mutexes since this would require async closures
for the callbacks, and async closures are unstable.  std::sync::Mutex works
perfectly OK for this use and, in fact, Tokio documentation states that
standard mutexes can work fine with Tokio code, and should be preferred
in most cases.

https://docs.rs/tokio/1.24.2/tokio/sync/struct.Mutex.html#which-kind-of-mutex-should-you-use
indicates we should use the standard mutex when we are not going to hold the
lock across await points.  Since that is exactly what we are doing (there are
no awaits involved in the set() and get() code for the data structure being
locked), then we can simply use the simple blocking mutex, which is what we
have done here.
@DFINITYManu DFINITYManu requested a review from a team as a code owner February 12, 2024 16:24
Copy link
Member

@sasa-tomic sasa-tomic left a comment

Choose a reason for hiding this comment

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

Looks much better, nice cleanup. Thanks Manuel!

@DFINITYManu DFINITYManu added this pull request to the merge queue Feb 12, 2024
Merged via the queue into main with commit 099c4de Feb 12, 2024
4 checks passed
@DFINITYManu DFINITYManu deleted the refactormetrics branch February 12, 2024 18:38
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.

2 participants