HTTP KeepAlive in Go

blov · · 2761 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Does anybody know how to change the default TCP keepalive timeout in net/http?</p> <p>I&#39;m building a backend for a mobile app, and while intercepting packets with Wireshark I&#39;ve realized that the connection times out after 10 seconds of inactivity, no matter what. Sending a keepalive:timeout=30 header from the client doesn&#39;t change anything, nor does setting ReadTimeout or WriteTimeout in Go.</p> <p>The client appears to be handshaking on the first request, maintaining that connection for all subsequent requests until 10 seconds pass with no activity, and then it handshakes again on the following request and repeats the cycle. Any ideas?</p> <hr/>**评论:**<br/><br/>firik: <pre><p>You need to create your own Client and Transport: <a href=""></a></p></pre>dinkumator: <pre><p>If you&#39;re using the package-level methods (<code>http.Get</code>/<code>http.Post</code>), you just need to update the default client which they use. Slap this somewhere in your code:</p> <pre><code>func init() { http.DefaultClient.Timeout = time.Second * 30 } </code></pre></pre>

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

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