<p>I have a small embedded webserver in an application and I want the "client" to be able to perform a post to it, and for the server to acknowledge the post and continue to process it. I've done a small test showing that I can spawn a goroutine and it will continue to execute, but I am curious to know if this is a good approach. I should mention that if the async part of the operation fails, the client doesn't need to be informed. </p>
<hr/>**评论:**<br/><br/>TheMerovius: <pre><p>For a standalone binary it's fine, especially when it doesn't matter if the thing succeeds or not. If it matters, you should inform the client somehow. And wrap additional logic around it for retries.</p></pre>caseynashvegas: <pre><p>Thanks, great feedback.</p></pre>Incisiv3: <pre><p>It depends on the type of app and expected traffic but yeah that's fine, however one way I've accomplished this before is to setup a pool of go-routines that wait for work (requests in this case) and are sent the data via a channel that all the workers pull from. </p>
<p>The goroutine pool can either be a fixed size or auto scaling based on how many requests are sitting in the queue pending processing. That way if you get an influx of web requests you aren't overloading the server with goroutines and you're able to set a cap on the number that are spawned, and adequately adjust the size automatically to handle the back log then scale it back down once the queue is lower for a certain period of time.</p></pre>TheMerovius: <pre><p>I think, if you don't fan-out too much with goroutines, you will die for other reasons before you die because of "too many goroutines" (especially as, AFAIK, the http server spawns a goroutine per request anyway).</p>
<p>I'm baffled by the popularity of the "goroutine-pool" pattern. I mean, that's basically what goroutines are there to abstract <em>away</em>, they are essentially a well-working threadpool.</p></pre>caseynashvegas: <pre><p>I wasn't considering go routine pool for my use case. </p></pre>caseynashvegas: <pre><p>Thanks! </p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传