Skip to content

Commit

Permalink
Merge pull request #13 from wowica/reconnect-ondisconnect
Browse files Browse the repository at this point in the history
improve reconnections
  • Loading branch information
caike authored Feb 12, 2024
2 parents 102a2a8 + cad1741 commit 1b9a106
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ An Elixir client for [Ogmios](https://github.com/CardanoSolutions/ogmios).
Mini-Protocols supported by this library:

- [x] Chain Synchronization
- [ ] State Query (partially supported)
- [x] State Query (partially supported)
- [ ] Mempool Monitoring
- [ ] Tx Submission

Expand Down
13 changes: 12 additions & 1 deletion lib/xogmios/chain_sync.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ defmodule Xogmios.ChainSync do

@callback handle_block(map(), any()) ::
{:ok, :next_block, map()} | {:ok, map()} | {:ok, :close, map()}
@callback handle_connect(map()) ::
{:ok, map()}
@callback handle_disconnect(String.t(), map()) ::
{:ok, map()} | {:reconnect, non_neg_integer(), map()}

# The keepalive option is used to maintain the connection active.
# This is important because proxies might close idle connections after a few seconds.
Expand All @@ -15,7 +19,10 @@ defmodule Xogmios.ChainSync do
def start_link(client, opts) do
{url, opts} = Keyword.pop(opts, :url)
initial_state = Keyword.merge(opts, handler: client)
:websocket_client.start_link(url, client, initial_state, keepalive: @keepalive_in_ms)

:websocket_client.start_link({:local, client}, url, client, initial_state,
keepalive: @keepalive_in_ms
)
end

defmacro __using__(_opts) do
Expand All @@ -26,6 +33,10 @@ defmodule Xogmios.ChainSync do

require Logger

def handle_connect(state), do: {:ok, state}
def handle_disconnect(_reason, state), do: {:ok, state}
defoverridable handle_connect: 1, handle_disconnect: 2

def handle_message(%{"id" => "start"} = message, state) do
%{
"method" => "nextBlock",
Expand Down
22 changes: 19 additions & 3 deletions lib/xogmios/chain_sync/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ defmodule Xogmios.ChainSync.Connection do

alias Xogmios.ChainSync.Messages

require Logger

defmacro __using__(_opts) do
quote do
@behaviour :websocket_client

require Logger

@name __MODULE__
@reconnect_interval 5_000

def child_spec(opts) do
%{
Expand All @@ -38,12 +41,25 @@ defmodule Xogmios.ChainSync.Connection do
def onconnect(_arg, state) do
start_message = Messages.next_block_start()
:websocket_client.cast(self(), {:text, start_message})
{:ok, state}

case state.handler.handle_connect(state) do
{:ok, new_state} ->
{:ok, new_state}

_ ->
{:ok, state}
end
end

@impl true
def ondisconnect(_reason, state) do
{:ok, state}
def ondisconnect(reason, state) do
case state.handler.handle_disconnect(reason, state) do
{:ok, state} ->
{:ok, state}

{:reconnect, reconnect_interval_in_ms, new_state} ->
{:reconnect, reconnect_interval_in_ms, new_state}
end
end

@impl true
Expand Down

0 comments on commit 1b9a106

Please sign in to comment.