<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>💡 Blog Entries on Igor's Tech Corner</title><link>https://yngvest.github.io/blog/posts/</link><description>Recent content in 💡 Blog Entries on Igor's Tech Corner</description><generator>Hugo</generator><language>en-us</language><lastBuildDate>Thu, 16 Jan 2025 19:20:26 +0200</lastBuildDate><atom:link href="https://yngvest.github.io/blog/posts/index.xml" rel="self" type="application/rss+xml"/><item><title>Response Body and Connection Reuse in Go</title><link>https://yngvest.github.io/blog/posts/go-pconn/</link><pubDate>Thu, 16 Jan 2025 19:20:26 +0200</pubDate><guid>https://yngvest.github.io/blog/posts/go-pconn/</guid><description>&lt;h2 id="introduction"&gt;Introduction&lt;/h2&gt;
&lt;p&gt;If you have ever used the &lt;code&gt;net/http&lt;/code&gt; package in Go, you know that you should close &lt;code&gt;Response.Body&lt;/code&gt;. The &lt;code&gt;io.ReadCloser&lt;/code&gt; type and its accompanying comment make this very clear.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;type&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;Response&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;struct&lt;/span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#f92672"&gt;...&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;// The http Client and Transport guarantee that Body is always&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;// non-nil, even on responses without a body or responses with&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;// a zero-length body. It is the caller&amp;#39;s responsibility to&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;// close Body. The default HTTP client&amp;#39;s Transport may not&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;// reuse HTTP/1.x &amp;#34;keep-alive&amp;#34; TCP connections if the Body is&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;// not read to completion and closed.&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;//
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt; // The Body is automatically dechunked if the server replied&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;// with a &amp;#34;chunked&amp;#34; Transfer-Encoding.&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;// ...&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;Body&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;io&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;ReadCloser&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#f92672"&gt;...&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;If you are attentive, you might also notice the part about reusing the connection. The comment states that for the underlying connection to be reused, the &lt;code&gt;Body&lt;/code&gt; should be read to completion. However, for me, it&amp;rsquo;s unclear who&amp;rsquo;s responsible for that. Why? I am not exactly sure - maybe because I&amp;rsquo;ve gotten used to &lt;code&gt;Close&lt;/code&gt; methods handling all termination logic, or because the comment on the next line looks like a good-to-know detail rather than a call to action.&lt;/p&gt;</description></item></channel></rss>