<p>I can't find any straightforward easy to understand example for 1.8's graceful shutdown. Can someone help me?</p>
<hr/>**评论:**<br/><br/>pierrrre: <pre><p>ListenAndServe() will return immediately after you call Shutdown().</p>
<p>Shutdown() will block until all HTTP request are done.</p></pre>gotBanana: <pre><p>could you please provide me a simple working example? thank you</p></pre>Shammyhealz: <pre><pre><code>ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
err := srv.Shutdown(ctx)
</code></pre>
<p>Haven't tested that it works, but that should cause the HTTP server to shutdown gracefully, or return an error after 5 seconds.</p></pre>bear1728: <pre><p>I've been using something like this</p>
<p><a href="https://play.golang.org/p/_Q3q5-fidF" rel="nofollow">https://play.golang.org/p/_Q3q5-fidF</a></p>
<p>It might not pass all the linters as is, but maybe this can give you something to start with. When you press control-C (not on the playground of course) or the equivalent, you should see the shutdown message.</p></pre>sh41: <pre><p>Not a big deal, but it's pointless to listen for the <code>os.Kill</code> signal. Your code will never see it; the OS intercepts it at a higher level and kills the process.</p></pre>sh41: <pre><p>This is an example of forceful shutdown (i.e., <code>Close()</code> method) on interrupt signal (SIGINT):</p>
<p><a href="https://github.com/shurcooL/home/blob/9c57e59a05baa9513b847fd945865935b7e529c2/main.go#L144-L161" rel="nofollow">https://github.com/shurcooL/home/blob/9c57e59a05baa9513b847fd945865935b7e529c2/main.go#L144-L161</a></p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传