Making actions in another go-routine for sequential code

xuanbao · · 439 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I have an API service that at some point should make an http request to an API service that checks access rights. Is there any point of making that call concurrently in contrast to simply making a call?</p> <p>For example, </p> <pre><code>func VerifyAccess(someParam string) (bool,error) { //Should I make call in another go routine ? // Like this for example done := make(chan bool) go func() { makeHttpCall() done &lt;- } &lt;-done /* some other actions here that can&#39;t proceed without the response from the action above */ // Or should I just make regular call and proceed depending on it state // Like this makeHttpCall() /* Proceed to do other actions that depend on the result from makeHttpCall() /* } </code></pre> <p>My main questions is whether there is actual performance benefit in using concurrency when the very next operation depends on the result of the concurrent operation?</p> <hr/>**评论:**<br/><br/>theclapp: <pre><blockquote> <p>whether there is actual performance benefit in using concurrency when the very next operation depends on the result of the concurrent operation</p> </blockquote> <p>No. Just make the call synchronously.</p></pre>d3sire: <pre><p>Thanks. That was my feeling.</p></pre>theclapp: <pre><p>So the obvious question is, why were you even in doubt?</p></pre>tylermumford: <pre><p>Concurrency is sometimes seen as a guaranteed way to increase performance. :shrug:</p></pre>mre12345: <pre><p>I&#39;m not OP but I have a follow up question. What if two people send a request to a specific endpoint at the exact same time? Won&#39;t one request have to wait for the other?</p> <p>For example if I had this server in node:</p> <pre><code>const express = require(&#39;express&#39;); const sleep = require(&#34;sleep&#34;); const app = express(); app.get(&#34;/&#34;, function() {sleep.sleep(5); res.body = &#34;finished&#34;}); app.listen(8888); </code></pre> <p>Then if I send two concurrent requests, the first request will show a response in 5 seconds and the second request will show a response in 10 seconds. </p></pre>djherbis: <pre><p>At least in Go, each request is handled by its own goroutine, so requests don&#39;t block one other (unless you explicitly block them with a lock or anther sync. method).</p> <p>Each of the requests in question would only block 5 seconds from the time of request.</p></pre>mre12345: <pre><p>Thanks for the clarification.</p></pre>theclapp: <pre><p>To expand a tiny bit on djherbis&#39;s reply: the OP is talking about sending a request, you&#39;re talking about handling a request. Opposite sides of the coin.</p></pre>

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

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