Golang select issue -- never timing out.

polaris · · 732 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Hello all.</p> <p>I&#39;ve been playing with this for about an hour or so now and I can&#39;t seem to find out why this never times out. The waitgroup blocks indefinitely (for testing) so that time.After() should trigger. Does anyone have any ideas? Thanks!</p> <pre><code>// A function that will block and only return when all connections have ended. var waitForConnections = func() (ok chan bool) { // Wait for the connections to end server.waitgroup.Wait(); // Send true to the channel ok &lt;- true; return ok; }; select { // Wait for all connections to end case &lt;-waitForConnections(): output.Log.Debug(&#34;Waited for connections to end...&#34;); // Since we don&#39;t want to wait indefinitely, we&#39;ll have a 45 second timeout. case &lt;-time.After(time.Second * 20): return fmt.Errorf(&#34;Graceful server shutdown timeout.&#34;); } </code></pre> <hr/>**评论:**<br/><br/>djherbis: <pre><p>All args to the select statement must be evaluated before it actually performs the select. <a href="https://golang.org/ref/spec#Select_statements">https://golang.org/ref/spec#Select_statements</a></p> <p>waitForConnections() is blocking indef before returning. Which means the select statement is stuck waiting for the ok channel to be returned before it can actually do the select statement.</p> <p>To resolve this you should return the channel from waitForConns right away, and run the server.waitgroup.Wait() and ok &lt;- true, call in a separate goroutine.</p> <p><strong>edit</strong> also note that you will need to do ok = make(chan bool), otherwise it will be a nil channel</p></pre>divoxx: <pre><p>What djherbis said.</p> <p>Here is an example that works: <a href="http://play.golang.org/p/cAtcG68W8a">http://play.golang.org/p/cAtcG68W8a</a></p></pre>JakeTheDev: <pre><p>Thanks!</p></pre>JakeTheDev: <pre><p>Thank you for the simple explanation -- suddenly selects/channels make a ton more sense. Solved my problem. :)</p></pre>dericofilho: <pre><p>Can you provide a playground link? (<a href="http://play.golang.org/" rel="nofollow">http://play.golang.org/</a>) with the whole code if possible?</p> <p>Besides the unusual function declaration, it seems OK. </p></pre>icholy: <pre><p>I hope you&#39;re not defining functions like this</p> <pre><code>// A function that will block and only return when all connections have ended. var waitForConnections = func() (ok chan bool) { // Wait for the connections to end server.waitgroup.Wait(); // Send true to the channel ok &lt;- true; return ok; }; </code></pre></pre>danpaquette: <pre><p>I hope you&#39;re not responding to someone&#39;s request for help like this</p> <blockquote> <p>I hope you&#39;re not defining functions like this</p> </blockquote> <pre><code>// A function that will block and only return when all connections have ended. var waitForConnections = func() (ok chan bool) { // Wait for the connections to end server.waitgroup.Wait(); // Send true to the channel ok &amp;lt;- true; return ok; }; </code></pre></pre>JakeTheDev: <pre><p>Actually only that one since: (1) it&#39;s really just a channel wrapper around a waitgroup.wait() (2) more concerned with PoC right now :)</p></pre>paddie: <pre><p>You are not very helpful, are you?</p></pre>

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

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