@@ -31,7 +31,7 @@ defmodule Agent do
31
31
@doc "Marks a task as executed"
32
32
def put_task(task, project) do
33
33
item = {task, project}
34
- Agent.update(__MODULE__, &Set.put(item, &1 ))
34
+ Agent.update(__MODULE__, &Set.put(&1, item ))
35
35
end
36
36
end
37
37
@@ -54,34 +54,35 @@ defmodule Agent do
54
54
Agent.get(agent, &(&1)) |> do_something_expensive()
55
55
end
56
56
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.
59
59
The trade-off here is exactly if the data is small enough to be
60
60
sent to the client cheaply or large enough to require processing on
61
61
the server (or at least some initial processing).
62
62
63
- ## Name registering
63
+ ## Name registration
64
64
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 .
66
66
Read more about it in the `GenServer` docs.
67
67
68
68
## A word on distributed agents
69
69
70
- It is important to consider the limitation of distributed agents. Agents
70
+ It is important to consider the limitations of distributed agents. Agents
71
71
work by sending anonymous functions in between the caller and the agent.
72
72
In a distributed setup with multiple nodes, agents only work if the caller
73
73
(client) and the agent have the same version of a given module.
74
74
75
75
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.
85
86
We detail how to do hot code swapping with agents in the next section.
86
87
87
88
## Hot code swapping
@@ -94,7 +95,7 @@ defmodule Agent do
94
95
95
96
{:update, :sample, {:advanced, {Enum, :into, [%{}]}}}
96
97
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 .
98
99
"""
99
100
100
101
@ typedoc "Return values of `start*` functions"
@@ -115,30 +116,30 @@ defmodule Agent do
115
116
This is often used to start the agent as part of a supervision tree.
116
117
117
118
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
119
120
until the given function has returned.
120
121
121
122
## Options
122
123
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}`.
127
128
128
- If the option `:debug` is present, the corresponding function in the
129
+ If the `:debug` option is present, the corresponding function in the
129
130
[`:sys` module](http://www.erlang.org/doc/man/sys.html) will be invoked.
130
131
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
132
133
to the underlying process as in `Process.spawn/3`.
133
134
134
135
## Return values
135
136
136
- If the server is successfully created and initialized the function returns
137
+ If the server is successfully created and initialized, the function returns
137
138
`{: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
139
140
`{:error, {:already_started, pid}}` with the pid of that process.
140
141
141
- If the given function callback fails with reason, the function returns
142
+ If the given function callback fails with ` reason` , the function returns
142
143
`{:error, reason}`.
143
144
"""
144
145
@ spec start_link ( ( ( ) -> term ) , GenServer . options ) :: on_start
@@ -175,7 +176,7 @@ defmodule Agent do
175
176
176
177
The function `fun` is sent to the `agent` which invokes the function
177
178
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)
179
180
and the second one is the new state.
180
181
181
182
A timeout can also be specified (it has a default value of 5000).
@@ -192,7 +193,7 @@ defmodule Agent do
192
193
passing the agent state. The function must return the new state.
193
194
194
195
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`.
196
197
"""
197
198
@ spec update ( agent , ( state -> state ) ) :: :ok
198
199
def update ( agent , fun , timeout \\ 5000 ) when is_function ( fun , 1 ) do
@@ -205,8 +206,8 @@ defmodule Agent do
205
206
The function `fun` is sent to the `agent` which invokes the function
206
207
passing the agent state. The function must return the new state.
207
208
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 .
210
211
"""
211
212
@ spec cast ( agent , ( state -> state ) ) :: :ok
212
213
def cast ( agent , fun ) when is_function ( fun , 1 ) do
@@ -216,7 +217,7 @@ defmodule Agent do
216
217
@ doc """
217
218
Stops the agent.
218
219
219
- Returns `:ok` if the agent is stopped in the given `timeout`.
220
+ Returns `:ok` if the agent is stopped within the given `timeout`.
220
221
"""
221
222
@ spec stop ( agent , timeout ) :: :ok
222
223
def stop ( agent , timeout \\ 5000 ) do
0 commit comments