<p>Does anybody know how to change the default TCP keepalive timeout in net/http?</p>
<p>I'm building a backend for a mobile app, and while intercepting packets with Wireshark I'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'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="http://play.golang.org/p/bL1gIUID-p">http://play.golang.org/p/bL1gIUID-p</a></p></pre>dinkumator: <pre><p>If you'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>