-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into rework-publish
- Loading branch information
Showing
11 changed files
with
526 additions
and
47 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
defmodule Neurow.ReceiverShard do | ||
use GenServer | ||
|
||
def start_link(shard) do | ||
GenServer.start_link(__MODULE__, shard, name: build_name(shard)) | ||
end | ||
|
||
def build_name(shard) do | ||
String.to_atom("receiver_#{shard}") | ||
end | ||
|
||
defp create_table(table_name) do | ||
:ets.new(table_name, [:duplicate_bag, :protected, :named_table]) | ||
end | ||
|
||
def table_name(shard, sub_shard) do | ||
String.to_atom("history_#{shard}_#{sub_shard}") | ||
end | ||
|
||
@impl true | ||
def init(shard) do | ||
:ok = Phoenix.PubSub.subscribe(Neurow.PubSub, Neurow.ReceiverShardManager.build_topic(shard)) | ||
table_0 = table_name(shard, 0) | ||
table_1 = table_name(shard, 1) | ||
create_table(table_0) | ||
create_table(table_1) | ||
{:ok, {table_0, table_1}} | ||
end | ||
|
||
# Read from the current process, not from GenServer process | ||
def get_history(shard, topic) do | ||
table_0 = table_name(shard, 0) | ||
table_1 = table_name(shard, 1) | ||
|
||
result = :ets.lookup(table_0, topic) ++ :ets.lookup(table_1, topic) | ||
|
||
Enum.sort(result, fn {_, {id_0, _}}, {_, {id_1, _}} -> id_0 < id_1 end) | ||
end | ||
|
||
@impl true | ||
def handle_info({:pubsub_message, user_topic, message}, {table_0, table_1}) do | ||
:ok = | ||
Phoenix.PubSub.local_broadcast( | ||
Neurow.PubSub, | ||
user_topic, | ||
{:pubsub_message, message} | ||
) | ||
|
||
true = :ets.insert(table_1, {user_topic, message}) | ||
{:noreply, {table_0, table_1}} | ||
end | ||
|
||
@impl true | ||
def handle_info({:rotate}, {table_0, table_1}) do | ||
:ets.delete_all_objects(table_0) | ||
{:noreply, {table_1, table_0}} | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
defmodule Neurow.ReceiverShardManager do | ||
require Logger | ||
use GenServer | ||
|
||
@shards 8 | ||
|
||
def start_link(opts) do | ||
GenServer.start_link(__MODULE__, opts, name: __MODULE__) | ||
end | ||
|
||
@impl true | ||
def init([history_min_duration]) do | ||
Process.send_after( | ||
self(), | ||
{:rotate_trigger, history_min_duration}, | ||
history_min_duration * 1_000 | ||
) | ||
|
||
{:ok, nil} | ||
end | ||
|
||
@impl true | ||
def handle_info({:rotate_trigger, history_min_duration}, state) do | ||
Process.send_after( | ||
self(), | ||
{:rotate_trigger, history_min_duration}, | ||
history_min_duration * 1_000 | ||
) | ||
|
||
rotate() | ||
{:noreply, state} | ||
end | ||
|
||
def all_pids(fun) do | ||
Enum.map(0..(@shards - 1), fn shard -> | ||
fun.({shard, Neurow.ReceiverShard.build_name(shard)}) | ||
end) | ||
end | ||
|
||
def rotate do | ||
Stats.inc_history_rotate() | ||
|
||
all_pids(fn {_, pid} -> | ||
send(pid, {:rotate}) | ||
end) | ||
end | ||
|
||
@impl true | ||
def handle_call({:rotate}, _, opts) do | ||
rotate() | ||
{:reply, :ok, opts} | ||
end | ||
|
||
def build_topic(shard) do | ||
"__topic#{shard}" | ||
end | ||
|
||
# Read from the current process, not from GenServer process | ||
def get_history(topic) do | ||
Neurow.ReceiverShard.get_history(shard_from_topic(topic), topic) | ||
end | ||
|
||
def create_receivers() do | ||
all_pids(fn {shard, pid} -> | ||
Supervisor.child_spec({Neurow.ReceiverShard, shard}, id: pid) | ||
end) | ||
end | ||
|
||
def broadcast(topic, message) do | ||
broadcast_topic = broadcast_topic(topic) | ||
|
||
Phoenix.PubSub.broadcast!( | ||
Neurow.PubSub, | ||
broadcast_topic, | ||
{:pubsub_message, topic, message} | ||
) | ||
end | ||
|
||
defp broadcast_topic(topic) do | ||
build_topic(shard_from_topic(topic)) | ||
end | ||
|
||
defp shard_from_topic(topic) do | ||
:erlang.phash2(topic, @shards) | ||
end | ||
end |
This file contains 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
Oops, something went wrong.