Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Elixir and OTP

354 views

Published on

My talk at TDC 2016 about Elixir Lang and OTP. Done at the functional languages track.

Published in: Software
  • Be the first to comment

Elixir and OTP

  1. 1. Pedro Medeiros Software Engineer @ Globo.com /pedrosnk /pesnk Elixir and OTP
  2. 2. Briefing ➔ Introduction to Elixir ➔ Processes (concurrency model) ➔ OTP ➔ GenServer, Supervisor, Application
  3. 3. Elixir
  4. 4. Elixir Functional, meta- programming aware language built on top of the Erlang VM
  5. 5. Elixir Functional, meta- programming aware language built on top of the Erlang VM
  6. 6. Elixir Sample defmodule Fibonacci do def fib(0), do: 0 def fib(1), do: 1 def fib(n), do fib(n-1) + fib(n-2) end end IO.puts Fibonacci.fib(10)
  7. 7. Disclaimer
  8. 8. Disclaimer Elixir is not about a better syntax to the Erlang VM.
  9. 9. 1. Extensibility 2. Productivity 3. Compatibility Elixir Design Goals
  10. 10. Types and modules polymorphism Extensibility
  11. 11. Extensibility Process Data 1 Data 2 data.value
  12. 12. Extensibility Process Process Process Call.here?
  13. 13. Extensibility Process Message 1 Message 2 JSON.decode
  14. 14. Productivity 1. Mix 2. Hex 3. Docs 4. Test Great tooling
  15. 15. Productivity Mix
  16. 16. Productivity Hex
  17. 17. Productivity Docs
  18. 18. Productivity Test
  19. 19. Productivity Test defmodule Fibonacci do @doc """ Fibonacci iex> Fibonacci.fib(10) 55 """ def fib(0), do: 0 def fib(1), do: 1 def fib(n) do fib(n - 1) + fib(n - 2) end end
  20. 20. Productivity Summary $ mix new foobar $ cd foobar $ mix test $ mix hex.publish $ mix hex.docs
  21. 21. Compatibility Elixir and Erlang sitting on a tree
  22. 22. Compatibility Elixir calls Erlang
  23. 23. Compatibility Erlang calls Elixir
  24. 24. Process 101 Basic concurrency model.
  25. 25. Process def ExpensiveProcess do def loop do receive do {:operation, {sender, operation}} -> operation = expensive_method(operation) send sender, {:ok, operation} loop() :stop -> :error _ -> loop() end end end
  26. 26. ProcessA P B C
  27. 27. ProcessA P B C
  28. 28. ProcessA P B C
  29. 29. ProcessA P B C
  30. 30. OTP Open Telecom Platform
  31. 31. 1. Built with the Erlang VM 2. Super Toolset of tools to build distributed fault- tolerant applications. OTP Open Telecom Platform
  32. 32. OTP What’s the problem?
  33. 33. OTP Switch Switch
  34. 34. OTP Server Browser, Mobile, IoTs Endpoints
  35. 35. OTP Open Telecom Platform 1. GenServer 2. Supervisor 3. Application Behaviours
  36. 36. OTP GenServer defmodule MyServer do use GenServer def handle_call({:pop, _from, [item | state]}) do {:reply, item, state} End def handle_cast({:push, item}, state) do {:noreply, [item | state]} End def handle_info(:log, state) do IO.puts("State of server is #{inspect(state)}") {:noreply, state} end end
  37. 37. OTP GenServer defmodule MyServer do use GenServer def handle_call({:pop, _from, [item | state]}) do {:reply, item, state} End def handle_cast({:push, item}, state) do {:noreply, [item | state]} End def handle_info(:log, state) do IO.puts("State of server is #{inspect(state)}") {:noreply, state} end end synchronous asynchronous message
  38. 38. OTP Supervisor Supervisor S1 S2 S3 ● Watches its children. ● Manage its children based on a strategy. ● :one_for_one, :rest_for_all, : rest_for_one
  39. 39. OTP Supervisor Supervisor S1 S3 Strategy :one_for_one :one_for_one Supervisor S1 S3S1
  40. 40. OTP Supervisor Supervisor S1 S3 Strategy :rest_for_all :rest_for_all Supervisor S1 S3S1
  41. 41. OTP Supervisor Supervisor S1 S3 Strategy :rest_for_one :rest_for_all Supervisor S1 S3S1
  42. 42. OTP Supervisor defmodule MyServer.Supervisor do use Supervisor def start_link do Supervisor.start_link(__MODULE__, []) end def init([]) do children = [ worker(MyServer, []), worker(MyServer2, []) ] supervise(children, strategy: :one_for_one) end end
  43. 43. OTP Applicaton Application is where the top supervisors are initialized. ● Small code. ● Initialize supervisors to “live forever”
  44. 44. OTP Applicaton
  45. 45. OTP Applicaton defmodule Logger.App do use Application def start(_type, _args) do import Supervisor.Spec # ... options = [strategy: :rest_for_one, name: Logger. Supervisor] children = [worker(GenEvent, [[name: Logger]]), worker(Logger.Watcher, [Logger, Logger.Config, []], [id: Logger.Config, function: :watcher]), supervisor(Logger.Watcher, [Logger.Config, :handlers, []]), worker(Logger.Watcher, [:error_logger, Logger.ErrorHandler, {otp_reports?, sasl_reports?, threshold}, :link], [id: Logger.ErrorHandler, function: : watcher])] case Supervisor.start_link(children, options) do {:ok, sup} -> {:ok, sup, config} {:error, _} = error -> error end end
  46. 46. Remember this picture? Server Browser, Mobile, IoTs Endpoints
  47. 47. Thanks =D
  48. 48. Recursos: 1. http://elixir-lang.org/ 2. https://hex.pm 3. Programming Elixir Book 4. The little Elixir and OTP Guidebook 5. slideshare.net/pemedeiros

×