Drone using sql.Open multiple times, it's OK?

agolangf · · 358 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I know you should open the SQL connection once because it handles pooling for you, as talked about in this <a href="https://stackoverflow.com/questions/33578876/how-frequently-should-i-be-calling-sql-open-in-my-program">stackoverflow</a>.</p> <p>I was reading the Drone code and came across how they handle storage. They are storing the reference to the db in the context which makes testing easier. I&#39;m trying to understand why they open a new connect every request. They never call close so I assume it reuses the connection anyway.</p> <p><a href="https://github.com/drone/drone/blob/master/router/middleware/store.go">Here is the link</a> to their middleware using a new sql per request.</p> <p><a href="https://github.com/drone/drone/blob/master/store/datastore/store.go">Here is the link</a> to their datastore.new code.</p> <p>I like how they put the db in the context but am not sure why they wouldn&#39;t open the db once.</p> <p>Thanks</p> <hr/>**评论:**<br/><br/>loganjspears: <pre><p>Did you open an issue?</p></pre>peppage: <pre><p>I will but I was going under the assumption they were doing it a way I didn&#39;t understand. I was hoping someone here would have more insight into database/sql or why this worked.</p></pre>Ploobers: <pre><p>I think they chose datastore flexibility and testing over performance. There&#39;s absolutely no reason to open a new connection per request. They set <code>MaxIdleConnections = 0</code>, so the connection will be terminated once it is determined to be idle.</p></pre>the_web_dev: <pre><p>I second this. Context is a highly debatable topic in regards to what should be included within it and establishing a new connection per request goes against all the idioms I&#39;ve seen in Go regarding databases in general. Usually you stash a pool on a global and draw from it as needed.</p></pre>icholy: <pre><p>Read the code, it doesn&#39;t create a new connection for each request.</p></pre>icholy: <pre><p>It&#39;s not creating a new connection per request. They&#39;re <a href="https://github.com/drone/drone/blob/5130919869a1cf855975870fdf9ac72624f53be5/router/middleware/store.go#L13-L19" rel="nofollow">creating an instance</a> of the store when the middleware is initialised. Every request gets the same instance of the store. It&#39;s initialized once <a href="https://github.com/drone/drone/blob/5130919869a1cf855975870fdf9ac72624f53be5/drone/server.go#L295" rel="nofollow">here</a>. </p></pre>peppage: <pre><p>I thought middleware was supposed to run on every request and the comments clearly state &#34;initializes the Datastore and attaches to the context of every http.Request&#34;. Also that is a local var, I don&#39;t think Gin is saving that in a global context.</p></pre>icholy: <pre><p>The comment is saying that 1. It creates the store and 2. It attaches that store to every request.</p> <p>The instance is created once and the middleware (the function) closes over that instance. So every time the middleware is used (the function is called) it is referencing the same store instance.</p></pre>peppage: <pre><p>Thanks, I get it now. I appreciate it!</p></pre>tcrypt: <pre><p>The first part of the comment, &#34;Store is a middleware function&#34;, is wrong. <code>Store(cli *cli.Context) gin.HandlerFunc</code> is not a middleware function, it <em>returns</em> a middleware function. One that encloses over the created connection. When the server starts up it calls <code>Store(cli *cli.Context) gin.HandlerFunc</code> once to create a connection and return a middleware that &#34;attaches that connection to the context of every http.Request&#34;.</p></pre>peppage: <pre><p>OK that clears it up for me, thanks. I typically use Iris so it works a little differently. I appreciate you taking the time to help</p></pre>

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

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