Skip to content

Instantly share code, notes, and snippets.

@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
@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])
]
-module(pecypc_session).
-author('Vladimir Dronnikov <dronnikov@gmail.com>').
%% -----------------------------------------------------------------------------
%% API exports
%% -----------------------------------------------------------------------------
-export([
add/1,
add/2,
@fishcakez
fishcakez / restforbug.erl
Created April 2, 2013 21:32
Rest for one restart bug.
-module(restforbug).
-export([start/0]).
start() ->
ok = application:start(restforbug),
exit(whereis(restforbug_first), bye).
Dispatch = cowboy_router:compile([
{'_', [
{"/", cowboy_static,
[{directory, {priv_dir, web, []}},
{mimetypes, [{<<".html">>, [<<"text/html">>]}]},
{file, <<"index.html">>}]},
{"/js/[...]", cowboy_static,
[{directory, {priv_dir, web, [<<"js">>]}},
{mimetypes, {fun mimetypes:path_to_mimes/2, default}}]},

#vim-erlang_tools

https://github.com/fishcakez/vim-erlang_tools

Vim plugin to combine vim and tmux to add support for some erlang tools to vim. Inspired by the vimux plugin. Currently erlc, ct_run, dialyzer, eunit and rebar have some support.

The plugin works by using a ct hook or an eunit listener to produce identical format to dialyzer -o outputfile. This means all tools' output can be parsed

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
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