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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
|
<!DOCTYPE html>
<html>
<head>
<title>worker.rb</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, target-densitydpi=160dpi, initial-scale=1.0; maximum-scale=1.0; user-scalable=0;">
<link rel="stylesheet" media="all" href="docco.css" />
</head>
<body>
<div id="container">
<div id="background"></div>
<ul id="jump_to">
<li>
<a class="large" href="javascript:void(0);">Jump To …</a>
<a class="small" href="javascript:void(0);">+</a>
<div id="jump_wrapper">
<div id="jump_page">
<a class="source" href="configurator.html">
configurator.rb
</a>
<a class="source" href="logging.html">
logging.rb
</a>
<a class="source" href="lookout.html">
lookout.rb
</a>
<a class="source" href="master.html">
master.rb
</a>
<a class="source" href="server.html">
server.rb
</a>
<a class="source" href="worker.html">
worker.rb
</a>
</div>
</li>
</ul>
<ul class="sections">
<li id="title">
<div class="annotation">
<h1>worker.rb</h1>
</div>
</li>
<li id="section-1">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-1">¶</a>
</div>
</div>
<div class="content"><div class='highlight'><pre><span class="keyword">require</span> <span class="string">'time'</span>
<span class="keyword">require</span> <span class="string">'rack/utils'</span></pre></div></div>
</li>
<li id="section-2">
<div class="annotation">
<div class="pilwrap for-h1">
<a class="pilcrow" href="#section-2">¶</a>
</div>
<h1>Worker</h1>
</div>
</li>
<li id="section-3">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-3">¶</a>
</div>
</div>
<div class="content"><div class='highlight'><pre><span class="class"><span class="keyword">module</span> <span class="title">Spyglass</span></span>
<span class="class"><span class="keyword">class</span> <span class="title">Worker</span></span>
<span class="keyword">include</span> <span class="constant">Logging</span>
<span class="function"><span class="keyword">def</span> <span class="title">initialize</span><span class="params">(socket, app, writable_pipe, connection = <span class="keyword">nil</span>)</span></span>
<span class="variable">@socket</span>, <span class="variable">@app</span>, <span class="variable">@writable_pipe</span> = socket, app, writable_pipe
<span class="variable">@parser</span> = <span class="constant">Spyglass::HttpParser</span>.new
handle_connection(connection) <span class="keyword">if</span> connection
<span class="keyword">end</span>
<span class="function"><span class="keyword">def</span> <span class="title">start</span></span>
trap_signals
loop <span class="keyword">do</span>
handle_connection <span class="variable">@socket</span>.accept
<span class="keyword">end</span>
<span class="keyword">end</span>
<span class="function"><span class="keyword">def</span> <span class="title">handle_connection</span><span class="params">(conn)</span></span>
verbose <span class="string">"Received connection"</span></pre></div></div>
</li>
<li id="section-4">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-4">¶</a>
</div>
<p>This notifies our Master that we have received a connection, expiring
it's <code>IO.select</code> and preventing it from timing out.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="variable">@writable_pipe</span>.write_nonblock(<span class="string">'.'</span>)</pre></div></div>
</li>
<li id="section-5">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-5">¶</a>
</div>
<p>This clears any state that the http parser has lying around
from the last connection that was handled.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="variable">@parser</span>.reset</pre></div></div>
</li>
<li id="section-6">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-6">¶</a>
</div>
<p>The Rack spec requires that 'rack.input' be encoded as ASCII-8BIT.</p>
</div>
<div class="content"><div class='highlight'><pre> empty_body = <span class="string">''</span>
empty_body.encode!(<span class="constant">Encoding::ASCII_8BIT</span>) <span class="keyword">if</span> empty_body.respond_to?(<span class="symbol">:encode!</span>)</pre></div></div>
</li>
<li id="section-7">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-7">¶</a>
</div>
<p>The Rack spec requires that the env contain certain keys before being
passed to the app. These are the keys that aren't provided by each
incoming request, server-specific stuff.</p>
</div>
<div class="content"><div class='highlight'><pre> env = {
<span class="string">'rack.input'</span> => <span class="constant">StringIO</span>.new(empty_body),
<span class="string">'rack.multithread'</span> => <span class="keyword">false</span>,
<span class="string">'rack.multiprocess'</span> => <span class="keyword">true</span>,
<span class="string">'rack.run_once'</span> => <span class="keyword">false</span>,
<span class="string">'rack.errors'</span> => <span class="constant">STDERR</span>,
<span class="string">'rack.version'</span> => [<span class="number">1</span>, <span class="number">0</span>]
}</pre></div></div>
</li>
<li id="section-8">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-8">¶</a>
</div>
<p>This reads data in from the client connection. We'll read up to
10000 bytes at the moment.</p>
</div>
<div class="content"><div class='highlight'><pre> data = conn.readpartial(<span class="number">10000</span>)</pre></div></div>
</li>
<li id="section-9">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-9">¶</a>
</div>
<p>Here we pass the data and the env into the http parser. It parses
the raw http request data and updates the env with all of the data
it can withdraw.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="variable">@parser</span>.execute(env, data, <span class="number">0</span>)</pre></div></div>
</li>
<li id="section-10">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-10">¶</a>
</div>
<p>Call the Rack app, goes all the way down the rabbit hole and back again.</p>
</div>
<div class="content"><div class='highlight'><pre> status, headers, body = <span class="variable">@app</span>.call(env)</pre></div></div>
</li>
<li id="section-11">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-11">¶</a>
</div>
<p>These are the default headers we always include in a response. We
only speak HTTP 1.1 and we always close the client connection. At
the monment keepalive is not supported.</p>
</div>
<div class="content"><div class='highlight'><pre> head = <span class="string">"HTTP/1.1 <span class="subst">#{status}</span>\r\n"</span> \
<span class="string">"Date: <span class="subst">#{<span class="constant">Time</span>.now.httpdate}</span>\r\n"</span> \
<span class="string">"Status: <span class="subst">#{<span class="constant">Rack::Utils::HTTP_STATUS_CODES</span>[status]}</span>\r\n"</span> \
<span class="string">"Connection: close\r\n"</span>
headers.each <span class="keyword">do</span> |k,v|
head << <span class="string">"<span class="subst">#{k}</span>: <span class="subst">#{v}</span>\r\n"</span>
<span class="keyword">end</span>
conn.write <span class="string">"<span class="subst">#{head}</span>\r\n"</span>
body.each { |chunk| conn.write chunk }
body.close <span class="keyword">if</span> body.respond_to?(<span class="symbol">:close</span>)</pre></div></div>
</li>
<li id="section-12">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-12">¶</a>
</div>
<p>Since keepalive is not supported we can close the client connection
immediately after writing the body.</p>
</div>
<div class="content"><div class='highlight'><pre> conn.close
verbose <span class="string">"Closed connection"</span>
<span class="keyword">end</span>
<span class="function"><span class="keyword">def</span> <span class="title">trap_signals</span></span>
trap(<span class="symbol">:QUIT</span>) <span class="keyword">do</span>
out <span class="string">"Received QUIT"</span>
exit
<span class="keyword">end</span>
<span class="keyword">end</span>
<span class="keyword">end</span>
<span class="keyword">end</span></pre></div></div>
</li>
</ul>
</div>
</body>
</html>
|