英文:
How to stop and start gin server multiple times in one run
问题
我正在编写一个程序,在程序运行时需要多次启动和停止我的服务器(在这种情况下使用Gin框架)。
停止Gin服务器本身需要一个技巧,我在这个问题中找到了解决方法:
https://stackoverflow.com/questions/65686384/graceful-stop-of-gin-server
但是这种方法会阻止我的程序在未来启动Gin服务器,根据http.Server.Shutdown()
方法的文档,一旦在服务器上调用了Shutdown方法,就不能再重用它;对Serve等方法的未来调用将返回ErrServerClosed。
我确实需要未来的调用。
附加信息
Fiber可以轻松处理这种情况,但我想用Gin来实现。
我希望有类似于以下Fiber代码的功能:
fiber := NewFiberApp()
fiber.RegisterRoutes()
fiber.Start() // 在内部调用fiber.Listen()
fiber.Stop() // 在内部调用fiber.Shutdown()
fiber.Start()
fiber.Stop()
fiber.Start()
它按照我的期望工作。
英文:
I'm writing a program that I need to Start and Stop my server (using Gin framework in this case) multiple times when my program is running,
Stopping a gin server itself needed a trick that I found in this question:
https://stackoverflow.com/questions/65686384/graceful-stop-of-gin-server
But this kind of approach will prevent my program to start the gin server in the future, according to the documentation of http.Server.Shutdown()
method that says:
> Once Shutdown has been called on a server, it may not be reused; future calls to methods such as Serve will return ErrServerClosed.
I exactly need that future calls.
Aditional Information
Fiber can handle this situation so easily, but I want to make it with gin.
I want something like this fiber code:
fiber := NewFiberApp()
fiber.RegisterRoutes()
fiber.Start() // Calls fiber.Listen() Under the hood
fiber.Stop() // Calls fiber.Shutdown() Under the hood
fiber.Start()
fiber.Stop()
fiber.Start()
And it works as I expect.
答案1
得分: 2
你需要从头开始创建服务器结构体!
不能再使用已关闭的 http.Server
。
func serve() *http.Server {
router := gin.Default()
router.GET("/", func(c *gin.Context) {
time.Sleep(5 * time.Second)
c.String(http.StatusOK, "Welcome Gin Server\n")
})
srv := &http.Server{
Addr: ":8080",
Handler: router,
}
go func() {
// service connections
if err := srv.ListenAndServe(); err != nil {
log.Printf("listen: %s\n", err)
}
}()
return srv
}
func main() {
{
srv := serve()
time.Sleep(time.Second * 3)
fmt.Println("down", srv.Shutdown(context.Background()))
}
{
time.Sleep(time.Second * 3)
fmt.Println("up", serve())
}
select {}
}
英文:
You need to create the server struct from scratch!
Can't use the closed http.Server
again.
func serve() *http.Server {
router := gin.Default()
router.GET("/", func(c *gin.Context) {
time.Sleep(5 * time.Second)
c.String(http.StatusOK, "Welcome Gin Server\n")
})
srv := &http.Server{
Addr: ":8080",
Handler: router,
}
go func() {
// service connections
if err := srv.ListenAndServe(); err != nil {
log.Printf("listen: %s\n", err)
}
}()
return srv
}
func main() {
{
srv := serve()
time.Sleep(time.Second * 3)
fmt.Println("down", srv.Shutdown(context.Background()))
}
{
time.Sleep(time.Second * 3)
fmt.Println("up", serve())
}
select {}
}
答案2
得分: 0
我尝试了很多方法,最终写了一篇文章,这将帮助你解决问题:
https://dev.to/arshamalh/trying-to-shutdown-a-gin-server-goroutines-and-channels-tips-gf3
英文:
I tried many ways and finally wrote an article as a result, this will help you to solve the problem:
https://dev.to/arshamalh/trying-to-shutdown-a-gin-server-goroutines-and-channels-tips-gf3
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论