More thanSyntax
Patrick Huesler@phuesler
More thanSyntax
web apis with   erlanga ruby dev’s POV
snowboarding
Surfing
WIPEOUT
erlang
I’m an erlang    beginnerso don’t believe everything           I say
this is not an  erlangintroduction
erlangrefresher
functional language
runtimesystem
open telecom platform      OTP
whyerlang?
seriouslyconcurrent
with built inactors
because it isdistributed
faulttolerant
hotswaping
remoteshell
erlang atwooga
technicalchallenges
monthly active users20,543,500for Diamond Dash http://www.appdata.com/apps/facebook/127995567256931-diamond-dash (03/10/20...
daily active users3,871,133for Diamond Dashhttp://www.appdata.com/apps/facebook/127995567256931-diamond-dash (03/10/2012)
backend traffic up to 6,500 RPSfor Monster World
what does mean that for aDatabase?
read/write ratio?write heavy
differentarchitectures
no   databaseis faster than no database
stateful
Let’s use   S3all the way
http://www.slideshare.net/wooga/from-0-to-1000000-daily-users-with-erlang
http://www.slideshare.net/wooga/from-0-to-1000000-daily-users-with-erlang
processes arecheap  in erlang
erlangWeb APIs
rebar
$:rebar create-app appid=aloha
how to managedependencies like rubygems does?
rebar
$: rebar get-deps$: rebar compile$: rebar get-deps compile
1 % Compiler Options for rebar   2 {erl_opts, [   3     {src_dirs, ["src", "test"]},   4     debug_info   5 ]}.   6   7 % ...
Whatwebserver shall we use?
elli
1 elli:start_link([2                      {callback, aloha_api},3                      {port, 3000}4                 ])
how do I doRouting?
1   Path = [<<"foo">>, <<"bar">>], 2   HTTPMethod = "GET", 3   Request = {HTTPMethod, Path}, 4   RouteDefinitions = [{{"GE...
you’re doing itwrong!!!
usepatternmatching
1 handle(Req, _Args) ->2     Path = elli_request:path(Req),3     handle(Req#req.method, Path, Req).45 handle(GET,[<<"foo">...
does it supportmiddleware?
1 Config = [ 2            {mods, [ 3                     {aloha_ware, []}, 4                     {mahalo_ware, []}, 5     ...
eventsrequest_completerequest_throwrequest_exitrequest_errorrequest_parse_errorbad_requestclient_closedclient_timeoutelli_...
1 handle_event(request_complete, [ 2                                 Req, 3                                 ResponseCode, ...
awesomemonitoring
1 handle_event(request_throw, [ 2                                  Req, 3                                  Exception,Stack...
how aboutenvironments?
$: erl -pa deps/*/ebin ebin n   -config myconfig
1 [                Config file 2       {aloha, [ 3              {worker_port, 3333} 4            ] 5       }, 6 7      {la...
how aboutlogging?
1   error_logger:info_msg("alohan").2   % prints alohanok34   error_logger:warning_msg("freak set").5   % prints freak set...
sasl
=PROGRESS REPORT==== 8-Oct-2012::12:06:24 ===          supervisor: {local,sasl_safe_sup}             started: [{pid,<0.39....
how aboutunix style?
lager
1 [                Config file 2       {aloha, [ 3              {worker_port, 3333} 4            ] 5       }, 6 7      {la...
1 start(_StartType, _StartArgs) -> 2     ok = application:start(compiler), 3     ok = application:start(syntax_tools), 4  ...
1 dev_start(App) -> dev_start(App, temporary). 2 3 dev_start(App, Type) -> 4     case application:start(App, Type) of 5   ...
1   lager:debug("alohan").2   % prints alohanok34   lager:warning("freak set").5   % prints freak set ahead nok67   lager:...
how about  Unittesting?
eunit
nostacktracethe easy way
etest
1   -module(aloha_utils_test). 2   -compile(export_all). 3 4   % Include etests assertion macros. 5   -include_lib("etest/...
$: ./deps/etest/bin/etest-runner
How about  httptesting?
1   -module (aloha_api_test). 2   -compile (export_all). 3 4   % etest macros 5   -include_lib ("etest/include/etest.hrl")...
$: ./deps/etest/bin/etest-runner
how about autoreload?
Mochiwebreloader
1 start(_StartType, _StartArgs) -> 2     ok = application:start(compiler), 3     ok = application:start(syntax_tools), 4  ...
beware of NIFs
how about  autocompile?
guard
1 # # -*- encoding : utf-8 -*-23 guard shell do4   watch(%r{^src/.+.erl$}) do |m|5     puts `rebar compile`6   end7 end
how ab boutdeploying to  heroku?
$: heroku create aloha-erl -s cedar
Herokubuildpack
$: heroku config:addBUILDPACK_URL=http://github.com/heroku/heroku-buildpack-erlang.git
procfile
web: erl -pa deps/*/ebin ebin      -noshell      -noinput      -config priv/config/      environments/development      -s ...
1 start(_StartType, _StartArgs) -> 2     ok = application:start(compiler), 3     ok = application:start(syntax_tools), 4  ...
foreman
$: foreman start
deploy
$: git push heroku master
-----> Heroku receiving push-----> Fetching custom git buildpack... done-----> Erlang app detected-----> Installing Rebar ...
$:curl aloha-erl.herokuapp.com/             aloha
aloha-erl.herokuapp.com
that’s it folksmahalo
thanks to• https://twitter.com/wooga• https://twitter.com/knutin• https://twitter.com/hukl• https://twitter.com/hungryblan...
Resources• https://github.com/phuesler/aloha_erl• http://vimeo.com/45212367• https://github.com/knutin/elli• https://githu...
media• Erlang: The Movie - YouTube• bomb: http://www.flickr.com/photos/7969902@N07/511234695/
More than syntax
More than syntax
More than syntax
More than syntax
More than syntax
More than syntax
Upcoming SlideShare
Loading in...5
×

More than syntax

2,037

Published on

A story of a Ruby programmer having to understand that learning Erlang is more than just syntax. Learn differences in paradigms, pitfalls and applied use cases for this incredibly powerful language

Published in: Technology
1 Comment
10 Likes
Statistics
Notes
No Downloads
Views
Total Views
2,037
On Slideshare
0
From Embeds
0
Number of Embeds
9
Actions
Shares
0
Downloads
43
Comments
1
Likes
10
Embeds 0
No embeds

No notes for slide

More than syntax

  1. 1. More thanSyntax
  2. 2. Patrick Huesler@phuesler
  3. 3. More thanSyntax
  4. 4. web apis with erlanga ruby dev’s POV
  5. 5. snowboarding
  6. 6. Surfing
  7. 7. WIPEOUT
  8. 8. erlang
  9. 9. I’m an erlang beginnerso don’t believe everything I say
  10. 10. this is not an erlangintroduction
  11. 11. erlangrefresher
  12. 12. functional language
  13. 13. runtimesystem
  14. 14. open telecom platform OTP
  15. 15. whyerlang?
  16. 16. seriouslyconcurrent
  17. 17. with built inactors
  18. 18. because it isdistributed
  19. 19. faulttolerant
  20. 20. hotswaping
  21. 21. remoteshell
  22. 22. erlang atwooga
  23. 23. technicalchallenges
  24. 24. monthly active users20,543,500for Diamond Dash http://www.appdata.com/apps/facebook/127995567256931-diamond-dash (03/10/2012)
  25. 25. daily active users3,871,133for Diamond Dashhttp://www.appdata.com/apps/facebook/127995567256931-diamond-dash (03/10/2012)
  26. 26. backend traffic up to 6,500 RPSfor Monster World
  27. 27. what does mean that for aDatabase?
  28. 28. read/write ratio?write heavy
  29. 29. differentarchitectures
  30. 30. no databaseis faster than no database
  31. 31. stateful
  32. 32. Let’s use S3all the way
  33. 33. http://www.slideshare.net/wooga/from-0-to-1000000-daily-users-with-erlang
  34. 34. http://www.slideshare.net/wooga/from-0-to-1000000-daily-users-with-erlang
  35. 35. processes arecheap in erlang
  36. 36. erlangWeb APIs
  37. 37. rebar
  38. 38. $:rebar create-app appid=aloha
  39. 39. how to managedependencies like rubygems does?
  40. 40. rebar
  41. 41. $: rebar get-deps$: rebar compile$: rebar get-deps compile
  42. 42. 1 % Compiler Options for rebar 2 {erl_opts, [ 3 {src_dirs, ["src", "test"]}, 4 debug_info 5 ]}. 6 7 % Dependencies 8 {deps, [ 9 {elli, ".*", {git, "git://github.com/knutin/elli.git", "HEAD"}}, 10 {etest, ".*", {git, "git://github.com/wooga/etest.git", "HEAD"}}, 11 {etest_http, ".*", {git, "git://github.com/wooga/etest_http.git",HEAD"}} 12 ]}. 13 14 % Which files to cleanup when rebar clean is executed. 15 {clean_files, ["ebin/*.beam"]}.
  43. 43. Whatwebserver shall we use?
  44. 44. elli
  45. 45. 1 elli:start_link([2 {callback, aloha_api},3 {port, 3000}4 ])
  46. 46. how do I doRouting?
  47. 47. 1 Path = [<<"foo">>, <<"bar">>], 2 HTTPMethod = "GET", 3 Request = {HTTPMethod, Path}, 4 RouteDefinitions = [{{"GET",[<<"foo">>,<<"bar">>]}, {my_handler, my_method, []}}], 5 case erl_route_url:match(Request, RouteDefinitions) of 6 {error, notfound} -> 7 io:format("path not found"); 8 {ok, Params, {M,F,_}} -> 9 apply(M,F,Params)10 end.
  48. 48. you’re doing itwrong!!!
  49. 49. usepatternmatching
  50. 50. 1 handle(Req, _Args) ->2 Path = elli_request:path(Req),3 handle(Req#req.method, Path, Req).45 handle(GET,[<<"foo">>, <<"bar"], Req) ->6 {ok, [], <<"kekahi mau pipi">>};78 handle(_, _, _Req) ->9 {404, [], <<"wipe out!">>}.
  51. 51. does it supportmiddleware?
  52. 52. 1 Config = [ 2 {mods, [ 3 {aloha_ware, []}, 4 {mahalo_ware, []}, 5 ]} 6 ], 7 8 elli:start_link([ 9 {callback, aloha_api},10 {port, 3000},11 {callback_args, Config}12 ]).
  53. 53. eventsrequest_completerequest_throwrequest_exitrequest_errorrequest_parse_errorbad_requestclient_closedclient_timeoutelli_startup
  54. 54. 1 handle_event(request_complete, [ 2 Req, 3 ResponseCode, 4 ResponseHeaders, 5 ResponseBody, 6 Timings 7 ], Config) -> 8 9 TimingKeys = [10 accepted,11 request_start,12 heades_end,13 body_end,14 user_start,15 user_end,16 request_end17 ],18 ok;
  55. 55. awesomemonitoring
  56. 56. 1 handle_event(request_throw, [ 2 Req, 3 Exception,Stack 4 ], _Config) -> 5 ok; 6 7 handle_event(request_exit, [ 8 Req, 9 Exit,10 Stack11 ], _Config) ->12 ok;1314 handle_event(request_error, [15 Req,16 Error,17 Stack18 ], _Config) ->19 ok;20
  57. 57. how aboutenvironments?
  58. 58. $: erl -pa deps/*/ebin ebin n -config myconfig
  59. 59. 1 [ Config file 2 {aloha, [ 3 {worker_port, 3333} 4 ] 5 }, 6 7 {lager, [ 8 {handlers, [ 9 {lager_console_backend, debug},10 {lager_file_backend, [11 {"log/debug.log", debug, 10485760, "$D0", 5},12 {"log/error.log", error, 10485760, "$D0", 5},13 {"log/console.log", info, 10485760, "$D0", 5}14 ]}15 ]}16 ]}17 ].
  60. 60. how aboutlogging?
  61. 61. 1 error_logger:info_msg("alohan").2 % prints alohanok34 error_logger:warning_msg("freak set").5 % prints freak set ahead nok67 error_logger:error_msg("wipe outn").8 % prints wipe outnok
  62. 62. sasl
  63. 63. =PROGRESS REPORT==== 8-Oct-2012::12:06:24 === supervisor: {local,sasl_safe_sup} started: [{pid,<0.39.0>}, {name,overload}, {mfargs,{overload,start_link,[]}}, {restart_type,permanent}, {shutdown,2000}, {child_type,worker}]=PROGRESS REPORT==== 8-Oct-2012::12:06:24 === supervisor: {local,sasl_sup} started: [{pid,<0.37.0>}, {name,sasl_safe_sup}, {mfargs, {supervisor,start_link,[{local,sasl_safe_sup},sasl,safe]}},
  64. 64. how aboutunix style?
  65. 65. lager
  66. 66. 1 [ Config file 2 {aloha, [ 3 {worker_port, 3333} 4 ] 5 }, 6 7 {lager, [ 8 {handlers, [ 9 {lager_console_backend, debug},10 {lager_file_backend, [11 {"log/debug.log", debug, 10485760, "$D0", 5},12 {"log/error.log", error, 10485760, "$D0", 5},13 {"log/console.log", info, 10485760, "$D0", 5}14 ]}15 ]}16 ]}17 ].
  67. 67. 1 start(_StartType, _StartArgs) -> 2 ok = application:start(compiler), 3 ok = application:start(syntax_tools), 4 ok = application:start(lager), 5 6 % start mochiweb module reloader 7 reloader:start(), 8 9 elli:start_link([{callback, aloha_api}, {port, 3000}]),10 aloha_sup:start_link().
  68. 68. 1 dev_start(App) -> dev_start(App, temporary). 2 3 dev_start(App, Type) -> 4 case application:start(App, Type) of 5 {error, {not_started, DepApp}} -> 6 dev_start(DepApp), 7 dev_start(App, Type); 8 ok -> ok; 9 {error, {already_started, App}} -> ok10 end.
  69. 69. 1 lager:debug("alohan").2 % prints alohanok34 lager:warning("freak set").5 % prints freak set ahead nok67 lager:error("wipe outn").8 % prints wipe outnok
  70. 70. how about Unittesting?
  71. 71. eunit
  72. 72. nostacktracethe easy way
  73. 73. etest
  74. 74. 1 -module(aloha_utils_test). 2 -compile(export_all). 3 4 % Include etests assertion macros. 5 -include_lib("etest/include/etest.hrl"). 6 7 before_suite() -> 8 %start up applications 9 ok.1011 before_test() ->12 %load fixtures13 ok.1415 test_hello() ->16 ?assert_equal("kekahi mau pipi", aloha_utils:hello()).1718 after_test() ->19 % tear down fixtures20 ok.2122 after_suite() ->23 % stop applications24 ok.
  75. 75. $: ./deps/etest/bin/etest-runner
  76. 76. How about httptesting?
  77. 77. 1 -module (aloha_api_test). 2 -compile (export_all). 3 4 % etest macros 5 -include_lib ("etest/include/etest.hrl"). 6 % etest_http macros 7 -include_lib ("etest_http/include/etest_http.hrl"). 8 9 before_suite() ->10 application:start(aloha).1112 before_test() -> ok.1314 after_test() -> ok.1516 after_suite() ->17 application:stop(aloha).1819 test_aloha() ->20 Response = ?perform_get("http://localhost:3000/aloha"),21 ?assert_status(200, Response),22 ?assert_body("kekahi mau pipi", Response).
  78. 78. $: ./deps/etest/bin/etest-runner
  79. 79. how about autoreload?
  80. 80. Mochiwebreloader
  81. 81. 1 start(_StartType, _StartArgs) -> 2 ok = application:start(compiler), 3 ok = application:start(syntax_tools), 4 ok = application:start(lager), 5 6 % start mochiweb module reloader 7 reloader:start(), 8 9 elli:start_link([{callback, aloha_api}, {port, 3000}]),10 aloha_sup:start_link().
  82. 82. beware of NIFs
  83. 83. how about autocompile?
  84. 84. guard
  85. 85. 1 # # -*- encoding : utf-8 -*-23 guard shell do4 watch(%r{^src/.+.erl$}) do |m|5 puts `rebar compile`6 end7 end
  86. 86. how ab boutdeploying to heroku?
  87. 87. $: heroku create aloha-erl -s cedar
  88. 88. Herokubuildpack
  89. 89. $: heroku config:addBUILDPACK_URL=http://github.com/heroku/heroku-buildpack-erlang.git
  90. 90. procfile
  91. 91. web: erl -pa deps/*/ebin ebin -noshell -noinput -config priv/config/ environments/development -s aloha_app
  92. 92. 1 start(_StartType, _StartArgs) -> 2 ok = application:start(compiler), 3 ok = application:start(syntax_tools), 4 ok = application:start(lager), 5 reloader:start(), 6 7 {ok, DefaultPort} = application:get_env(aloha, worker_port), 8 Port = get_port(DefaultPort), 910 elli:start_link([{callback, aloha_api}, {port, Port}]),11 aloha_sup:start_link().1213 get_port(Default) ->14 Key = "PORT",15 case os:getenv(Key) of16 false -> Default;17 Val -> list_to_integer(Val)18 end.
  93. 93. foreman
  94. 94. $: foreman start
  95. 95. deploy
  96. 96. $: git push heroku master
  97. 97. -----> Heroku receiving push-----> Fetching custom git buildpack... done-----> Erlang app detected-----> Installing Rebar from buildpack-----> Building with Rebar ==> build_1us6u0b7p5agc (get-deps) Pulling etest from {git,"git://github.com/wooga/etest.git","HEAD"} ...... Compiled src/aloha_api.erl-----> Discovering process types Procfile declares types -> web-----> Compiled slug size: 6.3MB-----> Launching... done, v19 http://aloha-erl.herokuapp.com deployed to HerokuTo git@heroku.com:aloha-erl.git 4bf3f39..198ba04 master -> master
  98. 98. $:curl aloha-erl.herokuapp.com/ aloha
  99. 99. aloha-erl.herokuapp.com
  100. 100. that’s it folksmahalo
  101. 101. thanks to• https://twitter.com/wooga• https://twitter.com/knutin• https://twitter.com/hukl• https://twitter.com/hungryblank• https://twitter.com/guillermoo•https://twitter.com/klickmich• Andreas hasselberg• Johannes Huning
  102. 102. Resources• https://github.com/phuesler/aloha_erl• http://vimeo.com/45212367• https://github.com/knutin/elli• https://github.com/wooga/etest• https://github.com/wooga/etest_http• https://github.com/basho/lager• https://github.com/mochi/mochiweb/blob/master/src/reloader.erl• https://github.com/heroku/heroku-buildpack-erlang
  103. 103. media• Erlang: The Movie - YouTube• bomb: http://www.flickr.com/photos/7969902@N07/511234695/
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×