Browse Source

Make alarm option cooldown optional

When not specified, defaults to 5000 (5s)
Maria Scott 3 years ago
parent
commit
f3bef7c456
2 changed files with 14 additions and 6 deletions
  1. 10 5
      src/ranch.erl
  2. 4 1
      src/ranch_conns_sup.erl

+ 10 - 5
src/ranch.erl

@@ -59,7 +59,7 @@
 	type := Type,
 	callback := Callback,
 	treshold := non_neg_integer(),
-	cooldown := non_neg_integer()
+	cooldown => non_neg_integer()
 }.
 
 -type alarm_num_connections() :: alarm(num_connections, fun((ref(), term(), pid(), [pid()]) -> any())).
@@ -165,11 +165,16 @@ validate_transport_opt(socket_opts, _, _) ->
 validate_transport_opt(_, _, _) ->
 	false.
 
-validate_alarm(#{type := num_connections, treshold := Treshold,
-		callback := Callback, cooldown := Cooldown}) ->
+validate_alarm(Alarm = #{type := num_connections, treshold := Treshold,
+		callback := Callback}) ->
 	is_integer(Treshold) andalso Treshold >= 0
-	andalso is_integer(Cooldown) andalso Cooldown >= 0
-	andalso is_function(Callback, 4);
+	andalso is_function(Callback, 4)
+	andalso case Alarm of
+		#{cooldown := Cooldown} ->
+			is_integer(Cooldown) andalso Cooldown >= 0;
+		_ ->
+			true
+	end;
 validate_alarm(_) ->
 	false.
 

+ 4 - 1
src/ranch_conns_sup.erl

@@ -307,7 +307,10 @@ schedule_activate_alarm(_, _) ->
 get_alarms(#{alarms := Alarms}) when is_map(Alarms) ->
 	maps:fold(
 		fun
-			(Name, Opts = #{type := num_connections}, Acc) -> Acc#{Name => {Opts, undefined}};
+			(Name, Opts = #{type := num_connections, cooldown := _}, Acc) ->
+				Acc#{Name => {Opts, undefined}};
+			(Name, Opts = #{type := num_connections}, Acc) ->
+				Acc#{Name => {Opts#{cooldown => 5000}, undefined}};
 			(_, _, Acc) -> Acc
 		end,
 		#{},