forked from mmcgrana/gobyexample
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext
209 lines (167 loc) · 9.15 KB
/
context
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Go by Example: Context</title>
<link rel=stylesheet href="site.css">
</head>
<script>
onkeydown = (e) => {
if (e.key == "ArrowLeft") {
window.location.href = 'http-servers';
}
if (e.key == "ArrowRight") {
window.location.href = 'spawning-processes';
}
}
</script>
<body>
<div class="example" id="context">
<h2><a href="./">Go by Example</a>: Context</h2>
<table>
<tr>
<td class="docs">
<p>In the previous example we looked at setting up a simple
<a href="http-servers">HTTP server</a>. HTTP servers are useful for
demonstrating the usage of <code>context.Context</code> for
controlling cancellation. A <code>Context</code> carries deadlines,
cancellation signals, and other request-scoped values
across API boundaries and goroutines.</p>
</td>
<td class="code leading">
<a href="http://play.golang.org/p/0_bu1o8rIBO"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
<pre class="chroma">
<span class="kn">package</span> <span class="nx">main</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<pre class="chroma"><span class="kn">import</span> <span class="p">(</span>
<span class="s">"fmt"</span>
<span class="s">"net/http"</span>
<span class="s">"time"</span>
<span class="p">)</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<pre class="chroma"><span class="kd">func</span> <span class="nf">hello</span><span class="p">(</span><span class="nx">w</span> <span class="nx">http</span><span class="p">.</span><span class="nx">ResponseWriter</span><span class="p">,</span> <span class="nx">req</span> <span class="o">*</span><span class="nx">http</span><span class="p">.</span><span class="nx">Request</span><span class="p">)</span> <span class="p">{</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>A <code>context.Context</code> is created for each request by
the <code>net/http</code> machinery, and is available with
the <code>Context()</code> method.</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">ctx</span> <span class="o">:=</span> <span class="nx">req</span><span class="p">.</span><span class="nf">Context</span><span class="p">()</span>
<span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="s">"server: hello handler started"</span><span class="p">)</span>
<span class="k">defer</span> <span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="s">"server: hello handler ended"</span><span class="p">)</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>Wait for a few seconds before sending a reply to the
client. This could simulate some work the server is
doing. While working, keep an eye on the context’s
<code>Done()</code> channel for a signal that we should cancel
the work and return as soon as possible.</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="k">select</span> <span class="p">{</span>
<span class="k">case</span> <span class="o"><-</span><span class="nx">time</span><span class="p">.</span><span class="nf">After</span><span class="p">(</span><span class="mi">10</span> <span class="o">*</span> <span class="nx">time</span><span class="p">.</span><span class="nx">Second</span><span class="p">):</span>
<span class="nx">fmt</span><span class="p">.</span><span class="nf">Fprintf</span><span class="p">(</span><span class="nx">w</span><span class="p">,</span> <span class="s">"hello\n"</span><span class="p">)</span>
<span class="k">case</span> <span class="o"><-</span><span class="nx">ctx</span><span class="p">.</span><span class="nf">Done</span><span class="p">():</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>The context’s <code>Err()</code> method returns an error
that explains why the <code>Done()</code> channel was
closed.</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">err</span> <span class="o">:=</span> <span class="nx">ctx</span><span class="p">.</span><span class="nf">Err</span><span class="p">()</span>
<span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="s">"server:"</span><span class="p">,</span> <span class="nx">err</span><span class="p">)</span>
<span class="nx">internalError</span> <span class="o">:=</span> <span class="nx">http</span><span class="p">.</span><span class="nx">StatusInternalServerError</span>
<span class="nx">http</span><span class="p">.</span><span class="nf">Error</span><span class="p">(</span><span class="nx">w</span><span class="p">,</span> <span class="nx">err</span><span class="p">.</span><span class="nf">Error</span><span class="p">(),</span> <span class="nx">internalError</span><span class="p">)</span>
<span class="p">}</span>
<span class="p">}</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<pre class="chroma"><span class="kd">func</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>As before, we register our handler on the “/hello”
route, and start serving.</p>
</td>
<td class="code">
<pre class="chroma">
<span class="nx">http</span><span class="p">.</span><span class="nf">HandleFunc</span><span class="p">(</span><span class="s">"/hello"</span><span class="p">,</span> <span class="nx">hello</span><span class="p">)</span>
<span class="nx">http</span><span class="p">.</span><span class="nf">ListenAndServe</span><span class="p">(</span><span class="s">":8090"</span><span class="p">,</span> <span class="kc">nil</span><span class="p">)</span>
<span class="p">}</span>
</pre>
</td>
</tr>
</table>
<table>
<tr>
<td class="docs">
<p>Run the server in the background.</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="gp">$</span> go run context-in-http-servers.go &</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>Simulate a client request to <code>/hello</code>, hitting
Ctrl+C shortly after starting to signal
cancellation.</p>
</td>
<td class="code">
<pre class="chroma">
<span class="gp">$</span> curl localhost:8090/hello
<span class="go">server: hello handler started
</span><span class="go">^C
</span><span class="go">server: context canceled
</span><span class="go">server: hello handler ended</span></pre>
</td>
</tr>
</table>
<p class="next">
Next example: <a href="spawning-processes">Spawning Processes</a>.
</p>
<p class="footer">
by <a href="https://markmcgranaghan.com">Mark McGranaghan</a> and <a href="https://eli.thegreenplace.net">Eli Bendersky</a> | <a href="https://github.com/mmcgrana/gobyexample">source</a> | <a href="https://github.com/mmcgrana/gobyexample#license">license</a>
</p>
</div>
<script>
var codeLines = [];
codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"net/http\"\u000A \"time\"\u000A)\u000A');codeLines.push('func hello(w http.ResponseWriter, req *http.Request) {\u000A');codeLines.push(' ctx :\u003D req.Context()\u000A fmt.Println(\"server: hello handler started\")\u000A defer fmt.Println(\"server: hello handler ended\")\u000A');codeLines.push(' select {\u000A case \u003C-time.After(10 * time.Second):\u000A fmt.Fprintf(w, \"hello\\n\")\u000A case \u003C-ctx.Done():\u000A');codeLines.push(' err :\u003D ctx.Err()\u000A fmt.Println(\"server:\", err)\u000A internalError :\u003D http.StatusInternalServerError\u000A http.Error(w, err.Error(), internalError)\u000A }\u000A}\u000A');codeLines.push('func main() {\u000A');codeLines.push(' http.HandleFunc(\"/hello\", hello)\u000A http.ListenAndServe(\":8090\", nil)\u000A}\u000A');codeLines.push('');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>
</html>