Go newbie script stops responding

blov · · 476 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I am a newbie to Go coming from mostly dynamic languages (Javascript, PHP, Ruby). I wrote a simple script to ping urls every minute and then report to slack if they are down. The issue is my script seems to stop working after about six hours or so but the process remains running. I am having a bit of trouble figuring out why. Note that I have been working in Javascript mostly so the code may show that. Wondering if anyone sees any issues with the code or has any insights on why it would stop working. Thanks in advance.</p> <p><a href="https://gist.github.com/neb636/6883bd801ec171cabd99b28ace01b03a" rel="nofollow">https://gist.github.com/neb636/6883bd801ec171cabd99b28ace01b03a</a></p> <hr/>**评论:**<br/><br/>justinisrael: <pre><p>Here is a guess. You are using the default http client, which has no timeouts specified by default. On top of that, your schedule function continues to launch new goroutines and sleep. So a particular task could be hanging on requests and new tasks just get launched and stacked up. </p> <p><a href="https://medium.com/@nate510/don-t-use-go-s-default-http-client-4804cb19f779#.ctpy9xw7m" rel="nofollow">https://medium.com/@nate510/don-t-use-go-s-default-http-client-4804cb19f779#.ctpy9xw7m</a></p> <p>Try creating a client for your app, once, with really short timeouts, and share it across your tasks. That way it should return quickly if the site doesn&#39;t respond. </p></pre>justinisrael: <pre><p>Furthermore, is there any reason to be launching those tasks in a new goroutine before you sleep? If your scheduler is already looping forever why not just call the task synchronously and then sleep? </p></pre>neb636: <pre><p>Thank you very much! Will try those suggestions.</p></pre>GoTheFuckToBed: <pre><p>just some thoughts. </p> <ul> <li>Check the HTTP status instead error. </li> <li>You have a race on &#34;sites&#34;, when one go routines access it and one writes it at the same time it can crash. </li> <li>dont mix the http error check with your &#34;site is up&#34; logic</li> </ul></pre>kromem: <pre><blockquote> <p>The client must close the response body when finished with it</p> </blockquote> <p>From the very beginning of the overview <a href="https://golang.org/pkg/net/http/" rel="nofollow">here</a></p> <p>The documentation for the standard library is great, and if you&#39;re having issues, definitely read through the stuff you&#39;re using carefully. You&#39;ll learn all sorts of neat and powerful tools at your disposal you didn&#39;t even realize, and tips to avoid issues like this.</p> <p>Also, for future reference, the issue described of &#34;stops working after a while&#34; just screams of resource exhaustion. I had a strong suspicion even before looking at your code this was what was going on.</p> <p>Go is a bit more hands on than scripting languages. The garbage collector does a lot to reduce the things you need to worry about, but for I/O you need to tell the program when you are done using something. In this case, the http client probably has a number of allocated TCP ports available for connections, and until you release the port by closing the response, that port is considered to be in use. After all allocated ports are in use, the client stops sending requests, waiting for ports to close for new connections to use.</p> <p>TL;DR: Don&#39;t have the http.Post return nothing for the response. Have it return the response, then close it. Your problem will be fixed. </p></pre>neb636: <pre><p>Awesome thanks!</p></pre>itsmontoya: <pre><p>Did you add printlns to see where it&#39;s hanging?</p></pre>

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

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