英文:
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())
}
- 我的主函数需要能够告诉goroutine停止。由于Web服务器在
ListenAndServe
方法上阻塞,无法通过sync.WaitGroup
或通道进行信号传递。 - 我的一个想法是向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())
}
- 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 async.WaitGroup
or a channel. - 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论