Created
October 3, 2018 19:12
-
-
Save tehprofessor/1a01798a0aea669063f8fa5a88a100c9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Thinger do | |
end | |
defmodule Thinger.Fubar do | |
defstruct :queue, :some_module, :some_other_thing | |
use GenServer | |
def init(:ok) do | |
{ | |
:ok, | |
%__MODULE__{ | |
queue: :queue.new(), | |
some_module: Thinger, | |
some_other_thing: %{} | |
} | |
} | |
end | |
# bad | |
def do_fubar(state) do | |
# The closure is not released by the supervisor (parent process) | |
# until the child process terminates. | |
Task.Supervisor.async_nolink(state.task_supervisor, fn -> | |
# The entire state object is being captured, not just the specific values | |
# in this case `queue` is also quietly being copied into the closure as well | |
# causing each batch process to balloon in size. | |
{:performed, state.some_module.run(batch, retries, state.some_other_thing)} | |
end) | |
end | |
# without copying entire state object into closure | |
def dont_fubar(%{some_module: some_module, some_other_thing: some_other_thing} = state) do | |
Task.Supervisor.async_nolink(state.task_supervisor, fn -> | |
{:performed, some_module.run(batch, retries, some_other_thing)} | |
end) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment