Go http server and go routines.

blov · · 688 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I was just wondering if the go http server by default run each request by using a separate go routine to run the handler function. If it is so should one implement his own worker and request queues using channels and go routines ?</p> <p>My end goal is to increase the throughput of the server. </p> <hr/>**评论:**<br/><br/>The_Sly_Marbo: <pre><p>The <code>&#34;net/http&#34;</code> package does run each request in a separate goroutine, so it&#39;s fine for your handlers to be synchronous. If you need to use extra goroutines on top, that&#39;s also fine.</p></pre>adonovan76: <pre><p>Yes, the HTTP server runs each handler function in its own goroutine. This means you must not access global (or otherwise shared) variables from a handler without holding a mutex lock.</p> <p>The answer to your question depends on what your server does. A stateless server like a static content server can construct the response from the request (and read-only resources) entirely in the handler goroutine. A mostly stateless server may need to acquire a lock for a brief while to inspect or update a shared variable, or send a message to another goroutine and wait for a response. A highly stateful server like a database will have an elaborate locking or communication scheme.</p></pre>TheMerovius: <pre><p>Why do you think that worker and request queues will improve the throughput of the server? To me that sounds inherently slower (because you add another layer of scheduling on top of the one that the runtime already does with goroutines).</p> <p>The answers are, imho: Yes, every requests runs in it&#39;s own goroutine. No, you shouldn&#39;t implement a worker or request queue.</p> <p>Goroutines aren&#39;t threads for a reason. You shouldn&#39;t apply the patterns from thread-based programming to go, you should just use go&#39;s native concurrency model, it makes things a lot simpler.</p></pre>--Mister--j: <pre><p>Should I be using go routines in the handler functions then. Any resources or code samples would be helpful.</p></pre>tipsqueal: <pre><p>Adding a goroutine to your handler won&#39;t magically make your code run faster. In fact it will probably make it run slower. There is probably some other bottleneck you can try to fix.</p> <p>On a related note, do you really need to be worrying about any bottlenecks right now? Can you honestly say that you&#39;re going to get so much traffic that you need to be optimizing right now?</p></pre>--Mister--j: <pre><p>I guess not</p></pre>kron4eg: <pre><blockquote> <p>if the go http server by default run each request by using a separate go routine to run the handler function</p> </blockquote> <p>Yes, it will.</p> <blockquote> <p>If it is so should one implement his own worker and request queues using channels and go routines ?</p> </blockquote> <p>No, no need to do this, this is job for HAproxy/nginx in front of your code.</p></pre>

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

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