<p>I'm starting become more comfortable with some of the deeper concepts in Go but I'm a bit confused on how to properly use the <code>context</code> package. I think what really throws me is how to properly use it with the <code>net/http</code> stuff that was introduced in 1.8. Seems like this should be simple so I'm hoping that I'm just over analyzing it.</p>
<hr/>**评论:**<br/><br/>steffen25: <pre><p>Hey! I know Francesc Campoy made 2 videos about the context package. I haven't watched them myself they are on my todo list :) but I have watched a lot of his other videos that I can recommend so hopefully this can help you out.</p>
<p>Video 1: <a href="https://www.youtube.com/watch?v=LSzR0VEraWw">https://www.youtube.com/watch?v=LSzR0VEraWw</a></p>
<p>Video 2: <a href="https://www.youtube.com/watch?v=8M90t0KvEDY">https://www.youtube.com/watch?v=8M90t0KvEDY</a> </p></pre>Destructicorn: <pre><p>Nice I'll check them out thanks</p></pre>trailmixed: <pre><p>Came here to recommend Francesc Campoy's videos... +1!</p></pre>adelowo: <pre><p>Same here</p></pre>albertorestifo: <pre><p>I've watched them and they are the resource that best made me understand context.</p>
<p>Highly recommend.</p></pre>januzx: <pre><p>Since you mentioned net/ http, I'll give you a related example. Think of a scenario where you have a HTTP service that needs to perform a bunch of expensive operations whenever a new request comes through. For example, you might start a sync/errgroup to issue a bunch of requests in parallel to some backend microservices. Since some of those backend services could be congested, you might even issue some duplicate requests and take the response that comes back first. Then, when all the results come through, you might start an expensive computation to combine those results before returning a response back to the user. Now, what happens if the user drops out whilst you're halfway through all this <strong>expensive</strong> work. Ideally you want to stop everything as soon as you realise that the work is no longer necessary. Contexts allow you to achieve this very easily. The context exposed by net/http request gets cancelled if the request times out (you can configure this at server creation time) or if it detects that the remote connection was killed. If you propagate the context through your request processing code paths, you can easily detect when it has been cancelled (via ctx.Done()) and return early without doing the remaining work. Of course, you can add your own timeouts and explicit cancellation hooks as well by wrapping the original context using convenience methods provided by the context package.</p>
<p>Hope that made sense. </p></pre>Destructicorn: <pre><p>That does make sense. Nice to have a scenario to go along with the documentation.</p></pre>akavel: <pre><p>Very good explanation. Also it may (or may not?) be helpful to realize that the cancellation is "soft" — kinda more like a TERM signal than KILL signal in Linux, or more like a suggestion than an order in real life. So, you need to check it (the ctx.Done) explicitly from time to time; you may sometimes unnecessarily do some work after cancellation before you check/detect it. But it's ok, it's fully at your discretion to check it, it just allows you to sometimes free/stop some resources/work earlier than you could without the Context.</p></pre>docsavage: <pre><p><a href="https://blog.golang.org/context">https://blog.golang.org/context</a></p></pre>phxIsFarkinHot: <pre><p>This is an excellent article. I've read it a couple times, but today I spent the time to fully grok it. </p>
<p>Spend some time looking at the full code sample: <a href="https://blog.golang.org/context/google/google.go" rel="nofollow">https://blog.golang.org/context/google/google.go</a> </p>
<p>More specifically the httpDo method and how it works with the f function parameter. This example covers just about everything you need to know to work with go: channels, context, first class functions, select with channels, closures, and anonymous goroutines. </p></pre>Tacticus: <pre><p>Jaana Dogan (@rakyll on twitter/github) has a great blog post <a href="https://rakyll.org/leakingctx/" rel="nofollow">https://rakyll.org/leakingctx/</a> that really helped me grok what contexts are for. (Seriously just spend an afternoon reading her go unwritten parts blog. the profiling thing just put up is awesomesauce)</p>
<p>Also this talk <a href="https://www.youtube.com/watch?v=jUIh14avdDY" rel="nofollow">https://www.youtube.com/watch?v=jUIh14avdDY</a> about adding context to nats by Waldemar Quevedo (@wallyqs on the things) is great.</p></pre>Mentioned_Videos: <pre><p>Videos in this thread:</p>
<p><a href="http://subtletv.com/_r6lmfbm?feature=playlist&nline=1" rel="nofollow">Watch Playlist ▶</a></p>
<table><thead>
<tr>
<th>VIDEO</th>
<th>COMMENT</th>
</tr>
</thead><tbody>
<tr>
<td>(1) <a href="http://www.youtube.com/watch?v=LSzR0VEraWw" rel="nofollow">http://www.youtube.com/watch?v=LSzR0VEraWw</a> (2) <a href="http://www.youtube.com/watch?v=8M90t0KvEDY" rel="nofollow">http://www.youtube.com/watch?v=8M90t0KvEDY</a></td>
<td><a href="https://www.reddit.com/r/golang/comments/6lmfbm/_/djuvnr1?context=10#djuvnr1" rel="nofollow">+12</a> - Hey! I know Francesc Campoy made 2 videos about the context package. I haven't watched them myself they are on my todo list :) but I have watched a lot of his other videos that I can recommend so hopefully this can help you out. Video 1: Video 2:</td>
</tr>
<tr>
<td><a href="http://www.youtube.com/watch?v=jUIh14avdDY" rel="nofollow">http://www.youtube.com/watch?v=jUIh14avdDY</a></td>
<td><a href="https://www.reddit.com/r/golang/comments/6lmfbm/_/djvkuik?context=10#djvkuik" rel="nofollow">+1</a> - Jaana Dogan (@rakyll on twitter/github) has a great blog post that really helped me grok what contexts are for. (Seriously just spend an afternoon reading her go unwritten parts blog. the profiling thing just put up is awesomesauce) Also this talk...</td>
</tr>
</tbody></table>
<p>I'm a bot working hard to help Redditors find related videos to watch. I'll keep this updated as long as I can.</p>
<hr/>
<p><a href="http://subtletv.com/_r6lmfbm?feature=playlist&ftrlnk=1" rel="nofollow">Play All</a> | <a href="https://np.reddit.com/r/SubtleTV/wiki/mentioned_videos" rel="nofollow">Info</a> | Get me on <a href="https://chrome.google.com/webstore/detail/mentioned-videos-for-redd/fiimkmdalmgffhibfdjnhljpnigcmohf" rel="nofollow">Chrome</a> / <a href="https://addons.mozilla.org/en-US/firefox/addon/mentioned-videos-for-reddit" rel="nofollow">Firefox</a></p></pre>jimijiim: <pre><p>You need to understand why it was created at first place, and nobody's doing it on this thread.</p>
<p>So back in the days, people were writing go web apps, using global values to hold things like sessions. These people wrote code full of memory leaks because it was hard to do any clean up of values that depended on requests. </p>
<p>With context a request can now signal a global value to do some clean up when the request value is not needed anymore, since the request context has a channel for that purpose. So now global objects holding request objects can clean any request reference easily.</p></pre>gentleman_tech: <pre><p>I'm not 100% sure I've been using it right, but I made some middleware that adds session and user info to the context on a request, so downstream handlers have that available automatically.
It works fine, but it does feel like I'm using a sledgehammer to crack a nut.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传