<p>I was watching <a href="https://www.youtube.com/watch?v=QDDwwePbDtw">https://www.youtube.com/watch?v=QDDwwePbDtw</a> today, at about ~25 min he demos the "move Fetch to it's own goroutine" solution and claims that it's all safe, no leaks, cleaning up, etc.</p>
<p>Isn't it plausible however that if you <code>Close()</code> the <code>Subscriber</code> that he makes that there's nobody ever who can listen on <code>fetchDone</code>, so the fetch goroutine blocks after it comes out of doing IO if the <code>for{select{}}</code> has already been cancelled?</p>
<p>Just curious, of course it can be a mistake. I've been doing go for years and still sometimes struggle to find patterns to clean up potentially leaked goroutines, rewatching this now because it came up at work and we were lamenting this talk not using <code>context.Context</code> because we'd have liked to have seen it.</p>
<hr/>**评论:**<br/><br/>nsd433: <pre><p>Note I didn't watch 25 minutes of video. I'm just looking at what's in the video at 25:01. There it's not going to block because the fetchDone chan has capacity to hold the fetchResult. The result will be placed in the channel's queue, then the fetcher goroutine exits, and the channel and its queue get gc'ed and we're done.</p></pre>Loves_Portishead: <pre><p>Right, it's a buffered channel, and presumably the GC catches them all. Well spotted, I three bytes makes all the difference <code>, 1</code>!</p></pre>nsd433: <pre><p>A channel with cap=1 for this kind of pattern is pretty standard. It's either that or a select with two cases: deliver the reply or notice a cancelation.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传