OpenMix produced: https://openmix.org

Mix VarWatch

Monitor the data changes of the configuration structure variables and perform some tasks

Monitor the data changes of configuration structure variables and perform some tasks

Source address

Star won’t get lost, you can find it next time you use it

Installation

go get github.com/mix-go/varwatch

Usage

When using spf13/viper jinzhu/configor this kind of binding variable configuration library to dynamically update the configuration information

Any configuration library that uses the &Config pointer to bind data can be used
var Config struct {
    Logger struct {
        Level int `json:"level"`
    } `json:"logger" varwatch:"logger"`
    Database struct {
        User    string `json:"user"`
        Pwd     string `json:"pwd"`
        Db      string `json:"db"`
        MaxOpen int    `json:"max_open"`
        MaxIdle int    `json:"max_idle"`
    } `json:"database" varwatch:"database"`
}

err := viper.Unmarshal(&Config)

Take the example of dynamically modifying the log level: when Config.Logger.Level changes, we need to execute some code to modify the log level

  • First configure the Logger node with varwatch:"logger" label information
  • Then use the following code to execute the monitoring logic
w := varwatch.NewWatcher(&Config, 10 * time.Second)
w.Watch("logger", func() {
    // 获取变化后的值
    lv := Config.Logger.Level
    // 修改 logrus 的日志级别
    logrus.SetLevel(logrus.Level(uint32(lv)))
})

Need to dynamically modify the connection pool information, or the database account password can be achieved through the above example.

License

Apache License Version 2.0, http://www.apache.org/licenses/


撸代码的乡下人
252 声望46 粉丝

我只是一个单纯爱写代码的人,不限于语言,不限于平台,不限于工具