This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
%Worker.LogEntry{event: "friend", origin: #PID<0.183.0>, time: 1}, | |
%Worker.LogEntry{event: "dear", origin: #PID<0.184.0>, time: 1}, | |
%Worker.LogEntry{event: "my", origin: #PID<0.185.0>, time: 1}, | |
%Worker.LogEntry{event: "hello", origin: #PID<0.186.0>, time: 1}, | |
%Worker.LogEntry{event: "how", origin: #PID<0.186.0>, time: 5}, | |
%Worker.LogEntry{event: "are", origin: #PID<0.185.0>, time: 6}, | |
%Worker.LogEntry{event: "in", origin: #PID<0.183.0>, time: 7}, | |
%Worker.LogEntry{event: "you", origin: #PID<0.184.0>, time: 7}, | |
%Worker.LogEntry{event: "glorious", origin: #PID<0.185.0>, time: 9}, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Worker do | |
use GenServer | |
def start_link(_) do | |
GenServer.start_link(__MODULE__, %{}, []) | |
end | |
def get_history(self) do | |
GenServer.call(self, :get_history) | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Pool do | |
def start_link(poolName, %{worker_module: worker_module, size: size}) do | |
pool_config = [ | |
{:name, {:local, poolName}}, | |
{:worker_module, worker_module}, | |
{:size, size}, | |
{:max_overflow, 0} | |
] | |
children = [ :poolboy.child_spec(poolName, pool_config) ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule PubSub do | |
alias Phoenix.PubSub | |
def start_link() do | |
import Supervisor.Spec, warn: false | |
children = [ | |
supervisor(Phoenix.PubSub.PG2, [__MODULE__, []]), | |
] | |
Supervisor.start_link(children, strategy: :one_for_one) | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PubSub.broadcast(@log_replication_topic, {:replication_log, logEntry}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
newWorkerState = %{ worker | # Update worker state | |
clock: worker.clock + 1, # - increment clock by one | |
eventLog: [logEntry | worker.eventLog] # - prepend log entry to event log | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
logEntry = %LogEntry{ | |
origin: worker.name, | |
time: worker.clock, | |
event: event | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sortedLog = Enum.sort_by(worker.eventLog, fn event -> {event.time, event.origin} end) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule LogEntry do | |
defstruct origin: nil, time: 0, event: nil | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def init(_) do | |
PubSub.subscribe(@replication_log_topic, self()) | |
{:ok, %State{ name: self(), clock: 1, eventLog: [] }} | |
end |