Best 'graceful HTTP server' library?

xuanbao · · 966 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Hey all, looking for some advice on how to get a Go HTTP server to terminate gracefully on SIGINT. Right now we get a panic because we close the server while requests are still in flight. Ideally, we want closing the server to stop accepting new requests, then continue serving all requests until there aren&#39;t any left, then return control to the caller.</p> <p>There are a large number of competing packages that all claim to do the same thing. I&#39;m looking to consult the hive mind for which one is the best and, more importantly, the best maintained.</p> <ul> <li><a href="https://github.com/facebookgo/httpdown" rel="nofollow">facebookgo/httpdown</a></li> <li><a href="https://github.com/braintree/manners" rel="nofollow">braintree/manners</a></li> <li><a href="https://github.com/tylerb/graceful" rel="nofollow">tylerb/graceful</a></li> <li>Or, <a href="https://rcrowley.org/articles/golang-graceful-stop.html" rel="nofollow">rolling our own</a> with a sync.WaitGroup</li> </ul> <p>Would love to hear experiences from people who have tried one or more of the above options. Thanks!</p> <hr/>**评论:**<br/><br/>bradleyfalzon: <pre><p>An alternative approach, not using one or more of the above options, is to let the load balancer (LB) stop sending new requests.</p> <ol> <li>Have a health check mechanism checked by your LB, responding with http.StatusOK</li> <li>Catch SIGINT</li> <li>Set health check to respond with http.StatusServiceUnavailable</li> <li>Set a timeout to exit or sleep then exit after LB health check interval * LB health check failed probes + additional grace time for requests to finish</li> </ol> <p>This is the approach we took on a project due to not having time to rework and test the application before going live. The other options are better alternatives as they handover connections, without requiring long delays (especially in deployments across many servers).</p> <p>Just an option.</p></pre>divoxx: <pre><p>Just recently I researched those and I&#39;ve settled on tylerb/graceful because it seemed to handle everything a little bit better than the others. </p></pre>pkieltyka: <pre><p>there is also <a href="https://github.com/zenazn/goji/tree/master/graceful" rel="nofollow">https://github.com/zenazn/goji/tree/master/graceful</a> ..which even though is a subpackage of goji, it works with anything that uses http.Handler</p></pre>divoxx: <pre><p>This is old not supported anymore by the new goji package, github.com/goji/goji</p></pre>elithrar_: <pre><p>This is not true: Goji v1 is still supported, and the graceful package is pretty mature/battle tested.</p></pre>yimmy149: <pre><p>If your in-flight connections are not long lived, you can manually create a listener and close it to prevent new connections, then wait some predetermined period of time for sessions to end.</p> <p><a href="https://golang.org/pkg/net/http/#Serve" rel="nofollow">https://golang.org/pkg/net/http/#Serve</a></p> <p>This is how I&#39;m doing it (albeit with no timeout):</p> <p><a href="https://github.com/jhillyerd/inbucket/blob/master/httpd/server.go#L72" rel="nofollow">https://github.com/jhillyerd/inbucket/blob/master/httpd/server.go#L72</a></p></pre>kkirsche: <pre><p>Personally I&#39;d roll my own with a background process watching a channel for the os.Signal (SIGINT specifically) and kill the listener in there and wait for the wait group to complete. </p></pre>mwholt: <pre><p>I looked at all those, and I liked tylerb/graceful the most as far as usability/correctness goes. However one important thing to note: some of those don&#39;t handle graceful <em>restarts</em>, others do. Some are just graceful shutdown.</p> <p>I needed graceful restart but ended up rolling my own (needs were very specialized) using sync.WaitGroup.</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

966 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传