diff --git a/apps/ethereum_jsonrpc/config/config.exs b/apps/ethereum_jsonrpc/config/config.exs index aa5ec08b49..a311073443 100644 --- a/apps/ethereum_jsonrpc/config/config.exs +++ b/apps/ethereum_jsonrpc/config/config.exs @@ -9,6 +9,11 @@ config :ethereum_jsonrpc, EthereumJSONRPC.RequestCoordinator, wait_per_timeout: :timer.seconds(20), max_jitter: :timer.seconds(2) +config :ethereum_jsonrpc, + rpc_transport: if(System.get_env("ETHEREUM_JSONRPC_JSON_RPC_TRANSPORT", "http") == "http", do: :http, else: :ipc), + ipc_path: System.get_env("IPC_PATH") + + # Add this configuration to add global RPC request throttling. # throttle_rate_limit: 250, # throttle_rolling_window_opts: [ diff --git a/apps/ethereum_jsonrpc/lib/ethereum_jsonrpc/application.ex b/apps/ethereum_jsonrpc/lib/ethereum_jsonrpc/application.ex index 8c902e8f26..38af69a337 100644 --- a/apps/ethereum_jsonrpc/lib/ethereum_jsonrpc/application.ex +++ b/apps/ethereum_jsonrpc/lib/ethereum_jsonrpc/application.ex @@ -5,7 +5,7 @@ defmodule EthereumJSONRPC.Application do use Application - alias EthereumJSONRPC.{RequestCoordinator, RollingWindow} + alias EthereumJSONRPC.{IPC, RequestCoordinator, RollingWindow} @impl Application def start(_type, _args) do @@ -17,6 +17,7 @@ defmodule EthereumJSONRPC.Application do :hackney_pool.child_spec(:ethereum_jsonrpc, recv_timeout: 60_000, timeout: 60_000, max_connections: 1000), Supervisor.child_spec({RollingWindow, [rolling_window_opts]}, id: RollingWindow.ErrorThrottle) ] + |> add_ipc_client() |> add_throttle_rolling_window(config) |> Supervisor.start_link(strategy: :one_for_one, name: EthereumJSONRPC.Supervisor) end @@ -37,4 +38,11 @@ defmodule EthereumJSONRPC.Application do children end end + + defp add_ipc_client(children) do + case Application.get_env(:ethereum_jsonrpc, :rpc_transport) do + :ipc -> [IPC.child_spec([path: Application.get_env(:ethereum_jsonrpc, :ipc_path)]) | children] + _ -> children + end + end end diff --git a/apps/ethereum_jsonrpc/lib/ethereum_jsonrpc/ipc.ex b/apps/ethereum_jsonrpc/lib/ethereum_jsonrpc/ipc.ex index 1d2d7dcea3..6c27ce4fcb 100644 --- a/apps/ethereum_jsonrpc/lib/ethereum_jsonrpc/ipc.ex +++ b/apps/ethereum_jsonrpc/lib/ethereum_jsonrpc/ipc.ex @@ -4,8 +4,19 @@ defmodule EthereumJSONRPC.IPC do # Server - def start_link(state \\ []) do - GenServer.start_link(__MODULE__, Keyword.merge(state, socket: nil)) + def child_spec(opts) do + IO.inspect(opts) + default = %{ + id: __MODULE__, + start: {__MODULE__, :start_link, opts}, + type: :worker + } + + Supervisor.child_spec(default, []) + end + + def start_link({:path, path}) do + GenServer.start_link(__MODULE__, [path: path, socket: nil]) end def init(state) do