Current section
Files
Jump to
Current section
Files
src/ssevents@limit.erl
-module(ssevents@limit).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/ssevents/limit.gleam").
-export([new/4, max_line_bytes/1, max_event_bytes/1, max_data_lines/1, max_retry_value/1, new_checked/4, default/0]).
-export_type([limits/0, limit_config_error/0]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
?MODULEDOC(
" Parser and decoder safety limits.\n"
"\n"
" These values bound memory growth for the incremental decoder. The\n"
" `new` constructor rejects nonsensical values with a panic so callers\n"
" do not silently run with ineffective limits. For dynamic input\n"
" (config / env / framework), use `new_checked` and surface\n"
" `LimitConfigError.NonPositiveLimit` through your normal result\n"
" chain.\n"
).
-opaque limits() :: {limits, integer(), integer(), integer(), integer()}.
-type limit_config_error() :: {non_positive_limit, binary(), integer()}.
-file("src/ssevents/limit.gleam", 48).
-spec new(integer(), integer(), integer(), integer()) -> limits().
new(Max_line_bytes, Max_event_bytes, Max_data_lines, Max_retry_value) ->
case Max_line_bytes < 1 of
true ->
erlang:error(#{gleam_error => panic,
message => <<"max_line_bytes must be >= 1"/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"ssevents/limit"/utf8>>,
function => <<"new"/utf8>>,
line => 55});
false ->
nil
end,
case Max_event_bytes < 1 of
true ->
erlang:error(#{gleam_error => panic,
message => <<"max_event_bytes must be >= 1"/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"ssevents/limit"/utf8>>,
function => <<"new"/utf8>>,
line => 59});
false ->
nil
end,
case Max_data_lines < 1 of
true ->
erlang:error(#{gleam_error => panic,
message => <<"max_data_lines must be >= 1"/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"ssevents/limit"/utf8>>,
function => <<"new"/utf8>>,
line => 63});
false ->
nil
end,
case Max_retry_value < 0 of
true ->
erlang:error(#{gleam_error => panic,
message => <<"max_retry_value must be >= 0"/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"ssevents/limit"/utf8>>,
function => <<"new"/utf8>>,
line => 67});
false ->
nil
end,
{limits, Max_line_bytes, Max_event_bytes, Max_data_lines, Max_retry_value}.
-file("src/ssevents/limit.gleam", 78).
-spec max_line_bytes(limits()) -> integer().
max_line_bytes(Limits) ->
{limits, Max_line_bytes, _, _, _} = Limits,
Max_line_bytes.
-file("src/ssevents/limit.gleam", 83).
-spec max_event_bytes(limits()) -> integer().
max_event_bytes(Limits) ->
{limits, _, Max_event_bytes, _, _} = Limits,
Max_event_bytes.
-file("src/ssevents/limit.gleam", 88).
-spec max_data_lines(limits()) -> integer().
max_data_lines(Limits) ->
{limits, _, _, Max_data_lines, _} = Limits,
Max_data_lines.
-file("src/ssevents/limit.gleam", 93).
-spec max_retry_value(limits()) -> integer().
max_retry_value(Limits) ->
{limits, _, _, _, Max_retry_value} = Limits,
Max_retry_value.
-file("src/ssevents/limit.gleam", 124).
-spec check_min(binary(), integer(), integer()) -> {ok, nil} |
{error, limit_config_error()}.
check_min(Field, Value, Minimum) ->
gleam@bool:guard(
Value < Minimum,
{error, {non_positive_limit, Field, Value}},
fun() -> {ok, nil} end
).
-file("src/ssevents/limit.gleam", 106).
?DOC(
" Like `new`, but returns the argument-validation failure as a\n"
" `Result` instead of panicking. Use this when limit values come\n"
" from configuration, environment variables, or other dynamic\n"
" sources where a malformed value is a recoverable runtime\n"
" condition rather than a programmer error.\n"
"\n"
" On success the returned `Limits` is identical to what `new`\n"
" would return for the same arguments.\n"
).
-spec new_checked(integer(), integer(), integer(), integer()) -> {ok, limits()} |
{error, limit_config_error()}.
new_checked(Max_line_bytes, Max_event_bytes, Max_data_lines, Max_retry_value) ->
gleam@result:'try'(
check_min(<<"max_line_bytes"/utf8>>, Max_line_bytes, 1),
fun(_) ->
gleam@result:'try'(
check_min(<<"max_event_bytes"/utf8>>, Max_event_bytes, 1),
fun(_) ->
gleam@result:'try'(
check_min(<<"max_data_lines"/utf8>>, Max_data_lines, 1),
fun(_) ->
gleam@result:'try'(
check_min(
<<"max_retry_value"/utf8>>,
Max_retry_value,
0
),
fun(_) ->
{ok,
{limits,
Max_line_bytes,
Max_event_bytes,
Max_data_lines,
Max_retry_value}}
end
)
end
)
end
)
end
).
-file("src/ssevents/limit.gleam", 39).
-spec default() -> limits().
default() ->
{limits, 8192, 65536, 1024, 86400000}.