Skip to content

Commit c7a54ae

Browse files
committed
Improve docs on DynamicSupervisor blocking operations
1 parent 1a855ae commit c7a54ae

File tree

1 file changed

+16
-64
lines changed

1 file changed

+16
-64
lines changed

lib/elixir/lib/dynamic_supervisor.ex

Lines changed: 16 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -137,67 +137,6 @@ defmodule DynamicSupervisor do
137137
138138
A supervisor is bound to the same name registration rules as a `GenServer`.
139139
Read more about these rules in the documentation for `GenServer`.
140-
141-
## Migrating from Supervisor's :simple_one_for_one
142-
143-
In case you were using the deprecated `:simple_one_for_one` strategy from
144-
the `Supervisor` module, you can migrate to the `DynamicSupervisor` in
145-
few steps.
146-
147-
Imagine the given "old" code:
148-
149-
defmodule MySupervisor do
150-
use Supervisor
151-
152-
def start_link(init_arg) do
153-
Supervisor.start_link(__MODULE__, init_arg, name: __MODULE__)
154-
end
155-
156-
def start_child(foo, bar, baz) do
157-
# This will start child by calling MyWorker.start_link(init_arg, foo, bar, baz)
158-
Supervisor.start_child(__MODULE__, [foo, bar, baz])
159-
end
160-
161-
@impl true
162-
def init(init_arg) do
163-
children = [
164-
# Or the deprecated: worker(MyWorker, [init_arg])
165-
%{id: MyWorker, start: {MyWorker, :start_link, [init_arg]}}
166-
]
167-
168-
Supervisor.init(children, strategy: :simple_one_for_one)
169-
end
170-
end
171-
172-
It can be upgraded to the DynamicSupervisor like this:
173-
174-
defmodule MySupervisor do
175-
use DynamicSupervisor
176-
177-
def start_link(init_arg) do
178-
DynamicSupervisor.start_link(__MODULE__, init_arg, name: __MODULE__)
179-
end
180-
181-
def start_child(foo, bar, baz) do
182-
# If MyWorker is not using the new child specs, we need to pass a map:
183-
# spec = %{id: MyWorker, start: {MyWorker, :start_link, [foo, bar, baz]}}
184-
spec = {MyWorker, foo: foo, bar: bar, baz: baz}
185-
DynamicSupervisor.start_child(__MODULE__, spec)
186-
end
187-
188-
@impl true
189-
def init(init_arg) do
190-
DynamicSupervisor.init(
191-
strategy: :one_for_one,
192-
extra_arguments: [init_arg]
193-
)
194-
end
195-
end
196-
197-
The difference is that the `DynamicSupervisor` expects the child specification
198-
at the moment `start_child/2` is called, and no longer on the init callback.
199-
If there are any initial arguments given on initialization, such as `[initial_arg]`,
200-
it can be given in the `:extra_arguments` flag on `DynamicSupervisor.init/1`.
201140
"""
202141

203142
@behaviour GenServer
@@ -233,9 +172,10 @@ defmodule DynamicSupervisor do
233172
@typedoc """
234173
Return values of `start_child` functions.
235174
236-
Unlike `Supervisor`, this module ignores the child spec ids, so
237-
`{:error, {:already_started, pid}}` is not returned for child specs given with the same id.
238-
`{:error, {:already_started, pid}}` is returned however if a duplicate name is used when using
175+
Unlike `Supervisor`, this module ignores the child spec ids,
176+
so `{:error, {:already_started, pid}}` is not returned for child specs
177+
given with the same id. `{:error, {:already_started, pid}}` is returned
178+
however if a duplicate name is used when using
239179
[name registration](`m:GenServer#module-name-registration`).
240180
"""
241181
@type on_start_child ::
@@ -415,6 +355,10 @@ defmodule DynamicSupervisor do
415355
`{:error, {:already_started, pid}}` is returned however if a duplicate name is
416356
used when using [name registration](`m:GenServer#module-name-registration`).
417357
358+
This function will block the `DynamicSupervisor` until the child initializes.
359+
When starting too many processes dynamically, you may want to use a
360+
`PartitionSupervisor` to split the work across multiple processes.
361+
418362
If the child process start function returns `{:ok, child}` or `{:ok, child,
419363
info}`, then child specification and PID are added to the supervisor and
420364
this function returns the same value.
@@ -518,6 +462,14 @@ defmodule DynamicSupervisor do
518462
@doc """
519463
Terminates the given child identified by `pid`.
520464
465+
This function will block the `DynamicSupervisor` until the child
466+
terminates, which may take an arbitrary amount of time if the child
467+
is trapping exits and implements its own terminate callback.
468+
For this reason, it is often better to ask the child process
469+
itself to terminate, often by declaring in its child spec it has
470+
a restart strategy of `:transient` (or `:temporary`) and then
471+
sending it a message to stop with reason `:shutdown`.
472+
521473
If successful, this function returns `:ok`. If there is no process with
522474
the given PID, this function returns `{:error, :not_found}`.
523475
"""

0 commit comments

Comments
 (0)