parent
c010eebabf
commit
ad68c42fe0
@ -0,0 +1,28 @@ |
||||
defmodule Explorer.Chain.Events.DBSender do |
||||
@moduledoc """ |
||||
Sends events to Postgres. |
||||
""" |
||||
alias Explorer.Repo |
||||
|
||||
def send_data(event_type) do |
||||
payload = encode_payload({:chain_event, event_type}) |
||||
send_notify(payload) |
||||
end |
||||
|
||||
def send_data(_event_type, :catchup, _event_data), do: :ok |
||||
|
||||
def send_data(event_type, broadcast_type, event_data) do |
||||
payload = encode_payload({:chain_event, event_type, broadcast_type, event_data}) |
||||
send_notify(payload) |
||||
end |
||||
|
||||
defp encode_payload(payload) do |
||||
payload |
||||
|> :erlang.term_to_binary([:compressed]) |
||||
|> Base.encode64() |
||||
end |
||||
|
||||
defp send_notify(payload) do |
||||
Repo.query!("select pg_notify('chain_event', $1::text);", [payload]) |
||||
end |
||||
end |
@ -1,12 +0,0 @@ |
||||
defmodule Explorer.Chain.Events.Sender do |
||||
@moduledoc """ |
||||
Sends events to Postgres. |
||||
""" |
||||
alias Explorer.Repo |
||||
|
||||
@callback send_notify(String.t()) :: {:ok, any} |
||||
|
||||
def send_notify(payload) do |
||||
Repo.query!("select pg_notify('chain_event', $1::text);", [payload]) |
||||
end |
||||
end |
@ -1,10 +0,0 @@ |
||||
defmodule Explorer.Chain.Events.SenderMock do |
||||
@moduledoc """ |
||||
Sends events directly to Listener. |
||||
""" |
||||
alias Explorer.Chain.Events.Listener |
||||
|
||||
def send_notify(payload) do |
||||
send(Listener, {:notification, nil, nil, nil, payload}) |
||||
end |
||||
end |
@ -0,0 +1,23 @@ |
||||
defmodule Explorer.Chain.Events.SimpleSender do |
||||
@moduledoc """ |
||||
Publishes events through Registry without intermediate levels. |
||||
""" |
||||
|
||||
def send_data(event_type, broadcast_type, event_data) do |
||||
Registry.dispatch(Registry.ChainEvents, {event_type, broadcast_type}, fn entries -> |
||||
for {pid, _registered_val} <- entries do |
||||
send(pid, {:chain_event, event_type, broadcast_type, event_data}) |
||||
end |
||||
end) |
||||
end |
||||
|
||||
def send_data(_event_type, :catchup, _event_data), do: :ok |
||||
|
||||
def send_data(event_type) do |
||||
Registry.dispatch(Registry.ChainEvents, event_type, fn entries -> |
||||
for {pid, _registered_val} <- entries do |
||||
send(pid, {:chain_event, event_type}) |
||||
end |
||||
end) |
||||
end |
||||
end |
Loading…
Reference in new issue