-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Introduce sync.Map wrapper with generics support #29452
Conversation
sync.Map provides a map-like API that is safe for concurrent use by multiple goroutines without additional locking or coordination. Its main drawback, though, is that predates generics support in Go, which means that all methods operate on keys/values of any type. Hence, it is not type safe, and requires annoying type conversions. This commit introduces a thin generic wrapper around it, allowing consumers to specify the exact type of keys and values. Code comments from the standard library are copied (and slightly adapted where necessary) for users' convenience. Signed-off-by: Marco Iorio <[email protected]>
Following the introduction of the generic sync.Map wrapper, let's switch current usages over to it, to benefit from the explicit key/value types. Signed-off-by: Marco Iorio <[email protected]>
Extend the lock-check.sh linting script to additionally flag usages of the sync.Map type, and suggest the usage of the generic wrapper. Signed-off-by: Marco Iorio <[email protected]>
/test |
Cilium Cluster Mesh upgrade failure looks caused by #29078. Rerunning |
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.
Great idea! I am wondering if we need the MapCmpValues
struct in addition to the Map
struct here. What was the reasoning behind the split? From my understanding, if the CompareAnd{Swap,Delete}
methods were attached to the Map
struct instead, I don't think there would be any difference to users, since the corresponding sync.Map
methods can already support taking a value with type any
.
Yeah, I agree that the Overall the reasoning behind the split is that the Another alternative could be simply dropping the WDYT? |
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.
I only took a brief look at the changes in the agent, focused more on the script. Looks good to me, thanks
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.
Looks good!
I would probably drop MapCmpValues
though.
Ah I didn't realize that it would panic 🤦. I'm on the same page now. IMO, keeping the extra struct is probably a good idea. We could remove it for now, since no code uses it, but then we may have to reimplement it in the future anyways. Not a dealbreaker thoughl |
Yeah, I agree. Let's keep it there for the moment, we can always drop it afterwards. |
sync.Map provides a map-like API that is safe for concurrent use by
multiple goroutines without additional locking or coordination. Its
main drawback, though, is that predates generics support in Go, which
means that all methods operate on keys/values of any type. Hence, it
is not type safe, and requires annoying type conversions.
This PR introduces a thin generic wrapper around it, allowing
consumers to specify the exact type of keys and values. Code comments
from the standard library are copied (and slightly adapted where
necessary) for users' convenience.
Additionally, it converts current usages over to it, and extends the
lock-check.sh linting script to additionally flag usages of sync.Map.