Przeglądaj źródła

ConfigStore: clarify intended use

tags/v2.10.0-rc1
Shivaram Lingamneni 2 lat temu
rodzic
commit
34ad3a2dc1
1 zmienionych plików z 10 dodań i 9 usunięć
  1. 10
    9
      irc/utils/config.go

+ 10
- 9
irc/utils/config.go Wyświetl plik

@@ -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
 }

Ładowanie…
Anuluj
Zapisz