Skip to content

Latest commit

 

History

History
68 lines (48 loc) · 2.04 KB

README.md

File metadata and controls

68 lines (48 loc) · 2.04 KB

GenServerAsync


Elixir CI Hex pm Coverage Status

GenServerAsync adds a call_async method that allows you to run GenServer.call/3 method in a separate non-blocking GenServer process.

Extends the GenServer behavior, adds a .call_async(server_pid, message) method to it, which allows you to divide the blocking handle_call into three callbacks. handle_call(message, from, state)

handle_call_async(message, call_state) – asynchronously call this

handle_cast_async(message, call_async_result, state) -

# Start the server
{:ok, pid} = GenServerAsync.start_link(ExampleServer, state)
GenServerAsync.call_async(pid, message)

defmodule ExampleServer do
  use GenServerAsync

  # Makes a synchronous call to the server and waits for its reply.
  def handle_call(message, from, state) do
    if could_reply_immediately?(message) do
      {:reply, response, updated_state}
    else 

      # update state and calls `handle_call_async` asynchronously
      {:no_reply, updated_state}
    end
  end

  @doc """

  """
  def handle_call_async(message, call_state) do
    call_async_result = ... # complex  calculation
    {:reply, call_async_result}
  end

  # sync GenServer state after handle_call_async if needed
  def handle_cast_async(_message, call_async_result, state) do
    {:noreply, state}
  end 
  
end

Installation

It's available in Hex, the package can be installed as: Add gen_server_async to your list of dependencies in mix.exs:

def deps do
  [{:gen_server_async, ">= 0.0.1"}]
end

Documentation can be found at https://hexdocs.pm/gen_server_async.

License

This software is licensed under the MIT license.