more than syntax

109
Syntax Mr

Upload: wooga

Post on 22-Nov-2014

2.255 views

Category:

Technology


0 download

DESCRIPTION

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

TRANSCRIPT

Page 1: More than syntax

SyntaxMore than

Page 2: More than syntax

@phueslerPatrick Huesler

Page 3: More than syntax

SyntaxMore than

Page 4: More than syntax

a ruby dev’s POVerlang

web apis with

Page 5: More than syntax

snowboarding

Page 6: More than syntax

Surfing

Page 7: More than syntax

WIPEOUT

Page 8: More than syntax

erlang

Page 9: More than syntax

so don’t believe everything I say

beginnerI’m an erlang

Page 10: More than syntax

introductionerlang

this is not an

Page 11: More than syntax

refreshererlang

Page 12: More than syntax

functionallanguage

Page 13: More than syntax

runtimesystem

Page 14: More than syntax

OTPopen telecom platform

Page 15: More than syntax

erlang?why

Page 16: More than syntax
Page 17: More than syntax

concurrentseriously

Page 18: More than syntax

actorswith built in

Page 19: More than syntax

distributedbecause it is

Page 20: More than syntax

tolerantfault

Page 21: More than syntax
Page 22: More than syntax

hot swaping

Page 23: More than syntax

shellremote

Page 24: More than syntax

woogaerlang at

Page 25: More than syntax
Page 26: More than syntax
Page 27: More than syntax
Page 28: More than syntax

challengestechnical

Page 29: More than syntax

for Diamond Dash20,543,500monthly active users

http://www.appdata.com/apps/facebook/127995567256931-diamond-dash (03/10/2012)

Page 30: More than syntax

for Diamond Dash3,871,133daily active users

http://www.appdata.com/apps/facebook/127995567256931-diamond-dash (03/10/2012)

Page 31: More than syntax

for Monster World6,500 RPS

backend traffic up to

Page 32: More than syntax

Database?what does mean that for a

Page 33: More than syntax

write heavyread/write ratio?

Page 34: More than syntax

architecturesdifferent

Page 35: More than syntax

is faster than no databasedatabase

no

Page 36: More than syntax

stateful

Page 37: More than syntax

all the wayS3

Let’s use

Page 40: More than syntax

in erlangcheapprocesses are

Page 41: More than syntax

Web APIserlang

Page 42: More than syntax

rebar

Page 43: More than syntax

$:rebar create-app appid=aloha

Page 44: More than syntax

like rubygems does?dependencies

how to manage

Page 45: More than syntax

rebar

Page 46: More than syntax

$: rebar get-deps$: rebar compile$: rebar get-deps compile

Page 47: More than syntax

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"]}.

Page 48: More than syntax

shall we use?webserver

What

Page 49: More than syntax

elli

Page 50: More than syntax

1 elli:start_link([ 2 {callback, aloha_api}, 3 {port, 3000} 4 ])

Page 51: More than syntax

Routing?how do I do

Page 52: More than syntax

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.

Page 53: More than syntax

wrong!!!you’re doing it

Page 54: More than syntax

pattern matching

use

Page 55: More than syntax

1 handle(Req, _Args) -> 2 Path = elli_request:path(Req), 3 handle(Req#req.method, Path, Req). 4 5 handle('GET',[<<"foo">>, <<"bar"], Req) -> 6 {ok, [], <<"kekahi mau pipi">>}; 7 8 handle(_, _, _Req) -> 9 {404, [], <<"wipe out!">>}.

Page 56: More than syntax

middleware?does it support

Page 57: More than syntax

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 ]).

Page 58: More than syntax

request_completerequest_throwrequest_exitrequest_errorrequest_parse_errorbad_requestclient_closedclient_timeoutelli_startup

events

Page 59: More than syntax

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_end 17 ], 18 ok;

Page 60: More than syntax

awesome monitoring

Page 61: More than syntax

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 Stack 11 ], _Config) -> 12 ok; 13 14 handle_event(request_error, [ 15 Req, 16 Error, 17 Stack 18 ], _Config) -> 19 ok; 20

Page 62: More than syntax

environments?how about

Page 63: More than syntax

$: erl -pa deps/*/ebin ebin \n -config myconfig

Page 64: More than syntax

1 [ 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 ].

Config file

Page 65: More than syntax

logging?how about

Page 66: More than syntax

1 error_logger:info_msg("aloha\n"). 2 % prints aloha\nok 3 4 error_logger:warning_msg("freak set"). 5 % prints freak set ahead \nok 6 7 error_logger:error_msg("wipe out\n"). 8 % prints wipe out\nok

Page 67: More than syntax

sasl

Page 68: More than syntax

=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]}}, {restart_type,permanent}, {shutdown,infinity}, {child_type,supervisor}]

=PROGRESS REPORT==== 8-Oct-2012::12:06:24 === supervisor: {local,sasl_sup} started: [{pid,<0.40.0>}, {name,release_handler}, {mfargs,{release_handler,start_link,[]}}, {restart_type,permanent}, {shutdown,2000}, {child_type,worker}]

Page 69: More than syntax

unix style?how about

Page 70: More than syntax

lager

Page 71: More than syntax

1 [ 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 ].

Config file

Page 72: More than syntax

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().

Page 73: More than syntax

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}} -> ok 10 end.

Page 74: More than syntax

1 lager:debug("aloha\n"). 2 % prints aloha\nok 3 4 lager:warning("freak set"). 5 % prints freak set ahead \nok 6 7 lager:error("wipe out\n"). 8 % prints wipe out\nok

Page 75: More than syntax

Unit testing?

how about

Page 76: More than syntax

eunit

Page 77: More than syntax

the easy waystacktrace

no

Page 78: More than syntax

etest

Page 79: More than syntax

1 -module(aloha_utils_test). 2 -compile(export_all). 3 4 % Include etest's assertion macros. 5 -include_lib("etest/include/etest.hrl"). 6 7 before_suite() -> 8 %start up applications 9 ok. 10 11 before_test() -> 12 %load fixtures 13 ok. 14 15 test_hello() -> 16 ?assert_equal("kekahi mau pipi", aloha_utils:hello()). 17 18 after_test() -> 19 % tear down fixtures 20 ok. 21 22 after_suite() -> 23 % stop applications 24 ok.

Page 80: More than syntax

$: ./deps/etest/bin/etest-runner

Page 81: More than syntax

http testing?

How about

Page 82: More than syntax

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). 11 12 before_test() -> ok. 13 14 after_test() -> ok. 15 16 after_suite() -> 17 application:stop(aloha). 18 19 test_aloha() -> 20 Response = ?perform_get("http://localhost:3000/aloha"), 21 ?assert_status(200, Response), 22 ?assert_body("kekahi mau pipi", Response).

Page 83: More than syntax

$: ./deps/etest/bin/etest-runner

Page 84: More than syntax

autoreload?

how about

Page 85: More than syntax

Mochiwebreloader

Page 86: More than syntax

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().

Page 87: More than syntax

NIFsbeware of

Page 88: More than syntax
Page 89: More than syntax

auto compile?

how about

Page 90: More than syntax

guard

Page 91: More than syntax

1 # # -*- encoding : utf-8 -*- 2 3 guard 'shell' do 4 watch(%r{^src/.+\.erl$}) do |m| 5 puts `rebar compile` 6 end 7 end

Page 92: More than syntax

deploying toheroku?

how ab bout

Page 93: More than syntax

$: heroku create aloha-erl -s cedar

Page 94: More than syntax

Heroku buildpack

Page 96: More than syntax

procfile

Page 97: More than syntax

web: erl -pa deps/*/ebin ebin-noshell-noinput-config priv/config/environments/development-s aloha_app

Page 98: More than syntax

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), 9 10 elli:start_link([{callback, aloha_api}, {port, Port}]), 11 aloha_sup:start_link(). 12 13 get_port(Default) -> 14 Key = "PORT", 15 case os:getenv(Key) of 16 false -> Default; 17 Val -> list_to_integer(Val) 18 end.

Page 99: More than syntax

foreman

Page 100: More than syntax

$: foreman start

Page 101: More than syntax

deploy

Page 102: More than syntax

$: git push heroku master

Page 103: More than syntax

-----> 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 Heroku

To [email protected]:aloha-erl.git 4bf3f39..198ba04 master -> master

Page 104: More than syntax

$:curl aloha-erl.herokuapp.com/aloha

Page 105: More than syntax

aloha-erl.herokuapp.com

Page 106: More than syntax

mahalothat’s it folks

Page 107: More than syntax

• 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

thanks to