Skip to content

Commit

Permalink
feat: set a ping_interval from config, defaulting to 300 seconds
Browse files Browse the repository at this point in the history
  • Loading branch information
zachdaniel committed Apr 18, 2019
1 parent a177310 commit e4d0fef
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- [#1696](https://github.com/poanetwork/blockscout/pull/1696) - full-text search by tokens
- [#1742](https://github.com/poanetwork/blockscout/pull/1742) - Support RSK
- [#1777](https://github.com/poanetwork/blockscout/pull/1777) - show ERC-20 token transfer info on transaction page
- [#1770](https://github.com/poanetwork/blockscout/pull/1770) - set a websocket keepalive from config

### Fixes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ defmodule EthereumJSONRPC.WebSocket.WebSocketClient do

@impl WebSocket
# only allow secure WSS
def start_link(["wss://" <> _ = url, gen_fsm_options]) when is_list(gen_fsm_options) do
def start_link(["wss://" <> _ = url, websocket_opts, gen_fsm_options]) when is_list(gen_fsm_options) do
keepalive = websocket_opts[:keepalive]

fsm_name =
case Keyword.fetch(gen_fsm_options, :name) do
{:ok, name} when is_atom(name) -> {:local, name}
Expand All @@ -68,6 +70,7 @@ defmodule EthereumJSONRPC.WebSocket.WebSocketClient do
__MODULE__,
url,
ssl_verify: :verify_peer,
keepalive: keepalive,
socket_opts: [
cacerts: :certifi.cacerts(),
depth: 99,
Expand All @@ -78,7 +81,9 @@ defmodule EthereumJSONRPC.WebSocket.WebSocketClient do
)
end

def start_link(["ws://" <> _ = url, gen_fsm_options]) when is_list(gen_fsm_options) do
def start_link(["ws://" <> _ = url, websocket_opts, gen_fsm_options]) when is_list(gen_fsm_options) do
keepalive = websocket_opts[:keepalive]

fsm_name =
case Keyword.fetch(gen_fsm_options, :name) do
{:ok, name} when is_atom(name) -> {:local, name}
Expand All @@ -90,7 +95,7 @@ defmodule EthereumJSONRPC.WebSocket.WebSocketClient do
url,
__MODULE__,
url,
[]
keepalive: keepalive
)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ defmodule EthereumJSONRPC.WebSocket.WebSocketClientTest do

port = :ranch.get_port(EthereumJSONRPC.WebSocket.Cowboy)

pid = start_supervised!({WebSocketClient, ["ws://localhost:#{port}/websocket", []]})
pid = start_supervised!({WebSocketClient, ["ws://localhost:#{port}/websocket", [keepalive: :timer.hours(1)], []]})

%{pid: pid, port: port}
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ defmodule EthereumJSONRPC.WebSocket.Case.Geth do
def setup do
url = "wss://mainnet.infura.io/ws/8lTvJTKmHPCHazkneJsY"
web_socket_module = EthereumJSONRPC.WebSocket.WebSocketClient
web_socket = start_supervised!({web_socket_module, [url, []]})
web_socket = start_supervised!({web_socket_module, [url, [keepalive: :timer.minutes(10)], []]})

%{
block_interval: 25_000,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ defmodule EthereumJSONRPC.WebSocket.Case.Mox do
Supervisor.child_spec(
%{
id: web_socket_module,
start: {web_socket_module, :start_link, arguments}
start: {web_socket_module, :start_link, [arguments]}
},
[]
)
Expand All @@ -29,7 +29,7 @@ defmodule EthereumJSONRPC.WebSocket.Case.Mox do
end)

url = "wss://example.com/ws"
web_socket = start_supervised!({web_socket_module, [url]})
web_socket = start_supervised!({web_socket_module, [url, [keepalive: :timer.minutes(10)]]})

%{
block_interval: @block_interval,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ defmodule EthereumJSONRPC.WebSocket.Case.Parity do
def setup do
url = "ws://3.85.253.242:8546"
web_socket_module = EthereumJSONRPC.WebSocket.WebSocketClient
web_socket = start_supervised!({web_socket_module, [url, []]})
web_socket = start_supervised!({web_socket_module, [url, [keepalive: :timer.minutes(10)], []]})

%{
block_interval: 5_000,
Expand Down
4 changes: 4 additions & 0 deletions apps/indexer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ Memory usage is checked once per minute. If the soft-limit is reached, the shri

If all queues are at their minimum size, then no more memory can be reclaimed and an error will be logged.

## Websocket Keepalive

This defaults to 150 seconds, but it can be set via adding a configuration to `subscribe_named_arguments` in the appropriate config file (indexer/config/<env>/<variant>.exs) called `:keep_alive`. The value is an integer representing milliseconds.

## Testing

### Parity
Expand Down
4 changes: 3 additions & 1 deletion apps/indexer/lib/indexer/block/realtime/supervisor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ defmodule Indexer.Block.Realtime.Supervisor do
transport_options = %EthereumJSONRPC.WebSocket{transport_options | web_socket_options: web_socket_options}
%EthereumJSONRPC.WebSocket{url: url, web_socket: web_socket_module} = transport_options

keepalive = Keyword.get(subscribe_named_arguments, :keep_alive, :timer.seconds(150))

block_fetcher_subscribe_named_arguments =
put_in(subscribe_named_arguments[:transport_options], transport_options)

[
{Task.Supervisor, name: Indexer.Block.Realtime.TaskSupervisor},
{web_socket_module, [url, [name: web_socket]]},
{web_socket_module, [url, [keepalive: keepalive], [name: web_socket]]},
{Indexer.Block.Realtime.Fetcher,
[
%{block_fetcher: block_fetcher, subscribe_named_arguments: block_fetcher_subscribe_named_arguments},
Expand Down

0 comments on commit e4d0fef

Please sign in to comment.