Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Knee-Deep Into P2P: A Tale of Fail (non-Elixir)

Knee-Deep Into P2P: A Tale of Fail (non-Elixir)

The slides for my talk at Lambda Days 2018 (www.lambdadays.org/lambdadays2018)

Fernando Mendes

March 07, 2018
Tweet

More Decks by Fernando Mendes

Other Decks in Programming

Transcript

  1. Step 1: receive new connections Step 2: accept and send

    messages Step 3: do a bunch of Steps 1 and 2
  2. defmodule Gossip.Server do def listen(pid, port) do {:ok, server_socket} =

    :gen_tcp.listen(port, @socket_opts) accept_loop(pid, server_socket) end defp accept_loop(pid, server_socket) do {:ok, client} = :gen_tcp.accept(server_socket) :inet.setopts(client, [active: true]) :gen_tcp.controlling_process(client, pid) Gossip.accept(pid, client) accept_loop(pid, server_socket) end end
  3. defmodule Gossip.Server do def listen(pid, port) do {:ok, server_socket} =

    :gen_tcp.listen(port, @socket_opts) accept_loop(pid, server_socket) end defp accept_loop(pid, server_socket) do {:ok, client} = :gen_tcp.accept(server_socket) :inet.setopts(client, [active: true]) :gen_tcp.controlling_process(client, pid) Gossip.accept(pid, client) accept_loop(pid, server_socket) end end
  4. defmodule Gossip.Worker do def recv_loop(pid, socket) do continue = receive

    do {:tcp, _port, msg} -> Gossip.recv(pid, msg) true {:tcp_closed, port} -> :gen_tcp.close(port) Gossip.disconnect(pid, self()) false {:send, msg} -> :gen_tcp.send(socket, msg) true end continue and recv_loop(pid, socket) end end
  5. defmodule Gossip.Worker do def recv_loop(pid, socket) do continue = receive

    do {:tcp, _port, msg} -> Gossip.recv(pid, msg) true {:tcp_closed, port} -> :gen_tcp.close(port) Gossip.disconnect(pid, self()) false {:send, msg} -> :gen_tcp.send(socket, msg) true end continue and recv_loop(pid, socket) end end
  6. Step 1: receive new connections Step 2: accept and send

    messages Step 3: do a bunch of Steps 1 and 2
  7. g

  8. Things running on one Raspberry Pi ✓BEAM (x2) ✓thebox (sensors)

    ✓Phoenix app ✓Postgres ✓Cassandra it works!
  9. @antenna = Satellite::Antenna.new(host, port) @antenna.on(“data") do |data| if data.avg_temp >

    25 slack.send_msg(“people, it's too hot") end end @antenna.on("lights") do |payload, data| payload == "on" ? turn_lights_on : turn_lights_off end @antenna.watch
  10. Distributed System Checklist •Is the number of processes known or

    finite? •Is there a global notion of time?
  11. Distributed System Checklist •Is the number of processes known or

    finite? •Is there a global notion of time? •Is the network reliable?
  12. Distributed System Checklist •Is the number of processes known or

    finite? •Is there a global notion of time? •Is the network reliable? •Is there full connectivity?
  13. Distributed System Checklist •Is the number of processes known or

    finite? •Is there a global notion of time? •Is the network reliable? •Is there full connectivity? •What happens when a process crashes?