这是在golang中终止goroutine的正确方式吗?

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

Is this the correct way to terminate a goroutine in golang?

问题

我正在构建一个命令行应用程序,需要启动自己的HTTP服务器。
我的想法是在主程序中的goroutine中启动HTTP服务器,代码如下:

func main() {
    go myserver()

    // 执行许多耗时的任务
}

func myserver() {
    s := &http.Server{
        Addr:    ":8080",
        Handler: MyHandler{},
    }
    log.Fatal(s.ListenAndServe())
}
  1. 我的主函数需要能够告诉goroutine停止。由于Web服务器在ListenAndServe方法上阻塞,无法通过sync.WaitGroup或通道进行信号传递。
  2. 我的一个想法是向Web服务器发出HTTP POST请求,并要求在接收到特定约定值时自行关闭。

是否有其他符合Go语言习惯的替代方法?

英文:

I am building a command line app which needs to start its own HTTP server.
My idea is to start the http server inside a goroutine in my main program like this:

func main() {
    go myserver()

    // perform lots of time-consuming tasks
}

func myserver() {
    s := &http.Server{
        Addr: ":8080",
        Handler: MyHandler{},
    }
    log.Fatal(s.ListenAndServe())
}
  1. My main function needs to be able to tell the goroutine to die. Since the webserver is blocking on the ListenAndServe method, there is no way of signalling via a sync.WaitGroup or a channel.
  2. One idea I have is to make a HTTP POST to the webserver and ask it to commit suicide on receiving a specific agreed-upon value.

Are there other alternatives that are idiomatic to golang ?

答案1

得分: 6

如果你在程序退出之前一直运行Web界面,你可以直接从主线程返回。即使有其他goroutine在工作,从主线程返回也会退出程序。如果你希望主线程发送一个终止信号给HTTP服务器,最好的方法是关闭监听器。

func main() {
    l, err := net.Listen("tcp", ":8080")
    if err != nil {
        log.Fatal(err)
    }
    go myserver(l)

    // 执行大量耗时任务

    l.Close()
}

func myserver(l net.Listener) {
    s := &http.Server{
        Addr:    ":8080",
        Handler: MyHandler{},
    }
    log.Print(s.Serve(l))
}

这样做不会关闭已经打开的连接,只会阻止新连接的创建。你需要在处理程序中添加逻辑和/或停止保持活动连接,以确保在关闭监听器后不会长时间重用连接。

英文:

If you are running the web interface until the program exits, you can just return from the main thread. Returning from the main thread exits the program even if another goroutine is working. If you want the main thread to send a kill to the http server, the best way is to close the listener.

func main() {
	l, err := net.Listen("tcp", ":8080")
	if err != nil {
		log.Fatal(err)
	}
	go myserver(l)

	// perform lots of time-consuming tasks

	l.Close()
}

func myserver(l net.Listener) {
	s := &http.Server{
		Addr:    ":8080",
		Handler: MyHandler{},
	}
	log.Print(s.Serve(l))
}

This will not close connections already open. It will only prevent new ones from being created. You need to add logic to your handler and/or stop keep-alives to ensure that the connection isn't reused for long after you kill listener.

huangapple
  • 本文由 发表于 2014年3月3日 12:18:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/22138588.html
匿名

发表评论

匿名网友

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

确定