From 9a8409e5c3f4d865611cdc851d3a0515657b6b69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Thu, 16 May 2013 18:11:58 +0200 Subject: Add ranch:ref() type --- erlang.mk | 2 +- src/ranch.erl | 23 +++++++++++++---------- src/ranch_acceptors_sup.erl | 2 +- src/ranch_conns_sup.erl | 6 +++--- src/ranch_listener_sup.erl | 2 +- src/ranch_protocol.erl | 2 +- src/ranch_server.erl | 22 +++++++++++----------- 7 files changed, 31 insertions(+), 28 deletions(-) diff --git a/erlang.mk b/erlang.mk index 9e4c698..ecd4d54 100644 --- a/erlang.mk +++ b/erlang.mk @@ -116,7 +116,7 @@ CT_SUITES ?= CT_SUITES_FULL = $(addsuffix _SUITE,$(CT_SUITES)) tests: ERLC_OPTS += -DTEST=1 +'{parse_transform, eunit_autoexport}' -tests: clean clean-deps deps app build-tests +tests: clean deps app build-tests @mkdir -p logs/ @$(CT_RUN) -suite $(CT_SUITES_FULL) $(gen_verbose) rm -f test/*.beam diff --git a/src/ranch.erl b/src/ranch.erl index ea5f59b..94a3813 100644 --- a/src/ranch.erl +++ b/src/ranch.erl @@ -32,6 +32,9 @@ -type max_conns() :: non_neg_integer() | infinity. -export_type([max_conns/0]). +-type ref() :: any(). +-export_type([ref/0]). + %% @doc Start a listener for the given transport and protocol. %% %% A listener is effectively a pool of NbAcceptors acceptors. @@ -56,7 +59,7 @@ %% %% This function will return `{error, badarg}` if and only if the transport %% module given doesn't appear to be correct. --spec start_listener(any(), non_neg_integer(), module(), any(), module(), any()) +-spec start_listener(ref(), non_neg_integer(), module(), any(), module(), any()) -> {ok, pid()} | {error, badarg}. start_listener(Ref, NbAcceptors, Transport, TransOpts, Protocol, ProtoOpts) when is_integer(NbAcceptors) andalso is_atom(Transport) @@ -93,7 +96,7 @@ start_listener(Ref, NbAcceptors, Transport, TransOpts, Protocol, ProtoOpts) %% %% Note that stopping the listener will close all currently running %% connections abruptly. --spec stop_listener(any()) -> ok | {error, not_found}. +-spec stop_listener(ref()) -> ok | {error, not_found}. stop_listener(Ref) -> case supervisor:terminate_child(ranch_sup, {ranch_listener_sup, Ref}) of ok -> @@ -110,7 +113,7 @@ stop_listener(Ref) -> %% The parameters are the same as in start_listener/6 but rather %% than hooking the listener to the Ranch internal supervisor, it just returns %% the spec. --spec child_spec(any(), non_neg_integer(), module(), any(), module(), any()) +-spec child_spec(ref(), non_neg_integer(), module(), any(), module(), any()) -> supervisor:child_spec(). child_spec(Ref, NbAcceptors, Transport, TransOpts, Protocol, ProtoOpts) when is_integer(NbAcceptors) andalso is_atom(Transport) @@ -123,7 +126,7 @@ child_spec(Ref, NbAcceptors, Transport, TransOpts, Protocol, ProtoOpts) %% %% Effectively used to make sure the socket control has been given to %% the protocol process before starting to use it. --spec accept_ack(any()) -> ok. +-spec accept_ack(ref()) -> ok. accept_ack(Ref) -> receive {shoot, Ref} -> ok end. @@ -132,29 +135,29 @@ accept_ack(Ref) -> %% Useful if you have long-lived connections that aren't taking up %% resources and shouldn't be counted in the limited number of running %% connections. --spec remove_connection(any()) -> ok. +-spec remove_connection(ref()) -> ok. remove_connection(Ref) -> ConnsSup = ranch_server:get_connections_sup(Ref), ConnsSup ! {remove_connection, Ref}, ok. %% @doc Return the listener's port. --spec get_port(any()) -> inet:port_number(). +-spec get_port(ref()) -> inet:port_number(). get_port(Ref) -> ranch_server:get_port(Ref). %% @doc Return the max number of connections allowed concurrently. --spec get_max_connections(any()) -> max_conns(). +-spec get_max_connections(ref()) -> max_conns(). get_max_connections(Ref) -> ranch_server:get_max_connections(Ref). %% @doc Set the max number of connections allowed concurrently. --spec set_max_connections(any(), max_conns()) -> ok. +-spec set_max_connections(ref(), max_conns()) -> ok. set_max_connections(Ref, MaxConnections) -> ranch_server:set_max_connections(Ref, MaxConnections). %% @doc Return the current protocol options for the given listener. --spec get_protocol_options(any()) -> any(). +-spec get_protocol_options(ref()) -> any(). get_protocol_options(Ref) -> ranch_server:get_protocol_options(Ref). @@ -163,7 +166,7 @@ get_protocol_options(Ref) -> %% The upgrade takes place at the acceptor level, meaning that only the %% newly accepted connections receive the new protocol options. This has %% no effect on the currently opened connections. --spec set_protocol_options(any(), any()) -> ok. +-spec set_protocol_options(ref(), any()) -> ok. set_protocol_options(Ref, Opts) -> ranch_server:set_protocol_options(Ref, Opts). diff --git a/src/ranch_acceptors_sup.erl b/src/ranch_acceptors_sup.erl index 18574fa..d8f8ba1 100644 --- a/src/ranch_acceptors_sup.erl +++ b/src/ranch_acceptors_sup.erl @@ -24,7 +24,7 @@ %% API. --spec start_link(any(), non_neg_integer(), module(), any()) +-spec start_link(ranch:ref(), non_neg_integer(), module(), any()) -> {ok, pid()}. start_link(Ref, NbAcceptors, Transport, TransOpts) -> supervisor:start_link(?MODULE, [Ref, NbAcceptors, Transport, TransOpts]). diff --git a/src/ranch_conns_sup.erl b/src/ranch_conns_sup.erl index 5555413..2964b8b 100644 --- a/src/ranch_conns_sup.erl +++ b/src/ranch_conns_sup.erl @@ -34,7 +34,7 @@ -record(state, { parent = undefined :: pid(), - ref :: any(), + ref :: ranch:ref(), conn_type :: conn_type(), transport = undefined :: module(), protocol = undefined :: module(), @@ -44,7 +44,7 @@ %% API. --spec start_link(any(), conn_type(), module(), module()) -> {ok, pid()}. +-spec start_link(ranch:ref(), conn_type(), module(), module()) -> {ok, pid()}. start_link(Ref, ConnType, Transport, Protocol) -> proc_lib:start_link(?MODULE, init, [self(), Ref, ConnType, Transport, Protocol]). @@ -92,7 +92,7 @@ active_connections(SupPid) -> %% Supervisor internals. --spec init(pid(), any(), conn_type(), module(), module()) -> no_return(). +-spec init(pid(), ranch:ref(), conn_type(), module(), module()) -> no_return(). init(Parent, Ref, ConnType, Transport, Protocol) -> process_flag(trap_exit, true), ok = ranch_server:set_connections_sup(Ref, self()), diff --git a/src/ranch_listener_sup.erl b/src/ranch_listener_sup.erl index 2aecd60..0392105 100644 --- a/src/ranch_listener_sup.erl +++ b/src/ranch_listener_sup.erl @@ -24,7 +24,7 @@ %% API. --spec start_link(any(), non_neg_integer(), module(), any(), module(), any()) +-spec start_link(ranch:ref(), non_neg_integer(), module(), any(), module(), any()) -> {ok, pid()}. start_link(Ref, NbAcceptors, Transport, TransOpts, Protocol, ProtoOpts) -> MaxConns = proplists:get_value(max_connections, TransOpts, 1024), diff --git a/src/ranch_protocol.erl b/src/ranch_protocol.erl index c788547..c179789 100644 --- a/src/ranch_protocol.erl +++ b/src/ranch_protocol.erl @@ -17,7 +17,7 @@ %% Start a new connection process for the given socket. -callback start_link( - Ref::any(), + Ref::ranch:ref(), Socket::any(), Transport::module(), ProtocolOptions::any()) diff --git a/src/ranch_server.erl b/src/ranch_server.erl index 200e29c..0b21cce 100644 --- a/src/ranch_server.erl +++ b/src/ranch_server.erl @@ -53,12 +53,12 @@ start_link() -> gen_server:start_link({local, ?MODULE}, ?MODULE, [], []). %% @private --spec set_new_listener_opts(any(), ranch:max_conns(), any()) -> ok. +-spec set_new_listener_opts(ranch:ref(), ranch:max_conns(), any()) -> ok. set_new_listener_opts(Ref, MaxConns, Opts) -> gen_server:call(?MODULE, {set_new_listener_opts, Ref, MaxConns, Opts}). %% @doc Cleanup listener options after it has been stopped. --spec cleanup_listener_opts(any()) -> ok. +-spec cleanup_listener_opts(ranch:ref()) -> ok. cleanup_listener_opts(Ref) -> _ = ets:delete(?TAB, {port, Ref}), _ = ets:delete(?TAB, {max_conns, Ref}), @@ -66,48 +66,48 @@ cleanup_listener_opts(Ref) -> ok. %% @doc Set a connection supervisor associated with specific listener. --spec set_connections_sup(any(), pid()) -> ok. +-spec set_connections_sup(ranch:ref(), pid()) -> ok. set_connections_sup(Ref, Pid) -> true = gen_server:call(?MODULE, {set_connections_sup, Ref, Pid}), ok. %% @doc Return the connection supervisor used by specific listener. --spec get_connections_sup(any()) -> pid(). +-spec get_connections_sup(ranch:ref()) -> pid(). get_connections_sup(Ref) -> ets:lookup_element(?TAB, {conns_sup, Ref}, 2). %% @private --spec set_port(any(), inet:port_number()) -> ok. +-spec set_port(ranch:ref(), inet:port_number()) -> ok. set_port(Ref, Port) -> gen_server:call(?MODULE, {set_port, Ref, Port}). %% @doc Return the listener's port. --spec get_port(any()) -> inet:port_number(). +-spec get_port(ranch:ref()) -> inet:port_number(). get_port(Ref) -> ets:lookup_element(?TAB, {port, Ref}, 2). %% @doc Set the max number of connections allowed concurrently. --spec set_max_connections(any(), ranch:max_conns()) -> ok. +-spec set_max_connections(ranch:ref(), ranch:max_conns()) -> ok. set_max_connections(Ref, MaxConnections) -> gen_server:call(?MODULE, {set_max_conns, Ref, MaxConnections}). %% @doc Return the max number of connections allowed concurrently. --spec get_max_connections(any()) -> ranch:max_conns(). +-spec get_max_connections(ranch:ref()) -> ranch:max_conns(). get_max_connections(Ref) -> ets:lookup_element(?TAB, {max_conns, Ref}, 2). %% @doc Upgrade the protocol options. --spec set_protocol_options(any(), any()) -> ok. +-spec set_protocol_options(ranch:ref(), any()) -> ok. set_protocol_options(Ref, ProtoOpts) -> gen_server:call(?MODULE, {set_opts, Ref, ProtoOpts}). %% @doc Return the current protocol options. --spec get_protocol_options(any()) -> any(). +-spec get_protocol_options(ranch:ref()) -> any(). get_protocol_options(Ref) -> ets:lookup_element(?TAB, {opts, Ref}, 2). %% @doc Count the number of connections in the connection pool. --spec count_connections(any()) -> non_neg_integer(). +-spec count_connections(ranch:ref()) -> non_neg_integer(). count_connections(Ref) -> ranch_conns_sup:active_connections(get_connections_sup(Ref)). -- cgit v1.2.3