<p>Hello all.</p>
<p>I've been playing with this for about an hour or so now and I can'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 <- true;
return ok;
};
select {
// Wait for all connections to end
case <-waitForConnections():
output.Log.Debug("Waited for connections to end...");
// Since we don't want to wait indefinitely, we'll have a 45 second timeout.
case <-time.After(time.Second * 20):
return fmt.Errorf("Graceful server shutdown timeout.");
}
</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 <- 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'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 <- true;
return ok;
};
</code></pre></pre>danpaquette: <pre><p>I hope you're not responding to someone's request for help like this</p>
<blockquote>
<p>I hope you'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 &lt;- true;
return ok;
};
</code></pre></pre>JakeTheDev: <pre><p>Actually only that one since:
(1) it'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
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传