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

fix: avoid need to lock state for restart.Pending() #451

Merged
merged 3 commits into from
Jul 17, 2024

Conversation

benhoyt
Copy link
Contributor

@benhoyt benhoyt commented Jul 17, 2024

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

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
Copy link
Member

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) {
Copy link
Member

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.

Copy link
Contributor Author

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.

@benhoyt benhoyt merged commit 7d0cb7b into canonical:master Jul 17, 2024
16 checks passed
@benhoyt benhoyt deleted the avoid-restart-locking branch July 17, 2024 04: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.

2 participants