Skip to content

Commit

Permalink
Avoid clash between Plug nodes, closes #1179
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Oct 20, 2023
1 parent 7ac742f commit 4088bc4
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions lib/plug/upload.ex
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@ defmodule Plug.Upload do
end

defp generate_tmp_dir() do
tmp_roots = :persistent_term.get(__MODULE__)
{tmp_roots, suffix} = :persistent_term.get(__MODULE__)
{mega, _, _} = :os.timestamp()
subdir = "/plug-" <> i(mega)
subdir = "/plug-" <> i(mega) <> "-" <> suffix

if tmp = Enum.find_value(tmp_roots, &make_tmp_dir(&1 <> subdir)) do
{:ok, tmp}
Expand Down Expand Up @@ -206,7 +206,9 @@ defmodule Plug.Upload do
Process.flag(:trap_exit, true)
tmp = Enum.find_value(@temp_env_vars, "/tmp", &System.get_env/1) |> Path.expand()
cwd = Path.join(File.cwd!(), "tmp")
:persistent_term.put(__MODULE__, [tmp, cwd])
# Add a tiny random component to avoid clashes between nodes
suffix = :crypto.strong_rand_bytes(3) |> Base.url_encode64()
:persistent_term.put(__MODULE__, {[tmp, cwd], suffix})

:ets.new(@dir_table, [:named_table, :public, :set])
:ets.new(@path_table, [:named_table, :public, :duplicate_bag])
Expand Down

0 comments on commit 4088bc4

Please sign in to comment.