<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'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'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'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'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'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't create a new connection for each request.</p></pre>icholy: <pre><p>It's not creating a new connection per request. They'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'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 "initializes the Datastore and attaches to the context of every http.Request". Also that is a local var, I don'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, "Store is a middleware function", 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 "attaches that connection to the context of every http.Request".</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
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传