Reliably detect database/sql connection leaks

polaris · · 607 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I recently encountered a PostgreSQL connection leak due to my forgetting to close a <em>database/sql</em> &#39;rows&#39; object. I only discovered this after adding enough unit tests to max out my Postgres connection limit. Is there a clean way to reliably detect this mistake in test that doesn&#39;t rely on such a coincidence?</p> <hr/>**评论:**<br/><br/>cs-guy: <pre><p>Try this: At the beginning of your test call <code>db.MaxIdleConns(0)</code>. Before your test exits verify that <code>db.Stats().OpenConnections == 0</code>.</p></pre>UniverseCity: <pre><p>Exits*</p></pre>kardianos: <pre><p>This is kinda an aside: I&#39;m working on a v2 of my rdb (relational db interface) package. This release will include support for context. Every query will have a context associated with it. Once that context is done (which should be when the request is done) that query/db conn will be finished and returned to the pool. Thus so long as your request have a context with a deadline associated with it that get&#39;s canceled when the request is done, database connections won&#39;t leak.</p> <p>I know that doesn&#39;t help you now... but...</p></pre>watr: <pre><p>What&#39;s your lib&#39;s name?</p></pre>tusharsingh: <pre><p>Wrap your DB connection with a struct that implements query, exec, etc. the common calls that you use. Use this struct for your DB connection with an embedded sql.DB.</p> <p>You can then include counter code or locks to see if you&#39;re leaving anything behind.</p> <p>The way I&#39;ve used it in dev/testing is to only allow one query at a time to prevent overlapping requests that haven&#39;t been closed. The program panics if you open a new query while still using an existing conn.</p></pre>bloodqc: <pre><p>Maybe with <a href="https://golang.org/pkg/database/sql/#DBStats" rel="nofollow">DBStats</a>. There&#39;s <code>OpenConnections</code> but I don&#39;t know if connections are kept open for better performance.</p></pre>warmans: <pre><p>I think they&#39;re easy to spot if you have telemetry data graphed out e.g. with Prometheus. Even just logging go routines would show leaks clearly on a graph since I think an open db cursor creates a gorourine. Another package like pprof would give you more info to actually fix the issue. A test would be better but having the graphs is a good catchall for this kind of thing.</p></pre>

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

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