Golang 优雅地关闭 HTTP 服务器应该花费多长时间?

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

How long should Golang graceful HTTP shutdown take?

问题

根据https://golang.org/pkg/net/http/#Server.Shutdown的说明:

"then waiting indefinitely for connections to return to idle and then shut down"的意思是等待连接无限期地返回到空闲状态,然后关闭连接。如果提供的上下文在关闭完成之前过期,那么将返回上下文的错误。

这句话的意思是,在关闭服务器之前,会等待连接变为空闲状态,然后再关闭连接。如果在这个优雅的关闭过程完成之前提供的上下文超时,那么会返回上下文的错误。根据描述,如果超时的时间是几秒钟的量级,那么通常可以预期上下文超时会发生在优雅关闭完成之前。

英文:

According to https://golang.org/pkg/net/http/#Server.Shutdown

> Shutdown gracefully shuts down the server without interrupting any active connections. Shutdown works by first closing all open listeners, then closing all idle connections, and then waiting indefinitely for connections to return to idle and then shut down. If the provided context expires before the shutdown is complete, then the context's error is returned.

What does "then waiting indefinitely for connections to return to idle and then shut down" mean exactly? Should I expect that the context timeout should typically occur before this graceful shutdown finishes, given a timeout on the order of seconds?

答案1

得分: 1

“无限期等待连接返回空闲状态”是指已经作为传入连接的一部分调用的HTTP处理程序。所有这些处理程序需要关闭/返回,通常通过调用w.Write或w.WriteHeader来实现。

英文:

> waiting indefinitely for connections to return to idle

refers to HTTP Handlers that have already been called as part of incoming connections. All these handlers need to be closed / returned, usually by calling w.Write or w.WriteHeader

huangapple
  • 本文由 发表于 2017年6月5日 14:41:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/44363096.html
匿名

发表评论

匿名网友

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

确定