Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

reset sync.OnceValue

258 views
Skip to first unread message

Hartmut Wieselburger

unread,
Nov 21, 2024, 3:14:46 PM11/21/24
to golang-nuts
Hi,
is there a way to reset sync.OnceValue(s), as you can do it with sync.Once?
A common use case is loading a config. I would like to switch from sync.Once to sync.OnceValue, but I need to a chance to reload my config, without restarting my service.
If this is possible with sync.OnceValue, please let me know. Otherwise the new OnceValue(s) are useless for me.

Regards, Harti

burak serdar

unread,
Nov 21, 2024, 3:24:40 PM11/21/24
to Hartmut Wieselburger, golang-nuts
On Thu, Nov 21, 2024 at 1:14 PM 'Hartmut Wieselburger' via golang-nuts
<golan...@googlegroups.com> wrote:
>
> Hi,
> is there a way to reset sync.OnceValue(s), as you can do it with sync.Once?

How can you reset a sync.Once?

> A common use case is loading a config. I would like to switch from sync.Once to sync.OnceValue, but I need to a chance to reload my config, without restarting my service.
> If this is possible with sync.OnceValue, please let me know. Otherwise the new OnceValue(s) are useless for me.
>
> Regards, Harti
>
> --
> You received this message because you are subscribed to the Google Groups "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
> To view this discussion visit https://groups.google.com/d/msgid/golang-nuts/46b4a4ff-18cd-494a-9ac0-3a40cf2074e2n%40googlegroups.com.

Hartmut Wieselburger

unread,
Nov 22, 2024, 3:52:59 AM11/22/24
to golang-nuts
var configOnce sync.Once
func Reset() {
configOnce = sync.Once{}
}

func Load() *Config {
configOnce.Do(func() {
...

Volker Dobler

unread,
Nov 22, 2024, 5:32:41 AM11/22/24
to golang-nuts
You can do the same with OnceValue, but neither is a good idea
as it totally invalidates what sync.Once is good for: preventing
data races which your "reset" (in quotation marks as you do not
reset the sync.Once) provokes.

V.

Hartmut Wieselburger

unread,
Nov 22, 2024, 5:41:28 AM11/22/24
to golang-nuts
| You can do the same with OnceValue
Can you give an example?

Volker Dobler

unread,
Nov 22, 2024, 6:55:54 AM11/22/24
to golang-nuts
var fOnce func() T
func Reset() {
fOnce = sync.OnceValue(your code here)
}
But as I said: Both is _wrong_. Really totally wrong.
The OnceValue code should make this clear.
Stop doing this. Redesign.

V.

burak serdar

unread,
Nov 22, 2024, 10:10:00 AM11/22/24
to Hartmut Wieselburger, golang-nuts
On Fri, Nov 22, 2024 at 1:53 AM 'Hartmut Wieselburger' via golang-nuts
<golan...@googlegroups.com> wrote:
>
> var configOnce sync.Once
> func Reset() {
> configOnce = sync.Once{}
> }

This is not resetting. You are creating a new sync.Once and assigning
that to a variable. That variable, configOnce, is shared among
multiple goroutines, because that is what Once is used for, to limit
the execution of a function called by multiple goroutines to a single
execution. The Reset function sets this shared variable without any
synchronization, so this is a data race.

Also note that reassigning a Once instance like this may not behave as
you expect. Any goroutine that is waiting on that Once instance will
continue waiting until the Once function is done. The behavior of a
goroutine that reads Once after Reset() is undefined. Since there is a
data race, there is no guarantee on which instance of Once another
goroutine will see. A goroutine may see the completed instance of Once
even after you Reset() it.
> To view this discussion visit https://groups.google.com/d/msgid/golang-nuts/f3df358a-d4ae-441c-9ff6-30af9ad1c568n%40googlegroups.com.

Hartmut Wieselburger

unread,
Nov 25, 2024, 3:04:35 AM11/25/24
to golang-nuts
Ok, thanks, once means once. So https://pkg.go.dev/sync/atomic#Value seems to be a better fit, do you agree?

burak serdar

unread,
Nov 25, 2024, 1:55:51 PM11/25/24
to Hartmut Wieselburger, golang-nuts
You can switch an active configuration using atomic.Value, after
initializing it with a Once. If your config is not initialized,
atomic.Value will not block others.

On Mon, Nov 25, 2024 at 1:05 AM 'Hartmut Wieselburger' via golang-nuts
> To view this discussion visit https://groups.google.com/d/msgid/golang-nuts/0479f342-0b35-49b4-be81-c631bfe15900n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages