Slide 1

Slide 1 text

EXTENDING OTP WITH CUSTOM BEHAVIOURS Looking beyond a gen_server

Slide 2

Slide 2 text

MICHAŁ MUSKAŁA http://michal.muskala.eu/ https://github.com/michalmuskala/ @michalmuskala

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

THE MASTERPLAN • The GenServer • OTP special processes • OTP behaviours • Custom behaviours

Slide 7

Slide 7 text

THE EXPERIMENT

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

WHO USED ELIXIR?

Slide 10

Slide 10 text

WHO USED SPAWN & RECEIVE FOR PMAP?

Slide 11

Slide 11 text

WHO WROTE A GENSERVER IMPLEMENTATION?

Slide 12

Slide 12 text

WHO WROTE A BEHAVIOUR?

Slide 13

Slide 13 text

BEHAVIOUR

Slide 14

Slide 14 text

OTP BEHAVIOURS • application • gen_server • gen_fsm • gen_statem • gen_event • supervisor

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

http://blog.plataformatec.com.br/2015/10/mocks-and-explicit-contracts/ “Furthermore, we have already defined three implementations of the Twitter API, so we better make it all explicit. In Elixir we do so by defining a behaviour with callback functions (…)”

Slide 17

Slide 17 text

TWO USE CASES FOR BEHAVIOURS message driven data driven behaviour spectrum

Slide 18

Slide 18 text

GENSERVER IS EMERGENT

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

defmodule Calculator do def start do spawn(__MODULE__, :loop, [[]]) end def add(pid, x, y) do send(pid, {:add, self(), x, y}) receive do {:result, x} -> x end end def loop(state) do receive do {:add, from, x, y} -> send(from, {:result, x + y}) end loop(state) end end

Slide 21

Slide 21 text

MESSAGE HANDLER PROCESS • waits for a message • parses the message • handles the message • sends a reply

Slide 22

Slide 22 text

defmodule Calculator do def start do spawn(__MODULE__, :loop, [[]]) end def add(pid, x, y) do send(pid, {:add, self(), x, y}) receive do {:result, x} -> x end end def loop(state) do receive do {:add, from, x, y} -> send(from, {:result, x + y}) end loop(state) end end

Slide 23

Slide 23 text

MESSAGE HANDLER PROCESS • waits for a message • parses the message • handles the message • sends a reply

Slide 24

Slide 24 text

defmodule Calculator do use GenServer def start do GenServer.start_link(__MODULE__, []) end def add(pid, x, y) do GenServer.call(pid, {:add, x, y}) end def handle_call({:add, x, y}, from, state) do {:reply, x + y, state} end end

Slide 25

Slide 25 text

GEN_SERVER • init/1 • handle_cast/2 • handle_call/3 • handle_info/2 • terminate/2 • code_change/3

Slide 26

Slide 26 text

GEN_SERVER • init/1 • handle_cast/2 • handle_call/3 • handle_info/2 • terminate/2 • code_change/3 • format_status/2

Slide 27

Slide 27 text

FORMAT_STATUS • :sys.get_status/2 • abnormal termination and logging

Slide 28

Slide 28 text

GenServer Callback module start_link call multi_call cast abcast init handle_cast handle_call handle_info terminate

Slide 29

Slide 29 text

HOW IS GEN_SERVER IMPLEMENTED? HOW CAN WE DO SOMETHING SIMILAR?

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

OTP SPECIAL PROCESSES • process fits into a supervision tree • support for the :sys debug utilities • process system messages • OTP design principles
 http://erlang.org/doc/design_principles/des_princ.html

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

SPECIAL PROCESS IMPLEMENTATION • start with :proc_lib.start_link/3 • initialise debug with :sys.debug_options/1 • respond to start_link with :proc_lib.init_ack/2 • use :sys.handle_system_msg/6 for {:system, from, request} • implement system_continue/3 and system_terminate/4 callbacks

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

GEN_SERVER TO THE RESCUE!

Slide 36

Slide 36 text

GEN_SERVER FOR EVERYTHING? • pooling • database connection • web server • chat room • data streams

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

CONNECTION • init/1, handle_call/3, handle_cast/2, handle_info/2, teminate/2, code_change/3 • connect/2 • disconnect/2 • :backoff, :connect, :disconnect,

Slide 39

Slide 39 text

DBCONNECTION • connect/1, disconnect/2, ping/1 • out of process state: checkout/1, checkin/1 • transaction: handle_begin/2, handle_commit/2, handle_rollback/2 • queries: handle_prepare/3, handle_execute/4, handle_close/3 • coursors: handle_declare/4, handle_first/4, handle_next/4, handle_deallocate/4

Slide 40

Slide 40 text

ECTO.ADAPTER • ensure_all_started/2, child_spec/3 • loaders/2, dumpers/2, autogenerate/1 • prepare/2, execute/6, insert_all/7, insert/6, update/6, delete/4 • transaction/3, in_transaction?/1, rollback/2 • extensions for storage, migrations, dumps

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

RANCH_TRANSPORT • acceptor: listen/1, accept/2, accept_ack/2 • connect/3, connect/4 • communication: recv/3, send/2, sendfile/2, sendfile/4, sendfile/5 • config: setopts/2, controlling_process/2, • read settings: name/0, secure/0, messages/0, peername/1, sockname/1 • shutdown/2, close/1

Slide 43

Slide 43 text

COWBOY_MIDDLEWARE AND PLUG

Slide 44

Slide 44 text

No content

Slide 45

Slide 45 text

COWBOY_MIDDLEWARE AND PLUG • cowboy: execute/2 • plug: call/2

Slide 46

Slide 46 text

PHOENIX.CHANNEL • join/2 • handle_in/3 • handle_out/3 (*) • handle_info/2 • terminate/2 • code_change/3

Slide 47

Slide 47 text

HACKNEY_POOL_HANDLER • start/0 • checkout/4 • checkin/2

Slide 48

Slide 48 text

GENSTAGE • init/1 • handle_demand/2 • handle_subscribe/4, handle_cancel/3 • handle_events/3 • handle_call/3, handle_cast/2, handle_info/2, teminate/2, code_change/3

Slide 49

Slide 49 text

THERE ARE TWO WAYS TO DO BEHAVIOURS

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

MAC ROS

Slide 52

Slide 52 text

defmacro __using__(_) do quote do use GenServer def handle_call({:foo, args}, _, state) do {reply, state} = some_callback(args, state) Some.more_logic(reply, state) {:reply, reply, state} end defp some_callback(args, state), do: {:ok, state} defoverridable [some_callback: 2] end end

Slide 53

Slide 53 text

use GenServer @callback init(term) :: {:ok, state :: term} | ... @callback some_callback(term) :: {reply :: term, state :: term} def start_link(module, args, opts) do GenServer.start_link(__MODULE__, {module, args, opts}, opts) end def init({mod, args, opts}) do case mod.init(args) do {:ok, int} -> %{mod: mod, int: int} other -> other end end

Slide 54

Slide 54 text

def handle_call({:foo, arg}, _from, %{mod: mod, int: int} = state) do {reply, int} = mod.some_callback(arg, int) {:reply, reply, %{state | internal: int}} end def format_status(:normal, [pdict, %{mod: mod, int: int}]) do [{:data, [{'State', int}]}] end def format_status(:terminate, [pdict, %{int: int}]) do int end

Slide 55

Slide 55 text

BEHAVIOURS ARE POWERFUL ABSTRACTIONS

Slide 56

Slide 56 text

THEY ARE GREAT OPPORTUNITIES FOR LIBRARIES

Slide 57

Slide 57 text

DON’T USE MACROS TO SPECIALISE GENSERVER

Slide 58

Slide 58 text

EXTENDING OTP WITH CUSTOM BEHAVIOURS Looking beyond a gen_server

Slide 59

Slide 59 text

No content