Skip to content

Commit ed4ee3f

Browse files
author
José Valim
committed
Merge pull request #2322 from alco/agent-doc
Proof-read Agent docs
2 parents 18aeb78 + cc8f6a4 commit ed4ee3f

File tree

1 file changed

+32
-31
lines changed

1 file changed

+32
-31
lines changed

lib/elixir/lib/agent.ex

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ defmodule Agent do
3131
@doc "Marks a task as executed"
3232
def put_task(task, project) do
3333
item = {task, project}
34-
Agent.update(__MODULE__, &Set.put(item, &1))
34+
Agent.update(__MODULE__, &Set.put(&1, item))
3535
end
3636
end
3737
@@ -54,34 +54,35 @@ defmodule Agent do
5454
Agent.get(agent, &(&1)) |> do_something_expensive()
5555
end
5656
57-
The first one blocks the agent while the second one moves copies
58-
all the state to the client and execute the operation in the client.
57+
The first one blocks the agent while the second one copies
58+
all the state to the client and executes the operation in the client.
5959
The trade-off here is exactly if the data is small enough to be
6060
sent to the client cheaply or large enough to require processing on
6161
the server (or at least some initial processing).
6262
63-
## Name registering
63+
## Name registration
6464
65-
An Agent is bound to the same name registering rules as a `GenServer`.
65+
An Agent is bound to the same name registration rules as GenServers.
6666
Read more about it in the `GenServer` docs.
6767
6868
## A word on distributed agents
6969
70-
It is important to consider the limitation of distributed agents. Agents
70+
It is important to consider the limitations of distributed agents. Agents
7171
work by sending anonymous functions in between the caller and the agent.
7272
In a distributed setup with multiple nodes, agents only work if the caller
7373
(client) and the agent have the same version of a given module.
7474
7575
This setup may exhibit issues when doing "rolling upgrades". By rolling
76-
upgrades we mean that you desire to deploy a new version your software
77-
by *shutting down* some of your nodes and replace them by nodes running
78-
a new version of the software. In this setup, part of your environment
79-
will have one version of a given module and other part another version
80-
of the same module, which may lead agents to crash. That said, if you
81-
plan to run on distributed environments, agents should likely be avoided.
82-
83-
Note however agents work fine if you want to perform hot code swap, as
84-
hot code swapping keeps both the old and new versions of a given module.
76+
upgrades we mean the following situation: you wish to deploy a new version of
77+
your software by *shutting down* some of your nodes and replacing them by
78+
nodes running a new version of the software. In this setup, part of your
79+
environment will have one version of a given module and the other part
80+
another version (the newer one) of the same module; this may cause agents to
81+
crash. That said, if you plan to run in distributed environments, agents
82+
should likely be avoided.
83+
84+
Note, however, that agents work fine if you want to perform hot code
85+
swapping, as it keeps both the old and new versions of a given module.
8586
We detail how to do hot code swapping with agents in the next section.
8687
8788
## Hot code swapping
@@ -94,7 +95,7 @@ defmodule Agent do
9495
9596
{:update, :sample, {:advanced, {Enum, :into, [%{}]}}}
9697
97-
The agent state will be added as first argument of the arguments list.
98+
The agent's state will be added to the given list as the first argument.
9899
"""
99100

100101
@typedoc "Return values of `start*` functions"
@@ -115,30 +116,30 @@ defmodule Agent do
115116
This is often used to start the agent as part of a supervision tree.
116117
117118
Once the agent is spawned, the given function is invoked and its return
118-
value is used as the agent state. Note this function does not return
119+
value is used as the agent state. Note that `start_link` does not return
119120
until the given function has returned.
120121
121122
## Options
122123
123-
The `:name` option is used for name registered as described in the module
124-
documentation. If the option `:timeout` option is present, the agent is
125-
allowed to spend the given milliseconds initializing or it will be terminated
126-
and the start function will return `{:error, :timeout}`.
124+
The `:name` option is used for registration as described in the module
125+
documentation. If the `:timeout` option is present, the agent is allowed to
126+
spend at most the given amount of milliseconds on initialization or it will
127+
be terminated and the start function will return `{:error, :timeout}`.
127128
128-
If the option `:debug` is present, the corresponding function in the
129+
If the `:debug` option is present, the corresponding function in the
129130
[`:sys` module](http://www.erlang.org/doc/man/sys.html) will be invoked.
130131
131-
If the option `:spawn_opt` is present, the given options will be passed
132+
If the `:spawn_opt` option is present, its value will be passed as options
132133
to the underlying process as in `Process.spawn/3`.
133134
134135
## Return values
135136
136-
If the server is successfully created and initialized the function returns
137+
If the server is successfully created and initialized, the function returns
137138
`{:ok, pid}`, where pid is the pid of the server. If there already exists
138-
an agent with the specified name the function returns
139+
an agent with the specified name, the function returns
139140
`{:error, {:already_started, pid}}` with the pid of that process.
140141
141-
If the given function callback fails with reason, the function returns
142+
If the given function callback fails with `reason`, the function returns
142143
`{:error, reason}`.
143144
"""
144145
@spec start_link((() -> term), GenServer.options) :: on_start
@@ -175,7 +176,7 @@ defmodule Agent do
175176
176177
The function `fun` is sent to the `agent` which invokes the function
177178
passing the agent state. The function must return a tuple with two
178-
elements, the first being the value to returned (i.e. the get value)
179+
elements, the first being the value to return (i.e. the get value)
179180
and the second one is the new state.
180181
181182
A timeout can also be specified (it has a default value of 5000).
@@ -192,7 +193,7 @@ defmodule Agent do
192193
passing the agent state. The function must return the new state.
193194
194195
A timeout can also be specified (it has a default value of 5000).
195-
This function always return `:ok`.
196+
This function always returns `:ok`.
196197
"""
197198
@spec update(agent, (state -> state)) :: :ok
198199
def update(agent, fun, timeout \\ 5000) when is_function(fun, 1) do
@@ -205,8 +206,8 @@ defmodule Agent do
205206
The function `fun` is sent to the `agent` which invokes the function
206207
passing the agent state. The function must return the new state.
207208
208-
Note this function returns `:ok` immediately, ignoring if the
209-
destination node or agent does not exist.
209+
Note that `cast` returns `:ok` immediately, regardless of whether the
210+
destination node or agent exists.
210211
"""
211212
@spec cast(agent, (state -> state)) :: :ok
212213
def cast(agent, fun) when is_function(fun, 1) do
@@ -216,7 +217,7 @@ defmodule Agent do
216217
@doc """
217218
Stops the agent.
218219
219-
Returns `:ok` if the agent is stopped in the given `timeout`.
220+
Returns `:ok` if the agent is stopped within the given `timeout`.
220221
"""
221222
@spec stop(agent, timeout) :: :ok
222223
def stop(agent, timeout \\ 5000) do

0 commit comments

Comments
 (0)