[Question] Number of channels and connection pools

xuanbao · · 685 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Hey everyone,</p> <p>I&#39;m slowly learning the concurrency principles integrated into the Go programming language.</p> <p>I wrote on paper what my app will look like. It will consist of n channels that will make network requests. The result of each channel will then be piped to n channels that will make database queries (most likely Redis).</p> <p>In other languages that I&#39;ve used in the past (like Node.js), the database driver was initialised once and created a connection pool of by default (usually) 5 connections to the database.</p> <p>My question is the following, if you have n channels that will make database queries, shouldn&#39;t you also set the connection pool size to n connections ?</p> <p>Thanks in advance!</p> <hr/>**评论:**<br/><br/>jjolla888: <pre><p>if the db requests are being performed faster than the incoming network requests, you should be ok to reduce the db pool size by the proportionate amount</p> <p>the other way is a little less determinate - if your db responses are slow such that your network gatherers are twiddling their thumbs, increasing the number of concurrent connections may not help - as it will depend on both your db and requesters hardware capabilities. </p></pre>calebdoxsey: <pre><p>You only need 2 channels.</p> <pre><code>var c1 chan databaseRequest var c2 chan redisRequest </code></pre> <p>Then make workers pull off the same channel:</p> <pre><code>for i :=0; i &lt; numberOfDatabaseWorkers; i++ { go func() { for { req := &lt;-c1 // do database stuff c2 &lt;- redisRequest{} } }() } for i := 0; i &lt; numberOfRedisWorkers; i++ { go func() { for { req := &lt;-c2 // do redis stuff } }() } </code></pre> <p>For cleanup you can either close the first channel, wait for all the database workers to finish and the close the second (look at <code>sync.WaitGroup</code> to see how to do that), or I tend to prefer some sort of <code>exit</code> channel that you close once and pass to every worker. Then every time they do a send/receive do it in a select:</p> <pre><code>select { case &lt;-exit: // when exit is closed this will happen return case req = &lt;-c1: } </code></pre> <p>As for the connection pool, <code>database/sql</code> is already using one. You can adjust the number of connections it will make with <a href="http://godoc.org/database/sql#DB.SetMaxOpenConns" rel="nofollow">http://godoc.org/database/sql#DB.SetMaxOpenConns</a>.</p> <p>I would first try it with the defaults and then if you are concerned about performance start tinkering with the options and benchmark/profile.</p> <p>But the defaults are pretty good and worrying about it now may be a sign of premature optimization.</p></pre>

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

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