Practical EventMachine

35bdded0ed436595945447a2484a77b0?s=47 febuiles
October 25, 2014

Practical EventMachine

EventMachine (EM) is an event-driven concurrency library for Ruby, akin to Python's Twisted or Node.js. In this talk we'll explore some of the patterns and abstractions behind EM, how we can apply those ideas to our applications and how the library has been used by companies like GitHub and Heroku.

35bdded0ed436595945447a2484a77b0?s=128

febuiles

October 25, 2014
Tweet

Transcript

  1. Practical EventMachine

  2. @febuiles

  3. http://goo.gl/Oqe4SI

  4. Events events

  5. $("#door").on("open", function(){ log("We got a new Joe in town..."); });

  6. WHY EVENTS?

  7. i/o

  8. latency

  9. MOST APPLICATIONS ARE I/O BOUND I/O BOUND

  10. https://github.com/blog/517-unicorn YOUR TYPICAL CONCURRENCY MODEL

  11. server = TCPServer.new(1337) ! while client = server.accept message =

    client.readline client.write "> #{message}" client.close end
  12. server = TCPServer.new(1337) ! while true Thread.new(server.accept) do |client| message

    = client.readline client.write "> #{message}" client.close end end
  13. THE REACTOR

  14. What is a reactor?

  15. while true events.each do |event| event.process end end

  16. Chernobyl, 1986

  17. New Safe Confinement (NSC)

  18. DON’T BLOCK THE REACTOR

  19. Reactor.run { on(:receive) do |msg| return if invalid_message(msg)? ! device

    = Device.find(msg.device_id) device.update(position: msg.position) end }
  20. Reactor.run { on(:receive) do |msg| return if invalid_message(msg)? ! device

    = Device.find(msg.device_id) device.update(position: msg.position) end }
  21. What is EventMachine?

  22. EventMachine is an event-driven I/O and lightweight concurrency library for

    Ruby.
  23. ?

  24. EventMachine is a fast, simple event- processing library for Ruby

    programs
  25. A PIMPED REACTOR.

  26. Why EventMachine?

  27. EventMachine is dead.

  28. None
  29. EventMachine is dead EventMachine is stable.

  30. Heroku Thin Campfire GitHub Rainbows EngineYard

  31. module Echo def receive_data(message) send_data "> #{message}" end end !

    EM.run { EM.start_server "0.0.0.0", 1337, Echo }
  32. module Echo def post_init puts "Client connected" end ! def

    receive_data(message) send_data "> #{message}" end ! def unbind puts "Client disconnected" end end ! EM.run { EM.start_server "0.0.0.0", 1337, Echo }
  33. EM HANDLERS VS CALLBACK HELL

  34. server = AsyncTCPServer.new("0.0.0.0", 1337) ! server.on(:accept) { message = client.async_read

    ! message.on(:ready) { client.write "> #{message}" client.close } } ! server.on(:connect) { puts "Client connected" } ! server.on(:disconnect) { puts "Client disconnected" }
  35. events
 post_init connection_completed receive_data
 unbind ssl_handshake_completed methods start_tls get_peername
 send_data


    close_connection close_connection_after_writing proxy_incoming_to pause/resume
  36. Mixing Paradigms

  37. module Echo def receive_data(data) message = parse_data(data) return if invalid_message(message)?

    ! device = Device.find(message.device_id) device.update(position: message.position) send_data :updated end end ! EM.run { EM.start_server "0.0.0.0", 1337, Echo }
  38. module Echo def receive_data(data) message = parse_data(data) return if invalid_message(message)?

    ! device = Device.find(message.device_id) device.update(position: message.position) send_data :updated end end ! EM.run { EM.start_server "0.0.0.0", 1337, Echo }
  39. DON’T BLOCK THE REACTOR

  40. None
  41. DATA STORES ! cassandra couchdb memcached mongodb mysql postgresql redis

  42. HTTP ! HttpRequest Faraday

  43. PROTOCOLS ! tcp udp amqp smtp stomp xmpp dns websockets

    and more…
  44. Other ! EM.popen EM.defer

  45. module Graham def receive_data(number) calculate = proc { graham(number) }

    ! report = proc { |result| send_data(result) } ! EM.defer(calculate, report) end end
  46. EM API

  47. EM API ! EM::timer EM::deferrable EM::queue EM::channel EM.watch_file EM.watch_process

  48. Closing Thoughts

  49. Events are hard.

  50. Evented Programming for Evented Problems

  51. DON’T BLOCK THE REACTOR !!1

  52. Thanks :) ! http://mheroin.com/rubyconfar @febuiles

  53. Credits Federico’s illustration: Pablo Pérez Nuclear cooling tower: Jonathan Brennan

    Some code samples: Aman Gupta & Jeremy Cheung Unicorn diagram: GitHub The rest: Wikipedia