Browse Source

Improve max_connections type

It is non_neg_integer() | infinity. Introduce the type
`ranch:max_conns/0` for easier manipulation.
Loïc Hoguin 12 years ago
parent
commit
201a0cb5e4
3 changed files with 11 additions and 8 deletions
  1. 5 2
      src/ranch.erl
  2. 3 3
      src/ranch_acceptor.erl
  3. 3 3
      src/ranch_listener.erl

+ 5 - 2
src/ranch.erl

@@ -28,6 +28,9 @@
 -export([set_option_default/3]).
 -export([require/1]).
 
+-type max_conns() :: non_neg_integer() | infinity.
+-export_type([max_conns/0]).
+
 %% @doc Start a listener for the given transport and protocol.
 %%
 %% A listener is effectively a pool of <em>NbAcceptors</em> acceptors.
@@ -124,14 +127,14 @@ get_port(Ref) ->
 	Port.
 
 %% @doc Return the max number of connections allowed concurrently.
--spec get_max_connections(any()) -> non_neg_integer().
+-spec get_max_connections(any()) -> max_conns().
 get_max_connections(Ref) ->
 	ListenerPid = ranch_server:lookup_listener(Ref),
 	{ok, MaxConnections} = ranch_listener:get_max_connections(ListenerPid),
 	MaxConnections.
 
 %% @doc Set the max number of connections allowed concurrently.
--spec set_max_connections(any(), non_neg_integer()) -> ok.
+-spec set_max_connections(any(), max_conns()) -> ok.
 set_max_connections(Ref, MaxConnections) ->
 	ListenerPid = ranch_server:lookup_listener(Ref),
 	ok = ranch_listener:set_max_connections(ListenerPid, MaxConnections).

+ 3 - 3
src/ranch_acceptor.erl

@@ -43,7 +43,7 @@ init(LSocket, Transport, Protocol, MaxConns, Opts, ListenerPid, ConnsSup) ->
 	loop(LSocket, Transport, Protocol, MaxConns, Opts, ListenerPid, ConnsSup).
 
 -spec loop(inet:socket(), module(), module(),
-	non_neg_integer(), any(), pid(), pid()) -> no_return().
+	ranch:max_conns(), any(), pid(), pid()) -> no_return().
 loop(LSocket, Transport, Protocol, MaxConns, Opts, ListenerPid, ConnsSup) ->
 	receive
 		%% We couldn't accept the socket but it's safe to continue.
@@ -70,8 +70,8 @@ loop(LSocket, Transport, Protocol, MaxConns, Opts, ListenerPid, ConnsSup) ->
 				MaxConns, Opts2, ListenerPid, ConnsSup)
 	end.
 
--spec maybe_wait(pid(), non_neg_integer(), non_neg_integer())
-	-> {ok, non_neg_integer()}.
+-spec maybe_wait(pid(), MaxConns, non_neg_integer())
+	-> {ok, MaxConns} when MaxConns::ranch:max_conns().
 maybe_wait(_, MaxConns, NbConns) when MaxConns > NbConns ->
 	{ok, MaxConns};
 maybe_wait(ListenerPid, MaxConns, NbConns) ->

+ 3 - 3
src/ranch_listener.erl

@@ -38,7 +38,7 @@
 
 -record(state, {
 	ref :: any(),
-	max_conns = undefined :: non_neg_integer(),
+	max_conns = undefined :: ranch:max_conns(),
 	port = undefined :: undefined | inet:port_number(),
 	proto_opts = undefined :: any(),
 	rm_diff = 0 :: non_neg_integer()
@@ -83,12 +83,12 @@ set_port(ServerPid, Port) ->
 	gen_server:cast(ServerPid, {set_port, Port}).
 
 %% @doc Return the max number of connections allowed concurrently.
--spec get_max_connections(pid()) -> {ok, non_neg_integer()}.
+-spec get_max_connections(pid()) -> {ok, ranch:max_conns()}.
 get_max_connections(ServerPid) ->
 	gen_server:call(ServerPid, get_max_connections).
 
 %% @doc Set the max number of connections allowed concurrently.
--spec set_max_connections(pid(), non_neg_integer()) -> ok.
+-spec set_max_connections(pid(), ranch:max_conns()) -> ok.
 set_max_connections(ServerPid, MaxConnections) ->
 	gen_server:call(ServerPid, {set_max_connections, MaxConnections}).