mirror of
https://github.com/hanzoai/tasks.git
synced 2026-10-04 02:04:03 +00:00
## What changed? Use unique.Handle for dynamicconfig.Keys instead of strings. ## Why? Force keys to always be lowercase so we don't have to convert at lookup time, and also speed up lookups by interning. ## How did you test it? - [x] covered by existing tests
45 lines
989 B
Go
45 lines
989 B
Go
package dynamicconfig
|
|
|
|
import (
|
|
"fmt"
|
|
"sync/atomic"
|
|
)
|
|
|
|
type (
|
|
registry struct {
|
|
settings map[Key]GenericSetting
|
|
queried atomic.Bool
|
|
}
|
|
)
|
|
|
|
var (
|
|
globalRegistry registry
|
|
)
|
|
|
|
func register(s GenericSetting) {
|
|
if globalRegistry.queried.Load() {
|
|
panic("dynamicconfig.New*Setting must only be called from static initializers")
|
|
}
|
|
if globalRegistry.settings == nil {
|
|
globalRegistry.settings = make(map[Key]GenericSetting)
|
|
}
|
|
if globalRegistry.settings[s.Key()] != nil {
|
|
// nolint:forbidigo // only called during static initialization
|
|
panic(fmt.Sprintf("duplicate registration of dynamic config key: %q", s.Key().String()))
|
|
}
|
|
globalRegistry.settings[s.Key()] = s
|
|
}
|
|
|
|
func queryRegistry(k Key) GenericSetting {
|
|
if !globalRegistry.queried.Load() {
|
|
globalRegistry.queried.Store(true)
|
|
}
|
|
return globalRegistry.settings[k]
|
|
}
|
|
|
|
// For testing only; do not call from regular code!
|
|
func ResetRegistryForTest() {
|
|
globalRegistry.settings = nil
|
|
globalRegistry.queried.Store(false)
|
|
}
|