From e4124de2c71564d37b3732ede0fe1542de1d6f99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Mon, 27 Aug 2012 11:50:35 +0200 Subject: Switch to Ranch for connection handling This is the first of many API incompatible changes. You have been warned. --- src/cowboy.erl | 119 +++++++++++---------------------------------------------- 1 file changed, 22 insertions(+), 97 deletions(-) (limited to 'src/cowboy.erl') diff --git a/src/cowboy.erl b/src/cowboy.erl index 6ef415b..94cae16 100644 --- a/src/cowboy.erl +++ b/src/cowboy.erl @@ -12,103 +12,28 @@ %% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -%% @doc Cowboy API to start and stop listeners. +%% @doc Convenience API to start and stop HTTP/HTTPS listeners. -module(cowboy). --export([start_listener/6, stop_listener/1, child_spec/6, accept_ack/1, - get_protocol_options/1, set_protocol_options/2]). - -%% @doc Start a listener for the given transport and protocol. -%% -%% A listener is effectively a pool of NbAcceptors acceptors. -%% Acceptors accept connections on the given Transport and forward -%% requests to the given Protocol handler. Both transport and protocol -%% modules can be given options through the TransOpts and the -%% ProtoOpts arguments. Available options are documented in the -%% listen transport function and in the protocol module of your choice. -%% -%% All acceptor and request processes are supervised by the listener. -%% -%% It is recommended to set a large enough number of acceptors to improve -%% performance. The exact number depends of course on your hardware, on the -%% protocol used and on the number of expected simultaneous connections. -%% -%% The Transport option max_connections allows you to define -%% the maximum number of simultaneous connections for this listener. It defaults -%% to 1024. See cowboy_listener for more details on limiting the number -%% of connections. -%% -%% Although Cowboy includes a cowboy_http_protocol handler, other -%% handlers can be created for different protocols like IRC, FTP and more. -%% -%% Ref can be used to stop the listener later on. --spec start_listener(any(), non_neg_integer(), module(), any(), module(), any()) - -> {ok, pid()}. -start_listener(Ref, NbAcceptors, Transport, TransOpts, Protocol, ProtoOpts) - when is_integer(NbAcceptors) andalso is_atom(Transport) - andalso is_atom(Protocol) -> - supervisor:start_child(cowboy_sup, child_spec(Ref, NbAcceptors, - Transport, TransOpts, Protocol, ProtoOpts)). - -%% @doc Stop a listener identified by Ref. --spec stop_listener(any()) -> ok | {error, not_found}. +-export([start_http/4]). +-export([start_https/4]). +-export([stop_listener/1]). + +%% @doc Start an HTTP listener. +-spec start_http(any(), non_neg_integer(), any(), any()) -> {ok, pid()}. +start_http(Ref, NbAcceptors, TransOpts, ProtoOpts) + when is_integer(NbAcceptors), NbAcceptors > 0 -> + ranch:start_listener(Ref, NbAcceptors, + ranch_tcp, TransOpts, cowboy_http_protocol, ProtoOpts). + +%% @doc Start an HTTPS listener. +-spec start_https(any(), non_neg_integer(), any(), any()) -> {ok, pid()}. +start_https(Ref, NbAcceptors, TransOpts, ProtoOpts) + when is_integer(NbAcceptors), NbAcceptors > 0 -> + ranch:start_listener(Ref, NbAcceptors, + ranch_ssl, TransOpts, cowboy_http_protocol, ProtoOpts). + +%% @doc Stop a listener. +-spec stop_listener(any()) -> ok. stop_listener(Ref) -> - case supervisor:terminate_child(cowboy_sup, {cowboy_listener_sup, Ref}) of - ok -> - supervisor:delete_child(cowboy_sup, {cowboy_listener_sup, Ref}); - {error, Reason} -> - {error, Reason} - end. - -%% @doc Return a child spec suitable for embedding. -%% -%% When you want to embed cowboy in another application, you can use this -%% function to create a ChildSpec suitable for use in a supervisor. -%% The parameters are the same as in start_listener/6 but rather -%% than hooking the listener to the cowboy internal supervisor, it just returns -%% the spec. --spec child_spec(any(), 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) - andalso is_atom(Protocol) -> - {{cowboy_listener_sup, Ref}, {cowboy_listener_sup, start_link, [ - NbAcceptors, Transport, TransOpts, Protocol, ProtoOpts - ]}, permanent, 5000, supervisor, [cowboy_listener_sup]}. - -%% @doc Acknowledge the accepted connection. -%% -%% Effectively used to make sure the socket control has been given to -%% the protocol process before starting to use it. --spec accept_ack(pid()) -> ok. -accept_ack(ListenerPid) -> - receive {shoot, ListenerPid} -> ok end. - -%% @doc Return the current protocol options for the given listener. --spec get_protocol_options(any()) -> any(). -get_protocol_options(Ref) -> - ListenerPid = ref_to_listener_pid(Ref), - {ok, ProtoOpts} = cowboy_listener:get_protocol_options(ListenerPid), - ProtoOpts. - -%% @doc Upgrade the protocol options for the given listener. -%% -%% 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. -set_protocol_options(Ref, ProtoOpts) -> - ListenerPid = ref_to_listener_pid(Ref), - ok = cowboy_listener:set_protocol_options(ListenerPid, ProtoOpts). - -%% Internal. - --spec ref_to_listener_pid(any()) -> pid(). -ref_to_listener_pid(Ref) -> - Children = supervisor:which_children(cowboy_sup), - {_, ListenerSupPid, _, _} = lists:keyfind( - {cowboy_listener_sup, Ref}, 1, Children), - ListenerSupChildren = supervisor:which_children(ListenerSupPid), - {_, ListenerPid, _, _} = lists:keyfind( - cowboy_listener, 1, ListenerSupChildren), - ListenerPid. + ranch:stop_listener(Ref). -- cgit v1.2.3