concept server in category elixir

This is an excerpt from Manning's book Elixir in Action.
iex(1)> server = Server.start() iex(2)> Enum.each( 1..5, fn i -> spawn(fn -> #1 IO.puts("Sending msg ##{i}") response = Server.send_msg(server, i) #2 IO.puts("Response: #{response}") end) end ) #1 Spawns a concurrent client #2 Sends a message to the server
Because you’re introducing the second request type, you need to change the format of messages that are passed between client processes and the server. This will allow you to determine the request type in the server process and handle different types of requests in different ways.
This can be as simple as including the request-type information in the tuple being passed from the client process to the server, as shown next.
Figure 9.5 Supervising to-do servers
![]()

This is an excerpt from Manning's book The Little Elixir & OTP Guidebook.
GenServer.call/3 makes a synchronous request to the server. This means a reply from the server is expected. The sibling to GenServer.call/3 is GenServer.cast/2, which makes an asynchronous request to the server. We’ll take a look at that shortly. For now, the following listing shows the implementation of handle_call/3 for the {:location, location} message.
Listing 7.1. Handling the consumer :DOWN message (lib/pooly/server.ex)
defmodule Pooly.Server do ############# # Callbacks # ############# def handle_info({:DOWN, ref, _, _, _}, state = %{monitors: monitors, workers: workers}) do case :ets.match(monitors, {:"$1", ref}) do [[pid]] -> true = :ets.delete(monitors, pid) new_state = %{state | workers: [pid|workers]} #1 {:noreply, new_state} [[]] -> {:noreply, state} end end end
If the server crashes, should it bring down the worker process? It should, because otherwise, the state of the server will be inconsistent with the pool’s actual state. On the other hand, when a worker process crashes, should it bring down the server process? Of course not! What does this mean for you? Well, because of the bidirectional dependency, you should use links. But because the server should not crash when a worker process crashes, the server process should trap exits, as shown in the following listing.
Listing 7.2. Making the server process trap exits (lib/pooly/server.ex)
defmodule Pooly.Server do ############# # Callbacks # ############# def init([sup, pool_config]) when is_pid(sup) do Process.flag(:trap_exit, true) #1 monitors = :ets.new(:monitors, [:private]) init(pool_config, %State{sup: sup, monitors: monitors}) end end

This is an excerpt from Manning's book Elixir in Action.
But the language gives you tools to further eliminate whatever you may regard as noise and duplication. For example, I have developed my own Elixir library called ExActor that makes the server process definition dense, as shown next.
Because you’re introducing the second request type, you need to change the format of messages that are passed between client processes and the server. This will allow you to determine the request type in the server process and handle different types of requests in different ways.
This can be as simple as including the request-type information in the tuple being passed from the client process to the server, as shown next.
iex(2)> bobs_list = Todo.Cache.server_process("Bob's list") Starting to-do server for Bob's list. #PID<0.64.0> iex(3)> Todo.Server.entries(bobs_list, {2013, 12, 19}) []