elixir and otp
TRANSCRIPT
Pedro Medeiros
Software Engineer @ Globo.com
/pedrosnk /pesnk
Elixir and OTP
Briefing
➔ Introduction to Elixir➔ Processes (concurrency model)➔ OTP➔ GenServer, Supervisor, Application
Elixir
ElixirFunctional, meta-programming aware language built on top of the Erlang VM
ElixirFunctional, meta-programming aware language built on top of the Erlang VM
ElixirSample
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)
Disclaimer
Disclaimer
Elixir is not about a better syntax to the Erlang VM.
1. Extensibility
2. Productivity
3. CompatibilityElixir Design Goals
Types and modules polymorphismExtensibility
Extensibility
Process
Data 1 Data 2
data.value
Extensibility
Process
Process
Process
Call.here?
Extensibility
Process
Message 1 Message 2
JSON.decode
Productivity1. Mix2. Hex3. Docs4. Test
Great tooling
Productivity
Mix
Productivity
Hex
Productivity
Docs
Productivity
Test
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
Productivity
Summary
$ mix new foobar$ cd foobar$ mix test$ mix hex.publish$ mix hex.docs
Compatibility Elixir and Erlang sitting on a tree
Compatibility
Elixir calls Erlang
Compatibility
Erlang calls Elixir
Process 101
Basic concurrency model.
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
ProcessA P
B
C
ProcessA P
B
C
ProcessA P
B
C
ProcessA P
B
C
OTP
Open Telecom Platform
1. Built with the Erlang VM2. Super Toolset of tools to
build distributed fault-tolerant applications.
OTP
Open Telecom Platform
OTP
What’s the problem?
OTP
Switch
Switch
OTP
Server
Browser, Mobile, IoTsEndpoints
OTP
Open Telecom Platform
1. GenServer2. Supervisor3. Application
Behaviours
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
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
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
OTP
Supervisor
Supervisor
S1 S3
Strategy :one_for_one
:one_for_one
Supervisor
S1 S3S1
OTP
Supervisor
Supervisor
S1 S3
Strategy :rest_for_all
:rest_for_all
Supervisor
S1 S3S1
OTP
Supervisor
Supervisor
S1 S3
Strategy :rest_for_one
:rest_for_all
Supervisor
S1 S3S1
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
OTP
Applicaton
Application is where the top supervisors are initialized.
● Small code.● Initialize supervisors to
“live forever”
OTP
Applicaton
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
Remember this picture?
Server
Browser, Mobile, IoTsEndpoints
Thanks =D
Recursos:
1. http://elixir-lang.org/2. https://hex.pm3. Programming Elixir Book4. The little Elixir and OTP Guidebook5. slideshare.net/pemedeiros