-
Notifications
You must be signed in to change notification settings - Fork 591
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 by applying the "Mutex hat" idiom #2621
base: master
Are you sure you want to change the base?
Conversation
Signed-off-by: Oleksandr Redko <[email protected]>
7fe5113
to
f94b240
Compare
I'm wondering if it would make sense to group protected fields together with their mutex into their own struct. That would avoid repeating some of the redundant naming, and makes it even more obvious which fields are protected by which mutex. Something like this (untested): type worthCheckingIPTables struct {
sync.RWMutex
value bool
}
type latestIPTables struct {
sync.RWMutex
entries []iptables.Entry
}
type agent struct {
worthCheckingIPTables
latestIPTables
}
func foo(a *agent) {
a.worthCheckingIPTables.Lock()
a.worthCheckingIPTables.value = true
a.worthCheckingIPTables.Unlock()
} This is only meant as a RFC, I'm not sure if this works better or not. |
I'm not sure if we will gain any benefit by adding a struct that contains a mutex and protected fields. We have only 7 mutexes in the repo, which is a small amount. The Go codebase doesn't introduce a separate struct for mutexes, so we shouldn't either. |
Just briefly browsing those search results it looks like almost half of them show a struct with a mutex at top (most often called just An exact example of the pattern I'm discussing here can be seen at https://github.com/golang/go/blob/81c92352a7c6aadc434e7d0921d046a599ba2807/src/os/signal/signal.go#L13. |
On the other hand, Uber Go Style Guide is against embedding mutexes:
The Goland may warn about embedded mutexes when the team implements https://youtrack.jetbrains.com/issue/GO-14143. |
I honestly don't understand the argument how I do get the argument that you should use one style consistently, but it seems arbitrary which one you pick. Anyways, I'm fine with naming the mutext |
Ok, I do get the argument if all the protected fields are non-exported fields. In that case you should not expose the locking mechanism either. But as soon as any protected fields are exported, you must export the locking mechanism too. And none of this should apply to non-exported types. |
The PR improves readability by applying the "Mutex hat" idiom. See also this slide.
Additionally, this PR renames mutexes names by using the
Mu
suffix.