use poolboy for concurrent request execution

pull/2791/head
Ayrat Badykov 5 years ago
parent 022086dce8
commit 6769f5b31c
No known key found for this signature in database
GPG Key ID: B44668E265E9396F
  1. 19
      apps/ethereum_jsonrpc/lib/ethereum_jsonrpc/application.ex
  2. 22
      apps/ethereum_jsonrpc/lib/ethereum_jsonrpc/ipc.ex
  3. 3
      apps/ethereum_jsonrpc/mix.exs

@ -41,8 +41,23 @@ defmodule EthereumJSONRPC.Application do
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
:ipc ->
[
:poolboy.child_spec(:worker, poolboy_config(), path: Application.get_env(:ethereum_jsonrpc, :ipc_path))
| children
]
_ ->
children
end
end
defp poolboy_config do
[
{:name, {:local, :ipc_worker}},
{:worker_module, IPC},
{:size, 10},
{:max_overflow, 5}
]
end
end

@ -6,18 +6,8 @@ defmodule EthereumJSONRPC.IPC do
# Server
def child_spec(opts) do
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], name: __MODULE__)
def start_link(opts) do
GenServer.start_link(__MODULE__, Keyword.merge(opts, socket: nil))
end
def init(state) do
@ -72,8 +62,8 @@ defmodule EthereumJSONRPC.IPC do
# Client
def json_rpc(payload, _opts) do
with {:ok, response} <- post(__MODULE__, Jason.encode!(payload)),
def request(pid, payload) do
with {:ok, response} <- post(pid, Jason.encode!(payload)),
{:ok, decoded_body} <- Jason.decode(response) do
case decoded_body do
%{"error" => error} ->
@ -97,4 +87,8 @@ defmodule EthereumJSONRPC.IPC do
{:error, error} -> {:error, error}
end
end
def json_rpc(payload, _opts) do
:poolboy.transaction(:ipc_worker, fn pid -> request(pid, payload) end, 600_000)
end
end

@ -91,7 +91,8 @@ defmodule EthereumJsonrpc.MixProject do
{:websocket_client, "~> 1.3"},
{:decimal, "~> 1.0"},
{:decorator, "~> 1.2"},
{:hackney, "~> 1.15.2"}
{:hackney, "~> 1.15.2"},
{:poolboy, "~> 1.5.2"}
]
end
end

Loading…
Cancel
Save