It's still a work in progress.
GenSever implementation which allows to asynchronously process messages from different clients.
If available in Hex, the package can be installed
by adding async_gen_server to your list of dependencies in mix.exs:
def deps do
[
{:async_gen_server, git: "https://github.com/undr/async_gen_server"}
]
endComming soon...
Create module by analogy with GenServer
defmodule TestServer do
use AsyncGenServer
def start_link(args) do
AsyncGenServer.start_link(__MODULE__, args, name: __MODULE__)
end
def init(args) do
sleep = Keyword.get(args, :sleep, 1000)
{:ok, %{sleep: sleep}}
end
def handle_async_call(message, %{sleep: sleep}) do
Process.sleep(sleep)
{:reply, {:rand.uniform(), message}}
end
endAdd it into the application supervision tree:
children = [
{TestServer, size: 50, queue_size: 50, handler_args: [sleep: 1000]}
]Emit a banch of messages to test module:
{t, r} = :timer.tc(fn ->
Enum.map(1..50, fn _ ->
Task.async(fn ->
Enum.map(1..10, fn _ ->
AsyncGenServer.call(TestServer, {:async, :test})
end)
end)
end)
|> Task.await_many(:infinity)
end)
t/1000/1000You'll get something like this: 10.027505. It took ~10 sec to process 500 messages.