我如何在 Go 中查看或测试优雅重启?
How can I see or test graceful restarts in Go?
我通过 gin's https://github.com/fvbock/endless 提供 HTTP。我想看看与基本 HTTP 服务器的区别。
我发送了 syscall.SIGUSR1
信号:
syscall.Kill(getPid(), syscall.SIGUSR1)
应用程序没有退出,但我检测不到重启。
我要做的是在 toml 配置文件更改时为应用程序初始化新配置。
我的代码如下:
package main
import (
"os"
"fmt"
"syscall"
"github.com/gin-gonic/gin"
"github.com/fvbock/endless"
"github.com/BurntSushi/toml"
)
type Config struct {
Age int
Cats []string
}
var cfg Config
func restart(c *gin.Context) {
syscall.Kill(os.Getpid(), syscall.SIGUSR1)
}
func init() {
toml.DecodeFile("config.toml", &cfg)
fmt.Println("Testing", cfg)
}
func main() {
router := gin.New()
router.GET("/restart", restart)
if err := endless.ListenAndServe("localhost:7777", router); err != nil {
panic(err)
}
}
当我到达重启端点时,我想要打印出 toml 配置。
正在根据您对问题的更改更新答案。无尽的图书馆可以让你默认处理那个信号。您将需要注册一个挂钩。我在下面扩展了您的示例代码:
package main
import (
"os"
"fmt"
"syscall"
"github.com/gin-gonic/gin"
"github.com/fvbock/endless"
"github.com/BurntSushi/toml"
)
type Config struct {
Age int
Cats []string
}
var cfg Config
func restart(c *gin.Context) {
syscall.Kill(os.Getpid(), syscall.SIGUSR1)
}
func readConfig() {
toml.DecodeFile("config.toml", &cfg)
fmt.Println("Testing", cfg)
}
func main() {
readConfig()
router := gin.New()
router.GET("/restart", restart)
srv := endless.NewServer("localhost:7777", router)
srv.SignalHooks[endless.PRE_SIGNAL][syscall.SIGUSR1] = append(
srv.SignalHooks[endless.PRE_SIGNAL][syscall.SIGUSR1],
readConfig)
if err := srv.ListenAndServe(); err != nil {
panic(err)
}
}
现在,当您调用重启端点时,您应该会在标准输出中看到对配置文件的更改。但是,为了观察文件的变化,您需要使用 fsnotify
之类的东西
我通过 gin's https://github.com/fvbock/endless 提供 HTTP。我想看看与基本 HTTP 服务器的区别。
我发送了 syscall.SIGUSR1
信号:
syscall.Kill(getPid(), syscall.SIGUSR1)
应用程序没有退出,但我检测不到重启。
我要做的是在 toml 配置文件更改时为应用程序初始化新配置。
我的代码如下:
package main
import (
"os"
"fmt"
"syscall"
"github.com/gin-gonic/gin"
"github.com/fvbock/endless"
"github.com/BurntSushi/toml"
)
type Config struct {
Age int
Cats []string
}
var cfg Config
func restart(c *gin.Context) {
syscall.Kill(os.Getpid(), syscall.SIGUSR1)
}
func init() {
toml.DecodeFile("config.toml", &cfg)
fmt.Println("Testing", cfg)
}
func main() {
router := gin.New()
router.GET("/restart", restart)
if err := endless.ListenAndServe("localhost:7777", router); err != nil {
panic(err)
}
}
当我到达重启端点时,我想要打印出 toml 配置。
正在根据您对问题的更改更新答案。无尽的图书馆可以让你默认处理那个信号。您将需要注册一个挂钩。我在下面扩展了您的示例代码:
package main
import (
"os"
"fmt"
"syscall"
"github.com/gin-gonic/gin"
"github.com/fvbock/endless"
"github.com/BurntSushi/toml"
)
type Config struct {
Age int
Cats []string
}
var cfg Config
func restart(c *gin.Context) {
syscall.Kill(os.Getpid(), syscall.SIGUSR1)
}
func readConfig() {
toml.DecodeFile("config.toml", &cfg)
fmt.Println("Testing", cfg)
}
func main() {
readConfig()
router := gin.New()
router.GET("/restart", restart)
srv := endless.NewServer("localhost:7777", router)
srv.SignalHooks[endless.PRE_SIGNAL][syscall.SIGUSR1] = append(
srv.SignalHooks[endless.PRE_SIGNAL][syscall.SIGUSR1],
readConfig)
if err := srv.ListenAndServe(); err != nil {
panic(err)
}
}
现在,当您调用重启端点时,您应该会在标准输出中看到对配置文件的更改。但是,为了观察文件的变化,您需要使用 fsnotify
之类的东西