-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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
proposal: x/sync/singleflight: add generic version #53427
Comments
Forked from golang.org/x/sync/singleflight at the x/sync repo's commit 67f06af15bc961c363a7260195bcd53487529a21 Updates golang/go#53427 Change-Id: Iec2b47b7777940017bb9b3db9bd7d93ba4a2e394 Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
Forked from golang.org/x/sync/singleflight at the x/sync repo's commit 67f06af15bc961c363a7260195bcd53487529a21 Updates golang/go#53427 Change-Id: Iec2b47b7777940017bb9b3db9bd7d93ba4a2e394 Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
Forked from golang.org/x/sync/singleflight at the x/sync repo's commit 67f06af15bc961c363a7260195bcd53487529a21 Updates golang/go#53427 Change-Id: Iec2b47b7777940017bb9b3db9bd7d93ba4a2e394 Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
Forked from golang.org/x/sync/singleflight at the x/sync repo's commit 67f06af15bc961c363a7260195bcd53487529a21 Updates golang/go#53427 Change-Id: Iec2b47b7777940017bb9b3db9bd7d93ba4a2e394 Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
I don't think we have a convention yet. |
What about x/sync/v2/singleflight ? |
Forked from golang.org/x/sync/singleflight at the x/sync repo's commit 67f06af15bc961c363a7260195bcd53487529a21 Updates golang/go#53427 Change-Id: Iec2b47b7777940017bb9b3db9bd7d93ba4a2e394 Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
I tried it before. Thank you. refs: #52520 |
Change https://go.dev/cl/425187 mentions this issue: |
Is there any update on this proposal? |
@lmittmann No. |
This proposal has been added to the active column of the proposals project |
Maybe it should be |
This really circles back to #48287, which we didn't resolve. |
I know this might sound crazy, but how about simply introducing a breaking change in an experimental package, which is v0, and has "looser compatibility requirements"? |
I agree that in this case and more generally in x/* experimental packages the right thing to do is a breaking change, eitherwise the whole purpose of these packages is mute and might as well put them directly under the standard library. |
Holding for #48287. |
Placed on hold. |
I took a crack at the v2 formulation for this change. I've posted it as a draft because it differs enough from the original proposal that I didn't want to send it out for full review unless I heard from @bradfitz or @rsc that this was an okay way to use the original. Happy to instead create a new proposal ticket for the v2 version of this if those folks would prefer! |
I made a simple lib for experiments: https://github.com/samber/go-singleflightx
|
@rsc this is marked as "on hold" pending the discussion on #48287, which was closed shortly thereafter, AFAICT without a follow-up. As @lmittmann mentions, the release of math/rand/v2 seems to indicate this blocker has been removed and the proposal could be put back on the table? |
Thanks. Moving back to incoming. |
This would be my preference, but either that or a v2 is preferable to not shipping anything |
@derekperkins A lot relies on the x/sync/singleflight package (including many libraries). It really doesn't make sense to make a breaking change here. A v2 is a much better option. (as was discussed above) Hopefully this makes it to the head of the proposal review committee's queue in the near-ish future. |
I'm just commenting based on the v0.8 release tag, which allows breaking changes. v2 being a "much better option" didn't receive consensus above, so I was offering my opinion. Either way is fine. |
As the singleflight API is considering adding generics, this is a good opportunity to simplify and improve its design. I would like to propose three changes that could enhance the API while maintaining its core functionality: Proposed Changes
MotivationThese changes aim to make singleflight simpler, more performant, and easier to maintain. Generics support already introduces API changes, making this an ideal time to address these improvements. ReferenceI have implemented a simplified version of singleflight with asynchronous deletion. In benchmarking tests, this approach showed approximately 2x faster performance compared to the current implementation. You can view the code here: |
awesome work, + how about adding support for “context” for cancellation specifically |
Why would it be a good idea to get rid of the shared flag? It adds complexity but it's there for a reason. |
Thank you for your feedback!
Additionally, I’d like to suggest another improvement:
Let me know your thoughts! @pkieltyka @ianlancetaylor |
There is an example of using the |
@catatsuy maybe I'm missing something, here https://github.com/catatsuy/cache/blob/main/singleflight.go#L51 there is no context, how would you use context with your library? |
Thank you for pointing that out! You're right, my current implementation at the link you provided doesn’t explicitly support ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
sf := cache.NewSingleflightGroup[string]()
result, err := sf.Do("key", func() (string, error) {
// Use context here
select {
case <-ctx.Done():
return "", ctx.Err()
default:
// Do some work
return "value", nil
}
}) In this way, If you have a specific scenario where |
it's just a less flexible option and depends on function closures. personally I'd like to see a context.Context argument prepended to Do (just my opinion). |
The singleflight package's
Group
type usesstring
keys andinterface{}
values:https://pkg.go.dev/golang.org/x/sync/singleflight#Group
I've locally forked it to add generics
[K comparable, V any]
but I'd like to get it upstream instead.Proposal: add
singleflight.TypedGroup[K, V]
orsingleflight.GenericGroup[K, V]
orsingleflight.GroupOf[K, V]
behind ago1.18
build tag.Do we have a convention yet on naming new parallel generic types alongside others?
Related: #47657 (for PoolOf, MapOf). (There was also talk of default type parameters so old code could instantiate types and get the old behavior (in this case
[string, any]
) and new callers could specify types?)The text was updated successfully, but these errors were encountered: