Calling net/http.Shutdown() from a handler?

blov · · 381 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I am doing something similar with a global variable by setting an infinite loop that breaks when the handler changes the global variable.</p> <p>It seems super hacky though, and I would like to more cleanly do this if at all possible. </p> <p>The problem seems to be that the handler (not done as a closure) isn&#39;t really &#34;aware&#34; of the server, and I don&#39;t really know how to make it aware of the server since the handler has to be defined by: <code>handler func(ResponseWriter, *Request))</code> <em>ie</em> there doesn&#39;t seem to be room to pass the server in to the handler as a variable. Similarly the handler doesn&#39;t return anything.</p> <p>Hopefully that makes sense. If not, I&#39;d be glad to clear anything up.</p> <hr/>**评论:**<br/><br/>everdev: <pre><p>It seems odd to shutdown a server after an HTTP request to a specific handler, but in any event, interfaces can have receivers:</p> <pre><code>type Env struct { Server *http.Server // any other environment variables } func (env *Env) ShutdownHandler(w http.ResponseWriter, r *http.Request) { env.Server.Shutdown() } </code></pre></pre>qspec02: <pre><p>Thanks. That&#39;s a far easier solution than I was expecting. I was so hung up on finding something in the net/http package.</p> <p>Thanks again!</p></pre>

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

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