Beyond OTP

All about the Psychobitch!

Loïc Hoguin - @lhoguin

Erlang Cowboy and Nine Nines Founder

OTP

OTP?

Why would I need OTP?

You need OTP.

OTP gives you...

OTP is built on top of Erlang

VM boot sequence

  1. Load a few very important modules
  2. Load the modules for kernel and stdlib
  3. Start the heart process
  4. Start the error_logger process
  5. Start the application_controller process
  6. Load and start OTP applications

Two types of applications

OTP library applications

OTP applications

Application behaviour

Supervisor behaviour

Great, but my application does nothing!

Generic server behaviour

Other behaviours

Great, I'll use behaviours then!

Sometimes a supervisor isn't enough

Sometimes a gen_server is too much

Time to follow Joe Armstrong's advice

Special processes

Special processes?

proc_lib

sys

Using special processes

  1. Start your process with proc_lib:start_link/3
  2. Call proc_lib:init_ack/1 from the newly started process
  3. Write a receive loop
  4. Die if the parent process dies
  5. Handle system messages
  6. Implement system_continue/3, system_terminate/4 and system_code_change/4

Template

Not quite a gen_server yet

Anatomy of a call

I know that!

General steps

  1. Find the Pid (if named locally, globally, is remote...)
  2. Try monitoring the process
  3. If monitor returns, continue to next slide
  4. Otherwise we have a C/Java node that might not support monitors
  5. Monitor the node instead and hope for the best

General steps, after monitor

  1. Send the message, noconnect (monitor did), catch exceptions (remote pid or port process)
  2. Receive either a reply, a node down, a process down or timeout
  3. Down? Exit with appropriate reason
  4. Timeout? Demonitor and exit(timeout)
  5. Reply? Demonitor and return {ok, Reply}

In the code

In the code, continued

Do I need all this?

Make your own call

Case study: custom supervisor

Ranch connections supervisor

Two processes

One custom supervisor

More savings

start_protocol

Is that really safe?

Even more savings

Supervisor savings

With all these savings we must be rich!

Conclusion

Keep it smart

Links

Questions