The hot update of the configuration file is a basic function of the server program. Through the hot update, the configuration of the program can be adjusted without stopping, especially in the production environment. For example, if you find that the log is too much, you can dynamically increase the log level, change in business logic parameters, and even the switches of a certain functional module can be dynamically adjusted.
package main import ( "encoding/json" "fmt" "io/ioutil" "log" "os" "os/signal" "sync" "syscall" ) //Configuration test with jsontype Config struct { Test1 string `json:"Test1:` Test2 int `json:"Test1:` } var ( config *Config configLock = new() ) func loadConfig() bool { f, err := ("") if err != nil { ("load config error: ", err) return false } // Different configuration rules, different resolution complexity temp := new(Config) err = (f, &config) if err != nil { ("Para config failed: ", err) return false } () config = temp () return true } func GetConfig() *Config { () defer () return config } func init() { if !loadConfig() { (1) } //There may be multiple triggering methods for hot update configuration, which is implemented using the system semaphore sigusr1 s := make(chan , 1) (s, syscall.SIGUSR1) go func() { for { <-s ("Reloaded config:", loadConfig()) } }() } func main() { select {} }
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.