GenServer.Proxy behaviour (GenServer Proxy v0.1.58)

View Source

Invokes the following functions with a GenServer registered via a server ID:

Will wait a bit if the GenServer is not yet registered on restarts. Note this is an assumption as the GenServer may have never started.

Summary

Types

Server ID

Callbacks

Called to convert the server_id into a server name.

Called when the server remains unregistered despite waiting a bit. Should serve to print a relevant message about the failed request.

Functions

Either aliases GenServer.Proxy (this module) and requires the alias or imports GenServer.Proxy. In the latter case, you could instead simply import GenServer.Proxy.

Makes a synchronous call to the GenServer registered via server_id. Will wait a bit if the GenServer is not yet registered on restarts.

Sends an async request to the GenServer registered via server_id. Will wait a bit if the GenServer is not yet registered on restarts.

Synchronously stops the GenServer registered via server_id. Will wait a bit if the GenServer is not yet registered on restarts.

Types

server_id()

@type server_id() :: term()

Server ID

Callbacks

server_name(server_id)

@callback server_name(server_id()) :: GenServer.name()

Called to convert the server_id into a server name.

Examples

@impl GenServer.Proxy
def server_name(game_name),
  do: {:via, Registry, {:registry, game_name}}

@impl GenServer.Proxy
def server_name(game_name),
  do: {:global, {GameServer, game_name}}

server_unregistered(server_id)

@callback server_unregistered(server_id()) :: term()

Called when the server remains unregistered despite waiting a bit. Should serve to print a relevant message about the failed request.

Examples

@impl GenServer.Proxy
def server_unregistered(game_name),
  do: :ok = IO.puts("Game #{game_name} not started.")

Functions

__using__(options)

(macro)

Either aliases GenServer.Proxy (this module) and requires the alias or imports GenServer.Proxy. In the latter case, you could instead simply import GenServer.Proxy.

Examples

use GenServer.Proxy, alias: Proxy

use GenServer.Proxy

import GenServer.Proxy

call(server_id, request, timeout \\ 5000, module \\ nil)

(macro)

Makes a synchronous call to the GenServer registered via server_id. Will wait a bit if the GenServer is not yet registered on restarts.

The given module (or by default <caller's_module>.GenServerProxy) must implement the 2 callbacks of GenServer.Proxy (this module).

Examples

# Assuming the following callback module:

defmodule Game.Engine.GenServerProxy do
  @behaviour GenServer.Proxy

  @impl GenServer.Proxy
  def server_name(game_name),
    do: {:via, Registry, {:registry, game_name}}

  @impl GenServer.Proxy
  def server_unregistered(game_name),
    do: :ok = IO.puts("Game #{game_name} not started.")
end

# We could use the call macro like so:

defmodule Game.Engine do
  use GenServer.Proxy

  def summary(game_name), do: call(game_name, :summary)
  ...
end

cast(server_id, request, module \\ nil)

(macro)

Sends an async request to the GenServer registered via server_id. Will wait a bit if the GenServer is not yet registered on restarts.

The given module (or by default <caller's_module>.GenServerProxy) must implement the 2 callbacks of GenServer.Proxy (this module).

stop(server_id, reason \\ :normal, timeout \\ :infinity, module \\ nil)

(macro)

Synchronously stops the GenServer registered via server_id. Will wait a bit if the GenServer is not yet registered on restarts.

The given module (or by default <caller's_module>.GenServerProxy) must implement the 2 callbacks of GenServer.Proxy (this module).