Skip to content
This repository has been archived by the owner on Jun 10, 2022. It is now read-only.

Commit

Permalink
Merge pull request #1 from ertgl/replication_imp
Browse files Browse the repository at this point in the history
Make Replicator.GenServer returns node names with results.
  • Loading branch information
ertgl committed Dec 20, 2017
2 parents fc84ece + af23086 commit fff5445
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,19 @@ defmodule Storage.KV do
end

def set(key, value) do
Distributed.Replicator.GenServer.cast(__MODULE__.process_id(), {:set, key, value})
|> List.first()
{_node_name, result} = Distributed.Replicator.GenServer.cast(__MODULE__.process_id(), {:set, key, value})
|> List.first()
result
end

def has?(key) do
Distributed.Scaler.GenServer.call(__MODULE__.process_id(), {:has, key})
end

def pop(key, default \\ nil) do
Distributed.Replicator.GenServer.call(__MODULE__.process_id(), {:pop, key, default})
|> List.first()
{_node_name, result} = Distributed.Replicator.GenServer.call(__MODULE__.process_id(), {:pop, key, default})
|> List.first()
result
end

end
Expand Down
12 changes: 9 additions & 3 deletions lib/distributed/replicator/gen_server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ defmodule Distributed.Replicator.GenServer do
"""
@spec info(dest :: pid | port | atom, msg :: any, opts :: [any]) :: any
def info(dest, msg, opts \\ []) do
Distributed.Parallel.map(Distributed.Node.list(opts), &(send({dest, &1}, msg)))
Distributed.Parallel.map(Distributed.Node.list(opts), fn node_name ->
{node_name, send({dest, node_name}, msg)}
end)
end

@doc """
Expand All @@ -39,15 +41,19 @@ defmodule Distributed.Replicator.GenServer do
@spec call(server :: atom, term, opts :: [any]) :: [term]
def call(server, term, opts \\ []) do
timeout = Keyword.get(opts, :timeout, :infinity)
Distributed.Parallel.map(Distributed.Node.list(opts), &(GenServer.call({server, &1}, term, timeout)))
Distributed.Parallel.map(Distributed.Node.list(opts), fn node_name ->
{node_name, GenServer.call({server, node_name}, term, timeout)}
end)
end

@doc """
Sends asynchronous requests to the servers on nodes. See `GenServer.cast/2`
"""
@spec cast(server :: atom, term :: term, opts :: [any]) :: [term]
def cast(server, term, opts \\ []) do
Distributed.Parallel.map(Distributed.Node.list(opts), &(GenServer.cast({server, &1}, term)))
Distributed.Parallel.map(Distributed.Node.list(opts), fn node_name ->
{node_name, GenServer.cast({server, node_name}, term)}
end)
end

end
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ defmodule Distributed.Mixfile do
description: description(),
package: package(),
app: :distributed,
version: "0.1.0",
version: "0.1.2",
elixir: "~> 1.4",
build_embedded: Mix.env() == :prod,
start_permanent: Mix.env() == :prod,
Expand Down

0 comments on commit fff5445

Please sign in to comment.