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

proposal: x/sync/singleflight: add generic API #52520

Open
catatsuy opened this issue Apr 24, 2022 · 4 comments
Open

proposal: x/sync/singleflight: add generic API #52520

catatsuy opened this issue Apr 24, 2022 · 4 comments
Labels
generics Issue is related to generics Proposal
Milestone

Comments

@catatsuy
Copy link

catatsuy commented Apr 24, 2022

  • singleflight is a great library, but the return value of the argument function uses interface{}. If you use generics, you can make it a flexible function by simply passing a type.
  • For compatibility, define it with a different name than singleflight.Group, e.g. singleflight.GroupGenerics.
  • singleflight.GroupGenerics has exported functions Do(), DoChan() and Forget() as well as singleflight.
    • singleflight.GroupGenerics has the following types, arguments, and return values.
type GroupGenerics[V any] struct {
	mu sync.Mutex          // protects m
	m  map[string]*call[V] // lazily initialized
}

func (g *GroupGenerics[V]) Do(key string, fn func() (V, error)) (v V, err error, shared bool)
func (g *GroupGenerics[V]) DoChan(key string, fn func() (V, error)) <-chan Result
func (g *GroupGenerics[V]) Forget(key string)

I changed the existing API to try it out. Please refer to it.

https://github.com/catatsuy/sync/pull/1/files

@ianlancetaylor
Copy link
Contributor

Please include the new API with docs (but without the implementation) in this issue. That makes it a easier for people to see the API being proposed. Thanks.

@ianlancetaylor ianlancetaylor added the generics Issue is related to generics label Apr 25, 2022
@ianlancetaylor ianlancetaylor added this to Incoming in Proposals (old) Apr 25, 2022
@ianlancetaylor ianlancetaylor changed the title proposal: x/sync: Add an API that can use generics to singleflight proposal: x/sync/singleflight: add generic API Apr 25, 2022
@catatsuy
Copy link
Author

@ianlancetaylor Sorry for the late reply. I have changed my description. Could you confirm it?

@ianlancetaylor
Copy link
Contributor

Thanks. I hope that we can find a better name than GroupGenerics.

@catatsuy
Copy link
Author

I think so, too. GroupGenerics does not represent the role. We are looking for the good ideas.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
generics Issue is related to generics Proposal
Projects
Status: Incoming
Development

No branches or pull requests

3 participants