aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJan Uhlig <[email protected]>2024-10-02 12:22:10 +0200
committerLoïc Hoguin <[email protected]>2024-11-12 14:56:59 +0100
commit53a8db10195f9aae640b238fa3d571bf0136a55a (patch)
tree6b2ec1f0b3275615e51ce06f5909dd234d47f5f8 /src
parente5649ba630c5be25a0c9c9f20c78f8f9f72e363e (diff)
downloadranch-53a8db10195f9aae640b238fa3d571bf0136a55a.tar.gz
ranch-53a8db10195f9aae640b238fa3d571bf0136a55a.tar.bz2
ranch-53a8db10195f9aae640b238fa3d571bf0136a55a.zip
Fix alarm setting typo
The setting `threshold` was misspelled as `treshold`.
Diffstat (limited to 'src')
-rw-r--r--src/ranch.erl31
-rw-r--r--src/ranch_conns_sup.erl5
2 files changed, 30 insertions, 6 deletions
diff --git a/src/ranch.erl b/src/ranch.erl
index e52f0e7..295e103 100644
--- a/src/ranch.erl
+++ b/src/ranch.erl
@@ -50,6 +50,9 @@
-export([require/1]).
-export([log/4]).
+%% Internal
+-export([compat_normalize_alarms_option/1]).
+
-type max_conns() :: non_neg_integer() | infinity.
-export_type([max_conns/0]).
@@ -59,7 +62,7 @@
-type alarm(Type, Callback) :: #{
type := Type,
callback := Callback,
- treshold := non_neg_integer(),
+ threshold := non_neg_integer(),
cooldown => non_neg_integer()
}.
@@ -135,6 +138,7 @@ validate_transport_opt(max_connections, infinity, _) ->
validate_transport_opt(max_connections, Value, _) ->
is_integer(Value) andalso Value >= 0;
validate_transport_opt(alarms, Alarms, _) ->
+ Alarms1 = compat_normalize_alarms_option(Alarms),
maps:fold(
fun
(_, Opts, true) ->
@@ -143,7 +147,7 @@ validate_transport_opt(alarms, Alarms, _) ->
false
end,
true,
- Alarms);
+ Alarms1);
validate_transport_opt(logger, Value, _) ->
is_atom(Value);
validate_transport_opt(num_acceptors, Value, _) ->
@@ -166,9 +170,9 @@ validate_transport_opt(socket_opts, _, _) ->
validate_transport_opt(_, _, _) ->
false.
-validate_alarm(Alarm = #{type := num_connections, treshold := Treshold,
+validate_alarm(Alarm = #{type := num_connections, threshold := Threshold,
callback := Callback}) ->
- is_integer(Treshold) andalso Treshold >= 0
+ is_integer(Threshold) andalso Threshold >= 0
andalso is_function(Callback, 4)
andalso case Alarm of
#{cooldown := Cooldown} ->
@@ -649,3 +653,22 @@ log(Level, Format, Args, _) ->
debug -> info_msg
end,
error_logger:Function(Format, Args).
+
+%% For backwards compatibility with the misspelled alarm
+%% setting `treshold`.
+%% See https://github.com/ninenines/ranch/issues/349
+-spec compat_normalize_alarms_option(any()) -> any().
+compat_normalize_alarms_option(Alarms = #{}) ->
+ maps:map(
+ fun
+ (_, Alarm = #{threshold := _}) ->
+ maps:remove(treshold, Alarm);
+ (_, Alarm = #{treshold := Threshold}) ->
+ maps:put(threshold, Threshold, maps:remove(treshold, Alarm));
+ (_, Alarm) ->
+ Alarm
+ end,
+ Alarms
+ );
+compat_normalize_alarms_option(Alarms) ->
+ Alarms.
diff --git a/src/ranch_conns_sup.erl b/src/ranch_conns_sup.erl
index 6e6de00..05ef84c 100644
--- a/src/ranch_conns_sup.erl
+++ b/src/ranch_conns_sup.erl
@@ -287,7 +287,7 @@ trigger_alarms(Ref, Alarms, CurConns) ->
Alarms
).
-trigger_alarm(Ref, AlarmName, {Opts=#{treshold := Treshold, callback := Callback}, undefined}, CurConns) when CurConns >= Treshold ->
+trigger_alarm(Ref, AlarmName, {Opts=#{threshold := Threshold, callback := Callback}, undefined}, CurConns) when CurConns >= Threshold ->
ActiveConns = [Pid || {Pid, active} <- get()],
case Callback of
{Mod, Fun} ->
@@ -306,6 +306,7 @@ schedule_activate_alarm(_, _) ->
undefined.
get_alarms(#{alarms := Alarms}) when is_map(Alarms) ->
+ Alarms1 = ranch:compat_normalize_alarms_option(Alarms),
maps:fold(
fun
(Name, Opts = #{type := num_connections, cooldown := _}, Acc) ->
@@ -315,7 +316,7 @@ get_alarms(#{alarms := Alarms}) when is_map(Alarms) ->
(_, _, Acc) -> Acc
end,
#{},
- Alarms
+ Alarms1
);
get_alarms(_) ->
#{}.