Browse Source

Return listener info as a map

juhlig 5 years ago
parent
commit
af739162fd
3 changed files with 101 additions and 112 deletions
  1. 6 5
      doc/src/manual/ranch.info.asciidoc
  2. 22 17
      src/ranch.erl
  3. 73 90
      test/acceptor_SUITE.erl

+ 6 - 5
doc/src/manual/ranch.info.asciidoc

@@ -8,10 +8,10 @@ ranch:info - Overview of Ranch listeners
 
 [source,erlang]
 ----
-info() -> [{Ref, Info}]
+info() -> #{Ref := Info}
 info(Ref) -> Info
 
-Info :: [{Key :: atom(), Value :: any()}]
+Info :: #{Key :: atom() := Value :: any()}
 ----
 
 Overview of Ranch listeners.
@@ -31,7 +31,6 @@ pid:: Pid of the listener's top-level supervisor.
 status:: Listener status, either running or suspended.
 ip:: Interface Ranch listens on.
 port:: Port number Ranch listens on.
-num_acceptors:: Number of acceptor processes.
 max_connections:: Maximum number of connections per connection supervisor.
 active_connections:: Number of active connections.
 all_connections:: Number of connections, including those removed from the count.
@@ -40,8 +39,10 @@ transport_options:: Transport options.
 protocol:: Protocol module.
 protocol_options:: Protocol options.
 
-// @todo I mistakenly removed the num_acceptors key,
-// it should be added back.
+== Changelog
+
+* *2.0*: The listener info is now returned as a map.
+* *2.0*: The `num_acceptors` key has been removed.
 
 == Examples
 

+ 22 - 17
src/ranch.erl

@@ -315,12 +315,17 @@ get_protocol_options(Ref) ->
 set_protocol_options(Ref, Opts) ->
 	ranch_server:set_protocol_options(Ref, Opts).
 
--spec info() -> [{any(), [{atom(), any()}]}].
+-spec info() -> #{ref() := #{atom() := term()}}.
 info() ->
-	[{Ref, listener_info(Ref, Pid)}
-		|| {Ref, Pid} <- ranch_server:get_listener_sups()].
+	lists:foldl(
+		fun ({Ref, Pid}, Acc) ->
+			Acc#{Ref => listener_info(Ref, Pid)}
+		end,
+		#{},
+		ranch_server:get_listener_sups()
+	).
 
--spec info(ref()) -> [{atom(), any()}].
+-spec info(ref()) -> #{atom() := term()}.
 info(Ref) ->
 	Pid = ranch_server:get_listener_sup(Ref),
 	listener_info(Ref, Pid).
@@ -337,19 +342,19 @@ listener_info(Ref, Pid) ->
 	MaxConns = get_max_connections(Ref),
 	TransOpts = ranch_server:get_transport_options(Ref),
 	ProtoOpts = get_protocol_options(Ref),
-	[
-		{pid, Pid},
-		{status, Status},
-		{ip, IP},
-		{port, Port},
-		{max_connections, MaxConns},
-		{active_connections, get_connections(Ref, active)},
-		{all_connections, get_connections(Ref, all)},
-		{transport, Transport},
-		{transport_options, TransOpts},
-		{protocol, Protocol},
-		{protocol_options, ProtoOpts}
-	].
+	#{
+		pid => Pid,
+		status => Status,
+		ip => IP,
+		port => Port,
+		max_connections => MaxConns,
+		active_connections => get_connections(Ref, active),
+		all_connections => get_connections(Ref, all),
+		transport => Transport,
+		transport_options => TransOpts,
+		protocol => Protocol,
+		protocol_options => ProtoOpts
+	}.
 
 -spec procs(ref(), acceptors | connections) -> [pid()].
 procs(Ref, Type) ->

+ 73 - 90
test/acceptor_SUITE.erl

@@ -137,47 +137,41 @@ misc_info(_) ->
 	{ok, _} = gen_tcp:connect("localhost", Port1, [binary, {active, false}, {packet, raw}]),
 	receive after 250 -> ok end,
 	%% Confirm the info returned by Ranch is correct.
-	[
-		{{misc_info, act}, [
-			{pid, Pid2},
-			{status, _},
-			{ip, _},
-			{port, Port2},
-			{max_connections, infinity}, %% Option was modified.
-			{active_connections, 0},
-			{all_connections, 0},
-			{transport, ranch_tcp},
-			{transport_options, #{num_acceptors := 2}},
-			{protocol, active_echo_protocol},
-			{protocol_options, {}}
-		]},
-		{{misc_info, ssl}, [
-			{pid, Pid3},
-			{status, _},
-			{ip, _},
-			{port, Port3},
-			{max_connections, 1024},
-			{active_connections, 0},
-			{all_connections, 0},
-			{transport, ranch_ssl},
-			{transport_options, #{num_acceptors := 3, socket_opts := Opts}},
-			{protocol, echo_protocol},
-			{protocol_options, [{}]}
-		]},
-		{{misc_info, tcp}, [
-			{pid, Pid1},
-			{status, _},
-			{ip, _},
-			{port, Port1},
-			{max_connections, 1024},
-			{active_connections, 2},
-			{all_connections, 5},
-			{transport, ranch_tcp},
-			{transport_options, #{num_acceptors := 1}},
-			{protocol, remove_conn_and_wait_protocol},
-			{protocol_options, [{remove, false, 2500}]} %% Option was modified.
-		]}
-	] = do_get_listener_info(misc_info),
+	#{
+		{misc_info, act} := #{
+			pid := Pid2,
+			port := Port2,
+			max_connections := infinity, %% Option was modified.
+			active_connections := 0,
+			all_connections := 0,
+			transport := ranch_tcp,
+			transport_options := #{num_acceptors := 2},
+			protocol := active_echo_protocol,
+			protocol_options := {}
+		},
+		{misc_info, ssl} := #{
+			pid := Pid3,
+			port := Port3,
+			max_connections := 1024,
+			active_connections := 0,
+			all_connections := 0,
+			transport := ranch_ssl,
+			transport_options := #{num_acceptors := 3, socket_opts := Opts},
+			protocol := echo_protocol,
+			protocol_options := [{}]
+		},
+		{misc_info, tcp} := #{
+			pid := Pid1,
+			port := Port1,
+			max_connections := 1024,
+			active_connections := 2,
+			all_connections := 5,
+			transport := ranch_tcp,
+			transport_options := #{num_acceptors := 1},
+			protocol := remove_conn_and_wait_protocol,
+			protocol_options := [{remove, false, 2500}] %% Option was modified.
+		}
+	} = ranch:info(),
 	%% Get acceptors.
 	[_] = ranch:procs({misc_info, tcp}, acceptors),
 	[_, _] = ranch:procs({misc_info, act}, acceptors),
@@ -224,47 +218,41 @@ misc_info_embedded(_) ->
 	{ok, _} = gen_tcp:connect("localhost", Port1, [binary, {active, false}, {packet, raw}]),
 	receive after 250 -> ok end,
 	%% Confirm the info returned by Ranch is correct.
-	[
-		{{misc_info_embedded, act}, [
-			{pid, Pid2},
-			{status, _},
-			{ip, _},
-			{port, Port2},
-			{max_connections, infinity}, %% Option was modified.
-			{active_connections, 0},
-			{all_connections, 0},
-			{transport, ranch_tcp},
-			{transport_options, #{num_acceptors := 2}},
-			{protocol, active_echo_protocol},
-			{protocol_options, {}}
-		]},
-		{{misc_info_embedded, ssl}, [
-			{pid, Pid3},
-			{status, _},
-			{ip, _},
-			{port, Port3},
-			{max_connections, 1024},
-			{active_connections, 0},
-			{all_connections, 0},
-			{transport, ranch_ssl},
-			{transport_options, #{num_acceptors := 3, socket_opts := Opts}},
-			{protocol, echo_protocol},
-			{protocol_options, [{}]}
-		]},
-		{{misc_info_embedded, tcp}, [
-			{pid, Pid1},
-			{status, _},
-			{ip, _},
-			{port, Port1},
-			{max_connections, 1024},
-			{active_connections, 2},
-			{all_connections, 5},
-			{transport, ranch_tcp},
-			{transport_options, #{num_acceptors := 1}},
-			{protocol, remove_conn_and_wait_protocol},
-			{protocol_options, [{remove, false, 2500}]} %% Option was modified.
-		]}
-	] = do_get_listener_info(misc_info_embedded),
+	#{
+		{misc_info_embedded, act} := #{
+			pid := Pid2,
+			port := Port2,
+			max_connections := infinity, %% Option was modified.
+			active_connections := 0,
+			all_connections := 0,
+			transport := ranch_tcp,
+			transport_options := #{num_acceptors := 2},
+			protocol := active_echo_protocol,
+			protocol_options := {}
+		},
+		{misc_info_embedded, ssl} := #{
+			pid := Pid3,
+			port := Port3,
+			max_connections := 1024,
+			active_connections := 0,
+			all_connections := 0,
+			transport := ranch_ssl,
+			transport_options := #{num_acceptors := 3, socket_opts := Opts},
+			protocol := echo_protocol,
+			protocol_options := [{}]
+		},
+		{misc_info_embedded, tcp} := #{
+			pid := Pid1,
+			port := Port1,
+			max_connections := 1024,
+			active_connections := 2,
+			all_connections := 5,
+			transport := ranch_tcp,
+			transport_options := #{num_acceptors := 1},
+			protocol := remove_conn_and_wait_protocol,
+			protocol_options := [{remove, false, 2500}] %% Option was modified.
+		}
+	} = ranch:info(),
 	%% Get acceptors.
 	[_] = ranch:procs({misc_info_embedded, tcp}, acceptors),
 	[_, _] = ranch:procs({misc_info_embedded, act}, acceptors),
@@ -276,24 +264,19 @@ misc_info_embedded(_) ->
 	%% Stop embedded tcp listener and ensure it is gone.
 	ok = embedded_sup:stop_listener(SupPid, {misc_info_embedded, tcp}),
 	timer:sleep(500),
-	[{{misc_info_embedded, act}, _}, {{misc_info_embedded, ssl}, _}] =
-		do_get_listener_info(misc_info_embedded),
+	false = maps:is_key({misc_info_embedded, tcp}, ranch:info()),
 	%% Stop embedded act listener and ensure it is gone.
 	ok = embedded_sup:stop_listener(SupPid, {misc_info_embedded, act}),
 	timer:sleep(500),
-	[{{misc_info_embedded, ssl}, _}] =
-		do_get_listener_info(misc_info_embedded),
+	false = maps:is_key({misc_info_embedded, act}, ranch:info()),
 	%% Stop embedded ssl listener and ensure it is gone.
 	ok = embedded_sup:stop_listener(SupPid, {misc_info_embedded, ssl}),
 	timer:sleep(500),
-	[] = do_get_listener_info(misc_info_embedded),
+	false = maps:is_key({misc_info_embedded, ssl}, ranch:info()),
 	%% Stop embedded supervisor.
 	embedded_sup:stop(SupPid),
 	ok.
 
-do_get_listener_info(ListenerGroup) ->
-	lists:sort([L || L={{G, _}, _} <- ranch:info(), G=:=ListenerGroup]).
-
 misc_opts_logger(_) ->
 	doc("Confirm that messages are sent via the configured logger module."),
 	register(misc_opts_logger, self()),