Skip to content

Commit 6437c83

Browse files
josevalimnovaugust
andauthored
Changes to new configuration and distribution
Co-authored-by: Matt Enlow <matt@novaugust.net>
1 parent 76c3d89 commit 6437c83

1 file changed

Lines changed: 8 additions & 9 deletions

File tree

lib/elixir/pages/mix-and-otp/config-and-distribution.md

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ Run `iex -S mix` and you will see the following message printed:
6262

6363
Run tests, without killing the development server, and you will see it running on port 4040.
6464

65-
Our change was straight-forward. We used `Application.fetch_env!/2` to read the entry for `port` in `:kv`'s environment. We explicitly used `fetch_env!/2` (instead of `get_env/2` or `fetch_env`) because it will raise if the port wasn not configured (therefore our app wouldn't even boot).
65+
Our change was straight-forward. We used `Application.fetch_env!/2` to read the entry for `port` in `:kv`'s environment. We explicitly used `fetch_env!/2` (instead of `get_env/2` or `fetch_env`) because it will raise if the port was not configured (preventing the app from booting).
6666

6767
## Compile vs runtime configuration
6868

@@ -143,7 +143,7 @@ iex> flush()
143143
:ok
144144
```
145145

146-
In other words, we can spawn processes in other nodes, hold to their PIDs, and then send messages to them as if they were running on the same machine. That's the *location transparency* principle. And because everything we have built so far was built on top of messaging passing, we should be able to adjust our key-value store to become a distributed one with little work.
146+
In other words, we can spawn processes in other nodes, hold onto their PIDs, and then send messages to them as if they were running on the same machine. That's the *location transparency* principle. And because everything we have built so far was built on top of messaging passing, we should be able to adjust our key-value store to become a distributed one with little work.
147147

148148
## Distributed naming registry with `:global`
149149

@@ -175,11 +175,11 @@ iex> KV.lookup_bucket("shopping")
175175
nil
176176
```
177177

178-
It returns `nil`. However, if you run `KV.lookup_bucket("shopping")` in `bar@computer-name`, it will return the proper bucket. In other words, the nodes can communicate with each other, but buckets spawned in one node are not visibly to the other.
178+
It returns `nil`. However, if you run `KV.lookup_bucket("shopping")` in `bar@computer-name`, it will return the proper bucket. In other words, the nodes can communicate with each other, but buckets spawned in one node are not visible to the other.
179179

180180
This is because we are using [Elixir's Registry](`Registry`) to name our buckets, which is a **local** process registry. In other words, it is designed for processes running on a single node and not for distribution.
181181

182-
Luckily, Erlang ships with a distributed registry called [`:global`](`:global`), which is support by default as a naming registry. All we need to do is to replace the `via/1` function in `lib/kv.ex` from this:
182+
Luckily, Erlang ships with a distributed registry called [`:global`](`:global`), which is support by default as a naming registry. All we need to do is replace the `via/1` function in `lib/kv.ex` from this:
183183

184184
```elixir
185185
defp via(name), do: {:via, Registry, {KV, name}}
@@ -200,11 +200,11 @@ iex> KV.lookup_bucket("shopping")
200200
#PID<21821.179.0>
201201
```
202202

203-
And there you go! By simply changing which naming registry we used, we now have a distributed key value store. You can even try using `telnet` to configure to the different ports and validate that changes in one session are visible to other one. Exciting!
203+
And there you go! By simply changing which naming registry we used, we now have a distributed key value store. You can even try using `telnet` to connect to the servers on different ports and validate that changes in one session are visible in the other one. Exciting!
204204

205205
## Node discovery and dependencies
206206

207-
There is one essential ingredient to wrap up our distributed key-value store. In other for the `:global` registry to work, we need to make sure the nodes are connected to each other. When we run `:erpc` call passing the node name:
207+
There is one essential ingredient to wrap up our distributed key-value store. In order for the `:global` registry to work, we need to make sure the nodes are connected to each other. When we run `:erpc` call passing the node name:
208208

209209
```elixir
210210
:erpc.call(:"bar@computer-name", KV, :create_bucket, ["shopping"])
@@ -260,14 +260,13 @@ Open up `config/runtime.exs` and add this to the bottom:
260260
```elixir
261261
nodes =
262262
System.get_env("NODES", "")
263-
|> String.split(",")
264-
|> Enum.reject(& &1 == "")
263+
|> String.split(",", trim: true)
265264
|> Enum.map(&String.to_atom/1)
266265

267266
config :kv, :nodes, nodes
268267
```
269268

270-
We fetch the environment variable, split it on ",", discard all empty strings, and then convert each entry to an atom, as node names are atoms.
269+
We fetch the environment variable, split it on "," while discarding all empty strings, and then convert each entry to an atom, as node names are atoms.
271270

272271
Now, in your `start/2` callback, we will add this to of the `start/2` function:
273272

0 commit comments

Comments
 (0)