Skip to content

Commit

Permalink
Improve pdict clearing based on José's suggestion
Browse files Browse the repository at this point in the history
  • Loading branch information
mtrudel committed May 10, 2024
1 parent 66074fc commit 23ad256
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 11 deletions.
9 changes: 2 additions & 7 deletions lib/bandit/http1/handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ defmodule Bandit.HTTP1.Handler do
under_limit = request_limit == 0 || requests_processed < request_limit

if under_limit && transport.keepalive do
Logger.reset_metadata()
if Keyword.get(state.opts.http_1, :clear_process_dict, true), do: clear_process_dict()
gc_every_n_requests = Keyword.get(state.opts.http_1, :gc_every_n_keepalive_requests, 5)
if rem(requests_processed, gc_every_n_requests) == 0, do: :erlang.garbage_collect()
Expand All @@ -33,12 +32,8 @@ defmodule Bandit.HTTP1.Handler do
end

defp clear_process_dict do
Enum.each(Process.get_keys(), fn
key when is_atom(key) ->
if !match?("$" <> _, to_string(key)), do: Process.delete(key)

key ->
Process.delete(key)
Enum.each(Process.get_keys(), fn key ->
if key not in ~w[$ancestors $initial_call]a, do: Process.delete(key)
end)
end

Expand Down
5 changes: 1 addition & 4 deletions test/bandit/http1/request_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -239,10 +239,7 @@ defmodule HTTP1RequestTest do
assert {:ok, "200 OK", _headers, "[]"} = SimpleHTTP1Client.recv_reply(client)

SimpleHTTP1Client.send(client, "GET", "/pdict", ["host: localhost"])

# Keep one $ value unfiltered to test that we do not clobber those
assert {:ok, "200 OK", _headers, "[\"$logger_metadata$\": %{}]"} =
SimpleHTTP1Client.recv_reply(client)
assert {:ok, "200 OK", _headers, "[]"} = SimpleHTTP1Client.recv_reply(client)
end

def pdict(conn) do
Expand Down

0 comments on commit 23ad256

Please sign in to comment.