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: sync: Once.DoP (Do with panics treated differently) #49221

Closed
pjebs opened this issue Oct 29, 2021 · 4 comments
Closed

proposal: sync: Once.DoP (Do with panics treated differently) #49221

pjebs opened this issue Oct 29, 2021 · 4 comments

Comments

@pjebs
Copy link
Contributor

pjebs commented Oct 29, 2021

Currently

func (o *Once) Do(f func())

If f panics, Do considers it to have returned; future calls of Do return without calling f.

This proposal suggests a new DoP function which does not consider f to have returned if it panics.

@gopherbot gopherbot added this to the Proposal milestone Oct 29, 2021
@DeedleFake
Copy link

I never actually noticed that behavior of sync.Once, but it doesn't particularly surprise me. I have to wonder what the utility of a function that behaves as proposed would be, though. Relying on panics for regular functionality is rarely a good idea.

@seankhliao
Copy link
Member

How frequent is the need for this? See also https://golang.org/doc/faq#x_in_std

@pjebs
Copy link
Contributor Author

pjebs commented Oct 29, 2021

I've always used sync.Once for lazy loading data that could come from anywhere, including via the network.
If the network connection fails, then sync.Once breaks down for my use case.
Ideally, I would like a func (o *Once) Do(f func() error).

Until recently, I had just been panicking in situations where the network connection fails in order to force fit into the current API design - believing that it worked as I had intended. ... only to recently discover that it did not (as per documentation).

... hence the proposal.

But really I don't like panics either. I really would like func (o *Once) Do(f func() error).

@seankhliao
Copy link
Member

I don't think you've fully explained what your intended flow is.
In the current API, once sync.Once.Do returns, all following code can safely assume initialization has run.
With the proposed API, without retries, that invariant is broken, and there's no universal schedule for retries that will work in all cases.

It seems your usecase would be better served by having f handle retries

@pjebs pjebs closed this as completed Oct 29, 2021
@golang golang locked and limited conversation to collaborators Oct 29, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants