I have a program which has a pointer to configuration information. Upon each request, the config information is used to determine how to handle the request.
The program will periodically look for changes in the config file and reload them. When changes are detected, the configuration object is built up again fresh and then simply reassigned to the
configuration variable. I was thinking that if I assign this variable atomically, there would be no need for use of RWMutex or anything, however I can' figure out how
to use LoadPointer to get back to my original type to use it. I'm assuming if I do StorePointer, I should LoadPointer to read it consistently.
Or am I approaching this all wrong?
// quick example with comments
package hello
import (
"sync/atomic"
"unsafe"
)
type Config struct {
}
type Server struct {
Configuration *Config
}
func (server *Server) loadConfig(configFile string) {
newConfig := new(Config)
// load config as necessary
// if I perform atomic assignment of (server.Configuration = newConfig), new config is picked up immediately on next read?
// is this right?
currentConfigPtr := unsafe.Pointer(server.Configuration)
newConfigPtr := unsafe.Pointer(newConfig)
atomic.StorePointer(¤tConfigPtr, newConfigPtr)
}
func (server *Server) handleRequest() {
// do I need to read config atomically with LoadPointer?
// if so, how do I do that. I've tried several things and
// can't figure out how to cast back to my original type
// or should I use RWMutex to faciliate access to config object?
}
Thanks!
Shane