|
@@ -11,22 +11,23 @@ import (
|
11
|
11
|
/*
|
12
|
12
|
This can be used to implement the following pattern:
|
13
|
13
|
|
14
|
|
-1. Load and munge a config (this can be arbitrarily expensive)
|
15
|
|
-2. Use Set() to install the config
|
16
|
|
-3. Use Get() to access the config
|
17
|
|
-4. As long as any individual config is not modified (by any goroutine)
|
18
|
|
- after the initial call to Set(), this is free of data races, and Get()
|
|
14
|
+1. Prepare a config object (this can be arbitrarily expensive)
|
|
15
|
+2. Take a pointer to the config object and use Set() to install it
|
|
16
|
+3. Use Get() to access the config from any goroutine
|
|
17
|
+4. To update the config, call Set() again with a new prepared config object
|
|
18
|
+5. As long as any individual config object is not modified (by any goroutine)
|
|
19
|
+ after it is installed with Set(), this is free of data races, and Get()
|
19
|
20
|
is extremely cheap (on amd64 it compiles down to plain MOV instructions).
|
20
|
21
|
*/
|
21
|
22
|
|
22
|
|
-type ConfigStore[T any] struct {
|
|
23
|
+type ConfigStore[Config any] struct {
|
23
|
24
|
ptr unsafe.Pointer
|
24
|
25
|
}
|
25
|
26
|
|
26
|
|
-func (c *ConfigStore[T]) Get() *T {
|
27
|
|
- return (*T)(atomic.LoadPointer(&c.ptr))
|
|
27
|
+func (c *ConfigStore[Config]) Get() *Config {
|
|
28
|
+ return (*Config)(atomic.LoadPointer(&c.ptr))
|
28
|
29
|
}
|
29
|
30
|
|
30
|
|
-func (c *ConfigStore[T]) Set(ptr *T) {
|
|
31
|
+func (c *ConfigStore[Config]) Set(ptr *Config) {
|
31
|
32
|
atomic.StorePointer(&c.ptr, unsafe.Pointer(ptr))
|
32
|
33
|
}
|