-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2449 from poanetwork/realtime-pubsub-by-postges-n…
…otify Send events through postgres notify
- Loading branch information
Showing
11 changed files
with
136 additions
and
20 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,26 @@ | ||
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, 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 |
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,55 @@ | ||
defmodule Explorer.Chain.Events.Listener do | ||
@moduledoc """ | ||
Listens and publishes events from PG | ||
""" | ||
|
||
use GenServer | ||
|
||
alias Postgrex.Notifications | ||
|
||
def start_link(_) do | ||
GenServer.start_link(__MODULE__, "chain_event", name: __MODULE__) | ||
end | ||
|
||
def init(channel) do | ||
{:ok, pid} = | ||
:explorer | ||
|> Application.get_env(Explorer.Repo) | ||
|> Notifications.start_link() | ||
|
||
ref = Notifications.listen!(pid, channel) | ||
|
||
{:ok, {pid, ref, channel}} | ||
end | ||
|
||
def handle_info({:notification, _pid, _ref, _topic, payload}, state) do | ||
payload | ||
|> decode_payload!() | ||
|> broadcast() | ||
|
||
{:noreply, state} | ||
end | ||
|
||
# sobelow_skip ["Misc.BinToTerm"] | ||
defp decode_payload!(payload) do | ||
payload | ||
|> Base.decode64!() | ||
|> :erlang.binary_to_term() | ||
end | ||
|
||
defp broadcast({:chain_event, event_type} = event) do | ||
Registry.dispatch(Registry.ChainEvents, event_type, fn entries -> | ||
for {pid, _registered_val} <- entries do | ||
send(pid, event) | ||
end | ||
end) | ||
end | ||
|
||
defp broadcast({:chain_event, event_type, broadcast_type, _data} = event) do | ||
Registry.dispatch(Registry.ChainEvents, {event_type, broadcast_type}, fn entries -> | ||
for {pid, _registered_val} <- entries do | ||
send(pid, event) | ||
end | ||
end) | ||
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
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,21 @@ | ||
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) 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 |
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