Created
May 9, 2014 19:18
-
-
Save patrickdet/a6fcc129e3f997db6f5a to your computer and use it in GitHub Desktop.
Elixir XGen GenServer semantics discussion
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| defmodule FooServer do | |
| @behaviour :gen_server | |
| @server_name {:global, :foo_server} | |
| # Client | |
| def do_it do | |
| try do | |
| :gen_server.call @server_name, :foo | |
| catch | |
| :exit, reason -> {:error, reason} | |
| end | |
| end | |
| def do_it! do | |
| :gen_server.call @server_name, :foo | |
| end | |
| # Server | |
| def start do | |
| :gen_server.start_link(@server_name, __MODULE__, [], []) | |
| end | |
| def init(args) do | |
| {:ok, args} | |
| end | |
| def handle_call(msg, _from, state) do | |
| :timer.sleep(10000) | |
| {:reply, {:ok, :my_response}, state} | |
| end | |
| def handle_info(msg, state) do | |
| {:noreply, state} | |
| end | |
| def handle_cast(msg, state) do | |
| {:stop, {:bad_cast, msg}, state} | |
| end | |
| def terminate(_reason, _state) do | |
| :ok | |
| end | |
| def code_change(_old, state, _extra) do | |
| {:ok, state} | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment