Skip to content

undr/async_gen_server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AsyncGenServer

It's still a work in progress.

GenSever implementation which allows to asynchronously process messages from different clients.

Installation

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"}
  ]
end

Usage

Comming soon...

Example

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
end

Add 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/1000

You'll get something like this: 10.027505. It took ~10 sec to process 500 messages.

About

GenSever implementation which allows to asynchronously process messages from different clients.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages