Elixir and OTP
-
Upload
pedro-medeiros -
Category
Software
-
view
194 -
download
2
Transcript of Elixir and OTP
![Page 1: Elixir and OTP](https://reader031.fdocuments.us/reader031/viewer/2022021419/589c2dc21a28ab65248b6689/html5/thumbnails/1.jpg)
Pedro Medeiros
Software Engineer @ Globo.com
/pedrosnk /pesnk
Elixir and OTP
![Page 2: Elixir and OTP](https://reader031.fdocuments.us/reader031/viewer/2022021419/589c2dc21a28ab65248b6689/html5/thumbnails/2.jpg)
Briefing
➔ Introduction to Elixir➔ Processes (concurrency model)➔ OTP➔ GenServer, Supervisor, Application
![Page 3: Elixir and OTP](https://reader031.fdocuments.us/reader031/viewer/2022021419/589c2dc21a28ab65248b6689/html5/thumbnails/3.jpg)
Elixir
![Page 4: Elixir and OTP](https://reader031.fdocuments.us/reader031/viewer/2022021419/589c2dc21a28ab65248b6689/html5/thumbnails/4.jpg)
ElixirFunctional, meta-programming aware language built on top of the Erlang VM
![Page 5: Elixir and OTP](https://reader031.fdocuments.us/reader031/viewer/2022021419/589c2dc21a28ab65248b6689/html5/thumbnails/5.jpg)
ElixirFunctional, meta-programming aware language built on top of the Erlang VM
![Page 6: Elixir and OTP](https://reader031.fdocuments.us/reader031/viewer/2022021419/589c2dc21a28ab65248b6689/html5/thumbnails/6.jpg)
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)
![Page 7: Elixir and OTP](https://reader031.fdocuments.us/reader031/viewer/2022021419/589c2dc21a28ab65248b6689/html5/thumbnails/7.jpg)
Disclaimer
![Page 8: Elixir and OTP](https://reader031.fdocuments.us/reader031/viewer/2022021419/589c2dc21a28ab65248b6689/html5/thumbnails/8.jpg)
Disclaimer
Elixir is not about a better syntax to the Erlang VM.
![Page 9: Elixir and OTP](https://reader031.fdocuments.us/reader031/viewer/2022021419/589c2dc21a28ab65248b6689/html5/thumbnails/9.jpg)
1. Extensibility
2. Productivity
3. CompatibilityElixir Design Goals
![Page 10: Elixir and OTP](https://reader031.fdocuments.us/reader031/viewer/2022021419/589c2dc21a28ab65248b6689/html5/thumbnails/10.jpg)
Types and modules polymorphismExtensibility
![Page 11: Elixir and OTP](https://reader031.fdocuments.us/reader031/viewer/2022021419/589c2dc21a28ab65248b6689/html5/thumbnails/11.jpg)
Extensibility
Process
Data 1 Data 2
data.value
![Page 12: Elixir and OTP](https://reader031.fdocuments.us/reader031/viewer/2022021419/589c2dc21a28ab65248b6689/html5/thumbnails/12.jpg)
Extensibility
Process
Process
Process
Call.here?
![Page 13: Elixir and OTP](https://reader031.fdocuments.us/reader031/viewer/2022021419/589c2dc21a28ab65248b6689/html5/thumbnails/13.jpg)
Extensibility
Process
Message 1 Message 2
JSON.decode
![Page 14: Elixir and OTP](https://reader031.fdocuments.us/reader031/viewer/2022021419/589c2dc21a28ab65248b6689/html5/thumbnails/14.jpg)
![Page 15: Elixir and OTP](https://reader031.fdocuments.us/reader031/viewer/2022021419/589c2dc21a28ab65248b6689/html5/thumbnails/15.jpg)
Productivity1. Mix2. Hex3. Docs4. Test
Great tooling
![Page 16: Elixir and OTP](https://reader031.fdocuments.us/reader031/viewer/2022021419/589c2dc21a28ab65248b6689/html5/thumbnails/16.jpg)
Productivity
Mix
![Page 17: Elixir and OTP](https://reader031.fdocuments.us/reader031/viewer/2022021419/589c2dc21a28ab65248b6689/html5/thumbnails/17.jpg)
Productivity
Hex
![Page 18: Elixir and OTP](https://reader031.fdocuments.us/reader031/viewer/2022021419/589c2dc21a28ab65248b6689/html5/thumbnails/18.jpg)
Productivity
Docs
![Page 19: Elixir and OTP](https://reader031.fdocuments.us/reader031/viewer/2022021419/589c2dc21a28ab65248b6689/html5/thumbnails/19.jpg)
Productivity
Test
![Page 20: Elixir and OTP](https://reader031.fdocuments.us/reader031/viewer/2022021419/589c2dc21a28ab65248b6689/html5/thumbnails/20.jpg)
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
![Page 21: Elixir and OTP](https://reader031.fdocuments.us/reader031/viewer/2022021419/589c2dc21a28ab65248b6689/html5/thumbnails/21.jpg)
Productivity
Summary
$ mix new foobar$ cd foobar$ mix test$ mix hex.publish$ mix hex.docs
![Page 22: Elixir and OTP](https://reader031.fdocuments.us/reader031/viewer/2022021419/589c2dc21a28ab65248b6689/html5/thumbnails/22.jpg)
Compatibility Elixir and Erlang sitting on a tree
![Page 23: Elixir and OTP](https://reader031.fdocuments.us/reader031/viewer/2022021419/589c2dc21a28ab65248b6689/html5/thumbnails/23.jpg)
Compatibility
Elixir calls Erlang
![Page 24: Elixir and OTP](https://reader031.fdocuments.us/reader031/viewer/2022021419/589c2dc21a28ab65248b6689/html5/thumbnails/24.jpg)
Compatibility
Erlang calls Elixir
![Page 25: Elixir and OTP](https://reader031.fdocuments.us/reader031/viewer/2022021419/589c2dc21a28ab65248b6689/html5/thumbnails/25.jpg)
Process 101
Basic concurrency model.
![Page 26: Elixir and OTP](https://reader031.fdocuments.us/reader031/viewer/2022021419/589c2dc21a28ab65248b6689/html5/thumbnails/26.jpg)
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
![Page 27: Elixir and OTP](https://reader031.fdocuments.us/reader031/viewer/2022021419/589c2dc21a28ab65248b6689/html5/thumbnails/27.jpg)
ProcessA P
B
C
![Page 28: Elixir and OTP](https://reader031.fdocuments.us/reader031/viewer/2022021419/589c2dc21a28ab65248b6689/html5/thumbnails/28.jpg)
ProcessA P
B
C
![Page 29: Elixir and OTP](https://reader031.fdocuments.us/reader031/viewer/2022021419/589c2dc21a28ab65248b6689/html5/thumbnails/29.jpg)
ProcessA P
B
C
![Page 30: Elixir and OTP](https://reader031.fdocuments.us/reader031/viewer/2022021419/589c2dc21a28ab65248b6689/html5/thumbnails/30.jpg)
ProcessA P
B
C
![Page 31: Elixir and OTP](https://reader031.fdocuments.us/reader031/viewer/2022021419/589c2dc21a28ab65248b6689/html5/thumbnails/31.jpg)
OTP
Open Telecom Platform
![Page 32: Elixir and OTP](https://reader031.fdocuments.us/reader031/viewer/2022021419/589c2dc21a28ab65248b6689/html5/thumbnails/32.jpg)
1. Built with the Erlang VM2. Super Toolset of tools to
build distributed fault-tolerant applications.
OTP
Open Telecom Platform
![Page 33: Elixir and OTP](https://reader031.fdocuments.us/reader031/viewer/2022021419/589c2dc21a28ab65248b6689/html5/thumbnails/33.jpg)
OTP
What’s the problem?
![Page 34: Elixir and OTP](https://reader031.fdocuments.us/reader031/viewer/2022021419/589c2dc21a28ab65248b6689/html5/thumbnails/34.jpg)
OTP
Switch
Switch
![Page 35: Elixir and OTP](https://reader031.fdocuments.us/reader031/viewer/2022021419/589c2dc21a28ab65248b6689/html5/thumbnails/35.jpg)
OTP
Server
Browser, Mobile, IoTsEndpoints
![Page 36: Elixir and OTP](https://reader031.fdocuments.us/reader031/viewer/2022021419/589c2dc21a28ab65248b6689/html5/thumbnails/36.jpg)
OTP
Open Telecom Platform
1. GenServer2. Supervisor3. Application
Behaviours
![Page 37: Elixir and OTP](https://reader031.fdocuments.us/reader031/viewer/2022021419/589c2dc21a28ab65248b6689/html5/thumbnails/37.jpg)
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
![Page 38: Elixir and OTP](https://reader031.fdocuments.us/reader031/viewer/2022021419/589c2dc21a28ab65248b6689/html5/thumbnails/38.jpg)
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
![Page 39: Elixir and OTP](https://reader031.fdocuments.us/reader031/viewer/2022021419/589c2dc21a28ab65248b6689/html5/thumbnails/39.jpg)
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
![Page 40: Elixir and OTP](https://reader031.fdocuments.us/reader031/viewer/2022021419/589c2dc21a28ab65248b6689/html5/thumbnails/40.jpg)
OTP
Supervisor
Supervisor
S1 S3
Strategy :one_for_one
:one_for_one
Supervisor
S1 S3S1
![Page 41: Elixir and OTP](https://reader031.fdocuments.us/reader031/viewer/2022021419/589c2dc21a28ab65248b6689/html5/thumbnails/41.jpg)
OTP
Supervisor
Supervisor
S1 S3
Strategy :rest_for_all
:rest_for_all
Supervisor
S1 S3S1
![Page 42: Elixir and OTP](https://reader031.fdocuments.us/reader031/viewer/2022021419/589c2dc21a28ab65248b6689/html5/thumbnails/42.jpg)
OTP
Supervisor
Supervisor
S1 S3
Strategy :rest_for_one
:rest_for_all
Supervisor
S1 S3S1
![Page 43: Elixir and OTP](https://reader031.fdocuments.us/reader031/viewer/2022021419/589c2dc21a28ab65248b6689/html5/thumbnails/43.jpg)
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
![Page 44: Elixir and OTP](https://reader031.fdocuments.us/reader031/viewer/2022021419/589c2dc21a28ab65248b6689/html5/thumbnails/44.jpg)
OTP
Applicaton
Application is where the top supervisors are initialized.
● Small code.● Initialize supervisors to
“live forever”
![Page 45: Elixir and OTP](https://reader031.fdocuments.us/reader031/viewer/2022021419/589c2dc21a28ab65248b6689/html5/thumbnails/45.jpg)
OTP
Applicaton
![Page 46: Elixir and OTP](https://reader031.fdocuments.us/reader031/viewer/2022021419/589c2dc21a28ab65248b6689/html5/thumbnails/46.jpg)
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
![Page 47: Elixir and OTP](https://reader031.fdocuments.us/reader031/viewer/2022021419/589c2dc21a28ab65248b6689/html5/thumbnails/47.jpg)
Remember this picture?
Server
Browser, Mobile, IoTsEndpoints
![Page 48: Elixir and OTP](https://reader031.fdocuments.us/reader031/viewer/2022021419/589c2dc21a28ab65248b6689/html5/thumbnails/48.jpg)
Thanks =D
![Page 49: Elixir and OTP](https://reader031.fdocuments.us/reader031/viewer/2022021419/589c2dc21a28ab65248b6689/html5/thumbnails/49.jpg)
Recursos:
1. http://elixir-lang.org/2. https://hex.pm3. Programming Elixir Book4. The little Elixir and OTP Guidebook5. slideshare.net/pemedeiros