Skip to content

Instantly share code, notes, and snippets.

def async_query(pid, statement, params) do
message = {:query, statement, params}
process = GenServer.whereis(pid)
monitor = Process.monitor(process)
from = {self(), monitor}
:ok = Connection.cast(pid, {message, from})
%Task{ref: monitor}
end
@fishcakez
fishcakez / gist:75720ec5c11b6cad519e
Created October 14, 2015 19:05
Race condition in GenRouter message protocol
source is a named process on different node to the sink
* sink sends ask to {:source, :source_node}
* source adds sink to its list of sinks
* source exits
* source supervisor receives :EXIT from source(1) and restarts the source, source(2)
* sink sends ask to {:source, :source_node}
* source(2) adds sink_pid to its list of sinks
* sink receives :DOWN from source(1) and assumes :eos
* source(2) sends events to sink
@fishcakez
fishcakez / event_watcher.ex
Created October 8, 2015 17:25
Simple example of the GenEvent watcher pattern to ensure handlers are re-added on crash
defmodule EventWatcher do
use Application
def start(_type, _args) do
import Supervisor.Spec, warn: false
children = [
worker(GenEvent, [[name: EventWatcher.GenEvent]], [id: :event_manager]),
supervisor(EventWatcher.Watcher.Supervisor, [], [id: :watcher_supervisor])
]
@fishcakez
fishcakez / simple_sup.ex
Last active March 22, 2018 17:28
Examples of supervision trees for `:simple_one_for_one` supervisors
defmodule SimpleSup do
@moduledoc """
This file shows methods for starting a configurable number of children under
a `:simple_one_for_one` supervisor.
When the supervision tree is first started all methods behave the same, `size`
children are started and the `:starter` returns `:ignore`. However if the
restart limit for those children is reached the `:simple_one_for_one`
supervisor will be restarted and then the `:starter`. It is possible that the
`:simple_one_for_one` is restarted successfully but the `:starter` fails to
defmodule KVServer do
use Application
def start(_type, _args) do
import Supervisor.Spec
children = [
supervisor(Task.Supervisor, [[name: KVServer.TaskSupervisor]]),
worker(Task, [KVServer, :accept, [4040]])
]
defmodule BasicBench do
use Benchfella
@num :random.uniform(999_999_999_999)
bench "interpolation" do
@num
|> NumToWordsString.say
end
def concurrent_factorial(range, worker_pool_by_nodenames \\ worker_pool_by_nodenames)
def concurrent_factorial(%Range{first: start, last: finish}, worker_pool_by_nodenames) do
pmap(split_range_of_numbers(start..finish), fn(range) -> range |> Enum.reduce(&(&1*&2)) end, worker_pool_by_nodenames) |>
Enum.reduce(&(&1*&2))
end
def concurrent_factorial(n, worker_pool_by_nodenames) when is_integer(n) and n > 0 do
concurrent_factorial(1..n, worker_pool_by_nodenames)
try do
is_atom(atom) or :erlang.error(:badarg)
else
true ->
:atom
rescue
x in [ArgumentError] ->
:not_atom
end
@fishcakez
fishcakez / enter_loop.ex
Last active August 29, 2015 14:04
EnterLoop
defmodule EnterLoop do
use GenServer
require Logger
def start_link(opts) do
spawn_opts = Keyword.get(opts, :spawn_opt, [])
# :infinity start timeout
:proc_lib.start_link(__MODULE__, :init_it, [opts], :infinity, spawn_opts)
end
File.read("match.exs") |> (fn({:ok, bin}) -> bin end).() |> String.length
#=> 156
s = """
id;name;value
1;foo;hi
2;bar;bye
"""
s