Skip to content

Instantly share code, notes, and snippets.

@kougazhang
Created January 4, 2022 09:46
Show Gist options
  • Save kougazhang/e9ba3845cbb7400fffabeb5b69dc5276 to your computer and use it in GitHub Desktop.
Save kougazhang/e9ba3845cbb7400fffabeb5b69dc5276 to your computer and use it in GitHub Desktop.
#golang #geektime
package main
func main() {
// holds current server configuration 动态更新当前服务的配置
var config atomic.Value
go func() {
// Reload config every 10 seconds 每 10 秒重载配置
// and update config value with the new version 用新版本的值更新配置项
for {
time.Sleep(10 * time.Second)
config.Store(loadConfig())
}
}()
// Create worker goroutines that handle incoming requests
// using the latest config value. 处理新请求时创建的 goroutine worker 使用最新的配置
for i := 0; i < 10; i++ {
go func() {
for r := range requests() {
c := config.Load()
// Handle request r using config c.
_, _ = r.c
}
}()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment