-
Notifications
You must be signed in to change notification settings - Fork 55
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
fix: avoid need to lock state for restart.Pending() #451
Conversation
This is based on canonical/snapd#12166 (though the diff was applied more or less manually, as the patch couldn't be applied directly). It includes the name changes to the restart.Handler interface methods.
@@ -60,7 +61,7 @@ type restartManagerKey struct{} | |||
// RestartManager takes care of restart-related state. | |||
type RestartManager struct { | |||
state *state.State | |||
restarting RestartType | |||
restarting atomic.Int32 // really of type RestartType |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In future we could use an atomic.Pointer[RestartType]
instead, although unnecessary heap allocation for such a small type, keeps the type.
rm := cached.(*RestartManager) | ||
return rm.restarting != RestartUnset, rm.restarting | ||
// NOTE: the state does not need to be locked to fetch this information. | ||
func (rm *RestartManager) Pending() (bool, RestartType) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
an "ok" bool is usually the last return value (like error)
e.g.
v, ok := myMap[k]
Since this is existing functionality, I don't mind.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I was actually tempted to get rid of the bool entirely, because it's just b != RestartUnset
, which is easy for the caller to do. However, given it has this signature in snapd, I want to keep it as is.
This change avoids the need to lock/unlock state when fetching
restart.Pending()
, avoiding the need for one state lock/unlock on every HTTP request. (We hope to get rid of the other lock/unlock by removing warnings, but that's a separate issue.)Updates #366