GenServer API documentation

From Elixir Wiki
Jump to navigation Jump to search

GenServer API documentation[edit]

The GenServer API in Elixir provides a set of functions and callbacks for building concurrent and fault-tolerant systems. It is a fundamental part of the Elixir standard library and offers a simple and powerful abstraction for managing state and handling asynchronous messages.

Callbacks[edit]

The GenServer API includes the following callbacks that can be implemented in a separate module to define the behavior of a GenServer:

  • `init/1`: Initializes the state of the GenServer.
  • `handle_call/3`: Handles synchronous calls from client processes.
  • `handle_cast/2`: Handles asynchronous messages.
  • `handle_info/2`: Handles non-call or non-cast messages.
  • `terminate/2`: Cleans up the GenServer before it is terminated.
  • `code_change/3`: Handles code changes and migration.

Starting a GenServer[edit]

To start a GenServer, the `GenServer.start/3` function is used:

```elixir GenServer.start(module, arg1, arg2) ```

Where `module` is the module that implements the GenServer behavior and `arg1` and `arg2` are optional arguments passed to the `init/1` callback.

Making Calls to a GenServer[edit]

To make synchronous calls to a GenServer, the `GenServer.call/2` function is used:

```elixir GenServer.call(server, message) ```

Where `server` is the PID of the GenServer process and `message` is the message to be sent. The call will block until a response is received.

Casting Messages to a GenServer[edit]

To send asynchronous messages to a GenServer, the `GenServer.cast/2` function is used:

```elixir GenServer.cast(server, message) ```

Where `server` is the PID of the GenServer process and `message` is the message to be sent. The cast will not block and the message will be handled asynchronously.

Handling GenServer Events[edit]

When a message is received, the appropriate callback function is invoked based on its type. Here is an overview of the callback functions:

  • `handle_call/3`: Receives a synchronous call and returns a reply.
  • `handle_cast/2`: Handles an asynchronous message.
  • `handle_info/2`: Handles non-call or non-cast messages.
  • `terminate/2`: Cleans up the GenServer before termination.
  • `code_change/3`: Handles code changes and migration.

Error Handling[edit]

To handle errors and exceptions, the `try...catch` construct can be used within the callback functions. Additionally, the `{:reply, response, state}` tuple can be returned to indicate an error condition.

Example Usage[edit]

```elixir defmodule MyServer do

 use GenServer
 def start_link(arg) do
   GenServer.start_link(__MODULE__, arg)
 end
 def init(arg) do
   {:ok, arg}
 end
 def handle_call(:get_state, _from, state) do
   {:reply, state, state}
 end
 def handle_cast({:set_state, new_state}, state) do
   {:noreply, new_state}
 end

end ```

This is a simple example of a GenServer implementation. It initializes its state with the provided argument in the `init/1` callback. It handles synchronous calls to retrieve the state and asynchronous casts to update the state.

Conclusion[edit]

The GenServer API is a powerful tool for building concurrent, fault-tolerant systems in Elixir. By implementing the necessary callbacks, you can define the behavior of your GenServer and handle various types of messages. Understanding the GenServer API is essential for developing robust Elixir applications.