YOU WILL REGRET THIS

Download YOU WILL REGRET THIS

Post on 26-May-2015

832 views

Category:

Technology

2 download

Embed Size (px)

DESCRIPTION

A lightning talk given at the London Erlang Factory in 2011, discussing Erlang anti-patterns seen in real world applications.

TRANSCRIPT

<ul><li> 1. YOU WILL REGRET THIS!My Other Mood is Even More AngryFriday, June 10, 2011</li></ul> <p> 2. IM GONNA TELL YOU A STORYIn which I tell you how to behave- how dare I?Friday, June 10, 2011 3. SOCKET.IO-ERLANG Developedby Yurii Rashkovskii, Omar Yasin, Fred Hebert (me) Usedas a base demo app for this presentation Allows to write neat front-ends for conversational websitesFriday, June 10, 2011 4. BIZARRO SOCKET.IO-ERLANG Bad socket.io-erlang used for the presentation Freelysubstitutes dependencies and actual code for whateverpurpose I haveFriday, June 10, 2011 5. APP STRUCTUREFriday, June 10, 2011 6. APP STRUCTUREBunch of supervisorsFriday, June 10, 2011 7. APP STRUCTUREBunch of supervisors web serverFriday, June 10, 2011 8. APP STRUCTUREBunch of supervisors transport handlers web serverFriday, June 10, 2011 9. APP STRUCTUREBunch of supervisorsClient code attaches here transport handlers web serverFriday, June 10, 2011 10. WE USE A WEB SERVER Weneed a web server (the slides title says just that (and alsothe previous slide)) Oh, Carrying parameters and a connection around isannoying! Lets use parametrised modules!Friday, June 10, 2011 11. PARAMETRISED MODULES-module(test_module, [Param1]).some_method() -&gt; Param1. Equivalent to:-module(test_non_pmod).some_method(Param1) -&gt; Param1.Friday, June 10, 2011 12. PARAMETRISED MODULES 1&gt; X = test_module:new(horror), X:some_method(). horror 2&gt; test_non_pmod:some_method(sane). saneFriday, June 10, 2011 13. PARAMETRISED MODULES Theyare great! We can use them as if theywere objects! They let us carry everythingas one large parameter! Lets do this!Friday, June 10, 2011 14. PARAMETRISED MODULES Theyare great! We can use them as if theywere objects! They let us carry everythingas one large parameter! Lets do this!Friday, June 10, 2011 15. What?Friday, June 10, 2011 16. ILL TELL YOU WHAT3&gt; dbg:tp({X, some_method, 0}, [...]).** exception error: no case clause matching{test_module,horror}4&gt; dbg:tp({test_module, some_method, 0}, [...]).* never matches anything *5&gt; dbg:tp({test_module, some_method, 1}, [...]).* will actually match stuff *Friday, June 10, 2011 17. What is this I dont even...Friday, June 10, 2011 18. ILL TELL YOU WHAT The parameters are global and they ghost values, become implicitfunction arguments. Errors out of nowhere. Theyre based on a old fun hack ({Mod, Fun}:(Args)) They mess up the concept of arity They mess up tracing (previous slide!) They were added because some library accepts callback modules andnothing else but might still need state around. No other reason.Friday, June 10, 2011 19. Ill use them anyway(in my server)Friday, June 10, 2011 20. YOU PROBABLY SHOULDNTBut you might not have a choice, so lets keep goingFriday, June 10, 2011 21. WHAT ELSE DO WE HAVE?A supervision tree, of course! Someof the supervisors are tricky and must interact withservers and dynamic children and ... Who starts who? By which API? Wecould probably just forget about supervisors, link stufftogether and make it simpler Lets do this!Friday, June 10, 2011 22. IN THIS VERY SLIDE I PRETENDI MAKE THE CHANGESFriday, June 10, 2011 23. APP STRUCTUREFriday, June 10, 2011 24. APP STRUCTUREtransport handlers were moved and no longer need a supervisor.They only use links.The code is more straightforward!Friday, June 10, 2011 25. APP STRUCTUREtransport handlers were moved and no longer need a supervisor.They only use links.The code is more straightforward!Friday, June 10, 2011 26. What?... my code is so much easier to read now!Friday, June 10, 2011 27. ILL TELL YOU WHAT You need to add ad-hoc start/restart policies and make sureorderly shutdowns work ne with just links. Care to add tests? You can no longer benet from systools application upgradesand downgrades as they depend on supervisors You will need to take your app down!Friday, June 10, 2011 28. Its okay, Ill do rolling upgradesthrough all nodesthis should avoid downtimeFriday, June 10, 2011 29. NOT WITH LIVE SESSIONS! Nobody likes to be disconnected during a conversation!Would you disconnect this guy?Friday, June 10, 2011 30. Stolen from Yuriis talk earlier todayNOT WITH LIVE SESSIONS!Nobody likes to be disconnected!Friday, June 10, 2011 31. Stolen from Yuriis talk earlier todayNOT WITH LIVE SESSIONS!Nobody likes to be disconnected!Friday, June 10, 2011 32. Youre killing me. Ill leave the change in. Code is meant to be read rst!Friday, June 10, 2011 33. OK, AS LONG AS YOU LISTENTO THE NEXT POINTFriday, June 10, 2011 34. SURE, IM HAPPYWITH MY APP Its all done in OTP Now Easier to read Usesparametrized modules, hell yes! Its time to start it! Lets do this!Friday, June 10, 2011 35. THIS IS GONNA BE FUN main(_) -&gt; appmon:start(), sasl:start(normal, []), socketio:start(normal,[]), {ok, Pid} = socketio_listener:start([ {http_port, 7878}, {default_http_handler,?MODULE}]), EventMgr = socketio_listener:event_manager(Pid), ok = gen_event:add_handler(EventMgr, ?MODULE,[]), receive _ -&gt; ok end.And it works! (believe me)Friday, June 10, 2011 36. THIS IS GONNA BE FUN main(_) -&gt; appmon:start(), sasl:start(normal, []), socketio:start(normal,[]), {ok, Pid} = socketio_listener:start([ {http_port, 7878}, {default_http_handler,?MODULE}]), EventMgr = socketio_listener:event_manager(Pid), ok = gen_event:add_handler(EventMgr, ?MODULE,[]), receive _ -&gt; ok end.And it works! (believe me)Friday, June 10, 2011 37. YOU, AGAIN!?You are worse than clippyWhat is it this time?Friday, June 10, 2011 38. ILL TELL YOU WHAT The VMstarts all applications under the Application Controller Startingone outside of it means it is not supervised by it No app failure strategy (permanent, transient, temporary) No access to env variables Norespect of start phasesor dependenciesFriday, June 10, 2011 39. AND IT GETS WORSE Withbad default values (or overly defensive code) for envvariables, bad things happen... In this case, innitely many web sockets opened for each client(before a patch to change that, because I forgot to listen to this hint)Friday, June 10, 2011 40. DO THIS INSTEAD main(_) -&gt; appmon:start(), application:start(sasl), application:start(misultin), application:start(socketio), {ok, Pid} = socketio_listener:start([ {http_port, 7878}, {default_http_handler,?MODULE}]), EventMgr = socketio_listener:event_manager(Pid), ok = gen_event:add_handler(EventMgr, ?MODULE,[]), receive _ -&gt; ok end.And it works better! (believe me)Friday, June 10, 2011 41. Oh...Friday, June 10, 2011 42. RIGHTYour OTP code is not worth much without thatFriday, June 10, 2011 43. One more thing...Friday, June 10, 2011 44. DONT LISTEN TO MEIf your product might not ship because of this.Making it work is #1. Making it pretty is not.Friday, June 10, 2011 45. HAPPY HACKINGHopefully you wont regret thisFriday, June 10, 2011</p>