Skip to content

Commit

Permalink
chore: set max heap size for handlers (#280)
Browse files Browse the repository at this point in the history
  • Loading branch information
abc3 authored Jan 24, 2024
1 parent f18652f commit 31be38f
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.1.20
1.1.21
1 change: 1 addition & 0 deletions lib/supavisor/client_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ defmodule Supavisor.ClientHandler do

def init(ref, trans, opts) do
Process.flag(:trap_exit, true)
H.set_max_heap_size(150)

{:ok, sock} = :ranch.handshake(ref)
:ok = trans.setopts(sock, active: true)
Expand Down
2 changes: 2 additions & 0 deletions lib/supavisor/db_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ defmodule Supavisor.DbHandler do
@impl true
def init(args) do
Process.flag(:trap_exit, true)
H.set_max_heap_size(150)

{_, tenant} = args.tenant
Logger.metadata(project: tenant, user: args.user, mode: args.mode)

Expand Down
13 changes: 13 additions & 0 deletions lib/supavisor/helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -330,4 +330,17 @@ defmodule Supavisor.Helpers do
kind, reason -> {:error, {:badrpc, {kind, reason}}}
end
end

@doc """
Sets the maximum heap size for the current process. The `max_heap_size` parameter is in megabytes.
## Parameters
- `max_heap_size`: The maximum heap size in megabytes.
"""
@spec set_max_heap_size(pos_integer()) :: map()
def set_max_heap_size(max_heap_size) do
max_heap_words = div(max_heap_size * 1024 * 1024, :erlang.system_info(:wordsize))
Process.flag(:max_heap_size, %{size: max_heap_words})
end
end

0 comments on commit 31be38f

Please sign in to comment.