如何在一次运行中多次停止和启动gin服务器

huangapple go评论158阅读模式
英文:

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

huangapple
  • 本文由 发表于 2022年9月1日 07:55:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/73563023.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定