summaryrefslogblamecommitdiffstats
path: root/talks/cowboy/all.lt
blob: 61d98ac9778cfe4b088dda910d2de1ecf0b4eb1b (plain) (tree)
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664























































































































































































































































































































































































































































































































































































































































































                                                                                                             
@Include { myslides }

@OverheadTransparencies
	@Title {
A Cowboy quest for a modern web
}
	@RunningTitle { cowboy }
	@Author { Lo�c Hoguin }
	@Institution {
Nine Nines
Dev:Extend
}
	@DateLine { Erlang User Conference 2011 }
	@InitialLanguage { English }
	@PageOrientation { Landscape }
//

@Overhead
	@Title { Why Cowboy? }
	@Begin

@BulletList
@ListItem { Because guns are better than arrows }
@ListItem { It's all about the hat }
@EndList

@End @Overhead

@Overhead
	@Title { History }
	@Begin

@BulletList
@ListItem { First commit early March 2011 }
@ListItem { Mentioned on the Github blog two weeks later }
@ListItem { First beta early September 2011 }
@ListItem { First talk early November 2011 }
@EndList

@End @Overhead

@Overhead
	@Title { Users and contributors }
	@Begin

@BulletList
@ListItem { The etorrent project for its acceptor pool and web interface }
@ListItem { The sockjs-erlang project for a websocket and HTTP server }
@ListItem { Smarkets, Nivertech and other companies }
@ListItem { The "#erlounge" IRC folks }
@EndList

@End @Overhead

@Overhead
	@Title { Cowboy's listeners }
	@Begin

@BulletList
@ListItem { Cowboy isn't just an HTTP server }
@ListItem { Cowboy allows you to start your own listener
	@BulletList
	@ListItem { Essentially a transport and protocol-agnostic acceptor pool }
	@ListItem { An acceptor pool is a pool of processes accepting connections }
	@EndList
}
@ListItem { You can have as many listeners running side by side as you want }
@ListItem { Even listeners completely unrelated to the HTTP protocol }
@EndList

@End @Overhead

@Overhead
	@Title { Transport handlers }
	@Begin

@BulletList
@ListItem { Tiny wrappers around transport-related code
	@BulletList
	@ListItem { listen, accept, recv, send, setopts... }
	@EndList
}
@ListItem { Works with any reliable transport
	@BulletList
	@ListItem { TCP, SSL }
	@ListItem { Theorically also the UDP-based ENet }
	@EndList
}
@EndList

@End @Overhead

@Overhead
	@Title { Protocol handlers }
	@Begin

@BulletList
@ListItem { Contains the protocol implementation }
@ListItem { Usually takes the form of a state machine }
@ListItem { gen_fsm can of course be used }
@ListItem { Only a "start_link/4" function is required }
@EndList

@End @Overhead

@Overhead
	@Title { Acceptor loop }
	@Begin

@BulletList
@ListItem { Wait for a connection }
@ListItem { Accept the connection
	@BulletList
	@ListItem { Start a request process }
	@ListItem { Give the socket to that new process }
	@ListItem { Inform the new process everything's ready }
	@EndList
}
@ListItem { Check for max "#" of connections
	@BulletList
	@ListItem { Wait if this max is reached }
	@EndList
}
@ListItem { Repeat }
@EndList

@End @Overhead

@Overhead
	@Title { A pool of many acceptors }
	@Begin

@BulletList
@ListItem { Idea taken from Mochiweb }
@ListItem { Having more than one acceptor process speeds things up }
@ListItem { Erlang processes are cheap }
@ListItem { Why not use 100 processes to accept connections? }
@EndList

@End @Overhead

@Overhead
	@Title { Supervision }
	@Begin

@BulletList
@ListItem { All processes started by a listener are supervised }
@ListItem { You don't need to worry about supervision! }
@ListItem { We're working on release upgrades }
@EndList

@End @Overhead

@Overhead
	@Title { Connection pools }
	@Begin

@BulletList
@ListItem { There are different kinds of connections }
@ListItem { Short-lived "request/response" should have a small max "#" }
@ListItem { Long-lived idle connections can have a much larger max }
@ListItem { Pools allow separating those connection types into two groups
	@BulletList
	@ListItem { Or more }
	@EndList
}
@ListItem { You can add, move or delete connections from pools }
@ListItem { By default all connections are added to the pool named 'default' }
@EndList

@End @Overhead

@Overhead
	@Title { Cowboy's HTTP server }
	@Begin

@BulletList
@ListItem { Because all applications have their own HTTP interface }
@EndList

@End @Overhead

@Overhead
	@Title { Initial design ideas }
	@Begin

@BulletList
@ListItem { gen_fsm, lists }
@ListItem { Normal process, lists, with "[{active, once}]" }
@ListItem { Normal process, binary, with "[{active, once}]" }
@ListItem { Normal process, binary with "[{active, false}]" }
@ListItem { Normal process, binary, calling "erlang:decode_packet/3" directly }
@ListItem { We did want binary from the start but it required more custom code }
@EndList

@End @Overhead

@Overhead
	@Title { Dispatch rules }
	@Begin

@BulletList
@ListItem { Idea taken from Webmachine }
@ListItem { Matching hostname and path to HTTP handlers }
@ListItem { Partial matching allows binding "hostname/path" parts to variables }
@ListItem { If we had the rule "[<<\"users\">>, id, <<\"pics\">>]"
	@BulletList
	@ListItem { The path "/users/42/pics" would match }
	@ListItem { "{id, 42}" would be added to the bindings }
	@EndList
}
@ListItem { Entirely optional }
@EndList

@End @Overhead

@Overhead
	@Title { HTTP handlers }
	@Begin

@CD @F @Box paint { lightyellow } { @Verbatim {
-module(my_handler).
-export([init/3, handle/2, terminate/2]).

init(_TransportType, Req, _Opts) ->
    {ok, Req, undefined_state}.

handle(Req, State) ->
    {ok, Req2} = cowboy_http_req:reply(200, [], <<"Hi EUC!">>,
        Req),
    {ok, Req2, State}.

terminate(_Req, _State) ->
    ok.
} }

@End @Overhead

@Overhead
	@Title { HTTP request object }
	@Begin

@BulletList
@ListItem { Retrieve the method, HTTP version, peer IP and port }
@ListItem { Retrieve the requested hostname and path }
@ListItem { Retrieve headers, query string values, bindings, cookies }
@ListItem { Semantically parse headers }
@ListItem { Read the body of the request }
@ListItem { Send a simple or a chunked reply }
@EndList

@End @Overhead

@Overhead
	@Title { HTTP handler loops }
	@Begin

@BulletList
@ListItem { Useful for long-polling or Event Source }
@ListItem { Receive messages from other processes and send back data }
@ListItem { Hibernate and timeouts support }
@EndList

@End @Overhead

@Overhead
	@Title { HTTP handlers for long-polling }
	@Begin

@CD @F @Box paint { lightyellow } { @Verbatim {
-module(my_loop_handler).
-export([init/3, info/3, terminate/2]).

init(_TransportType, Req, _Opts) ->
    my_session_server:hello(),
    {loop, Req, undefined_state, 60000, hibernate}.

info({my_session_server, Message}, Req, State) ->
    {ok, Req2} = cowboy_http_req:reply(200, [], Message, Req),
    {ok, Req2, State};
info(_Any, Req, State) ->
    {loop, Req, State, hibernate}.

terminate(_Req, _State) -> ok = my_session_server:bye().
} }

@End @Overhead

@Overhead
	@Title { Websocket support }
	@Begin

@BulletList
@ListItem { Cowboy supports all Websocket protocol versions in use in web browsers }
@ListItem { New versions get added as soon as browsers start implementing them }
@ListItem { Cowboy's websocket interface should be future-proof }
@ListItem { Websocket connections are on a different pool than normal HTTP }
@EndList

@End @Overhead

@Overhead
	@Title { Websocket handlers }
	@Begin

@CD @F @Box paint { lightyellow } { @Verbatim {
-module(my_ws_handler).
-export([init/3, websocket_init/3, websocket_handle/3,
    websocket_info/3, websocket_terminate/3]).

init(_TransportType, Req, _Opts) ->
    {upgrade, protocol, cowboy_http_websocket}.

websocket_init(_TransportType, Req, _Opts) ->
    Req2 = cowboy_http_req:compact(Req),
    {ok, Req2, undefined_state, 60000, hibernate}.

%% ...
} }
@End @Overhead

@Overhead
	@Title { Websocket handlers continued }
	@Begin

@CD @F @Box paint { lightyellow } { @Verbatim {
%% ...

websocket_handle({text, Data}, Req, State) ->
    {reply, {text, Data}, Req, State, hibernate};
websocket_handle(_Frame, Req, State) ->
    {ok, Req, State, hibernate}.

websocket_info(_Info, Req, State) ->
    {ok, Req, State, hibernate}.

websocket_terminate(_Reason, _Req, _State) ->
    ok.
} }

@End @Overhead

@Overhead
	@Title { REST handlers }
	@Begin

@BulletList
@ListItem { Use Webmachine's decision flow diagram }
@ListItem { Not a rewrite, a new implementation }
@ListItem { Clean, readable code not requiring the diagram to be understood }
@EndList

@End @Overhead

@Overhead
	@Title { Cowboy is clean code }
	@Begin

@BulletList
@ListItem { Easy to understand, easy to debug }
@ListItem { No process dictionaries or other side effects }
@ListItem { No parameterized modules }
@ListItem { Use only documented "Erlang/OTP" features }
@EndList

@End @Overhead

@Overhead
	@Title { Cowboy and OTP }
	@Begin

@BulletList
@ListItem { All Cowboy processes are supervised }
@ListItem { OTP upgrades work (though improvements are coming) }
@ListItem { HTTP handlers are inspired by gen_servers
	@BulletList
	@ListItem { gen_server: client > api call > server }
	@ListItem { http_handler: http client > http request > http handler }
	@EndList
}
@EndList

@End @Overhead

@Overhead
	@Title { Cowboy's performance }
	@Begin

@BulletList
@ListItem { 500 000 concurrent websocket connections and beyond }
@EndList

@End @Overhead

@Overhead
	@Title { Does performance matter? }
	@Begin

@BulletList
@ListItem { It depends on your application }
@ListItem { It probably doesn't matter for 99% of the applications }
@ListItem { In HTTP, response latency is very important }
@ListItem { When handling many concurrent connections, memory usage matters }
@EndList

@End @Overhead

@Overhead
	@Title { 1 process per connection }
	@Begin

@BulletList
@ListItem { Other Erlang HTTP servers use 2 processes per connection }
@ListItem { Cowboy uses only 1 }
@ListItem { Saves a significant amount of memory }
@ListItem { Reduces latency thanks to reduced message passing }
@EndList

@End @Overhead

@Overhead
	@Title { Low memory usage: binary }
	@Begin

@BulletList
@ListItem { Big binaries are ref counted }
@ListItem { Small binaries are still smaller than lists }
@ListItem { Sub-binary optimizations helps reduce copying }
@EndList

@End @Overhead

@Overhead
	@Title { Low memory usage: "cowboy_http_req:compact/1" }
	@Begin

@BulletList
@ListItem { Removes everything unwanted from the Req object }
@ListItem { Lowers memory usage for long-running processes }
@ListItem { Works especially well with process hibernation }
@EndList

@End @Overhead

@Overhead
	@Title { Requests per seconds }
	@Begin

@BulletList
@ListItem { Not a good indicator of performance }
@ListItem { But a good indicator of a design's simplicity }
@ListItem { If "requests/s" lowers significantly between two commits, you've messed up }
@EndList

@End @Overhead

@Overhead
	@Title { The Horse project }
	@Begin

@BulletList
@ListItem { Continuous performance testing of the Cowboy project }
@ListItem { Will measure latency, CPU usage, memory usage... }
@ListItem { Will produce nice graphs to quickly notice drops and fix them }
@ListItem { Release expected for Q1 2012 }
@EndList

@End @Overhead

@Overhead
	@Title { Cowboy's related projects }
	@Begin

@BulletList
@ListItem { Because the core project should stay lightweight }
@EndList

@End @Overhead

@Overhead
	@Title { Bullet handler }
	@Begin

@BulletList
@ListItem { A "Socket.IO/SockJS" alternative }
@ListItem { Sets up an always connected streaming interface between JS and Erlang }
@ListItem { Uses Websocket by default, other methods when not available }
@ListItem { A single interface both client and server-side }
@ListItem { More work is needed on the JS side }
@EndList

@End @Overhead

@Overhead
	@Title { cowboy_static handler }
	@Begin

@BulletList
@ListItem { A static file handler by Magnus Klaar }
@ListItem { Using the sendfile code originally from Yaws }
@ListItem { Will be using the future REST support }
@ListItem { Available as a separate project }
@EndList

@End @Overhead

@Overhead
	@Title { Bigwig: Spawnfest great winner }
	@Begin

@BulletList
@ListItem { Spawnfest is an annual Erlang programming contest }
@ListItem { First edition took place in June 2011 }
@ListItem { They produced an awesome webtool replacement with many more features }
@ListItem { Got the IRCCloud guys interested in Cowboy }
@EndList

@End @Overhead

@Overhead
	@Title { Farwest: a new kind of web development stack }
	@Begin

@BulletList
@ListItem { Default administration panel
	@BulletList
	@ListItem { Setup your data }
	@ListItem { Setup your views }
	@ListItem { Write simple rules to route your data to your views }
	@ListItem { Write simple rules to create forms and save the data }
	@ListItem { Edit and reload any client and server-side code live }
	@ListItem { Edit and reload the dispatch list live }
	@ListItem { Git integration }
	@EndList
}
@ListItem { NoSQL backend, nice development API and many plugins }
@ListItem { Beta expected for Q1 2012 }
@EndList

@End @Overhead

@Overhead
	@Title { Cowboy's future }
	@Begin

@BulletList
@ListItem { Because they aren't just figures of the past }
@EndList

@End @Overhead

@Overhead
	@Title { Listener upgrades }
	@Begin

@BulletList
@ListItem { We want to update the dispatch list on-the-fly }
@ListItem { We want to update most transport or protocol options }
@ListItem { Also allow adding or removing acceptors }
@EndList

@End @Overhead

@Overhead
	@Title { Improved dispatcher }
	@Begin

@BulletList
@ListItem { API will be improved }
@ListItem { Allow giving tokens as lists and not just binaries }
@ListItem { Hostname and path hierarchy will be added }
@ListItem { Might eventually switch to matchspecs for better performance }
@EndList

@End @Overhead

@Overhead
	@Title { gen_event for request tracking and monitoring }
	@Begin

@BulletList
@ListItem { Ad-hoc error and access logging }
@ListItem { Allows writing custom event handlers }
@ListItem { No cost when no handlers are defined }
@EndList

@End @Overhead

@Overhead
	@Title { Multipart support }
	@Begin

@BulletList
@ListItem { File upload support }
@ListItem { Convenience function
	@BulletList
	@ListItem { Save all files to temporary locations }
	@ListItem { Return the paths along with the additional POST parameters }
	@EndList
}
@ListItem { Streaming
	@BulletList
	@ListItem { Stream each parts individually }
	@ListItem { Stream each parts' payload }
	@EndList
}
@EndList

@End @Overhead

@Overhead
	@Title { Aiming for full "HTTP/1.1" compliance }
	@Begin

@BulletList
@ListItem { Support for all "HTTP/1.1" features }
@ListItem { Correct "HTTP/1.0" clients handling }
@ListItem { Semantic parsing of all header values }
@ListItem { Gzip compression enabled by default for replies }
@EndList

@End @Overhead

@Overhead
	@Title { Up-to-date Websocket support }
	@Begin

@BulletList
@ListItem { We are actively monitoring the Websocket draft changes }
@ListItem { Updated implementation usually supported within 7 days }
@ListItem { Interface shouldn't be changing anymore }
@EndList

@End @Overhead

@Overhead
	@Title { Links }
	@Begin

@BulletList
@ListItem { cowboy: "https://github.com/extend/cowboy" @ExternalLink { "https://github.com/extend/cowboy" } }
@ListItem { bullet: "https://github.com/extend/bullet" @ExternalLink { "https://github.com/extend/bullet" } }
@EndList

@DP

@BulletList
@ListItem { essen on "#erlounge" and "#erlang" on Freenode }
@ListItem { "@lhoguin" on Twitter }
@ListItem { Lo�c Hoguin on G+ }
@ListItem { "[email protected]" }
@EndList

@End @Overhead

@Overhead
	@Title { Questions? }
	@Begin

@BulletList
@ListItem { I won't shoot you for asking! }
@EndList

@End @Overhead