From e550ba7cd3e63d8d6e45336d6cadb00605d548dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Thu, 15 Dec 2011 20:19:02 +0100 Subject: Add cowboy:accept_ack/1 for a cleaner handling of the shoot message --- src/cowboy.erl | 11 ++++++++++- src/cowboy_acceptor.erl | 2 +- src/cowboy_http_protocol.erl | 2 +- src/cowboy_protocol.erl | 6 +++--- 4 files changed, 15 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/cowboy.erl b/src/cowboy.erl index 9b07921..6defeea 100644 --- a/src/cowboy.erl +++ b/src/cowboy.erl @@ -15,7 +15,7 @@ %% @doc Cowboy API to start and stop listeners. -module(cowboy). --export([start_listener/6, stop_listener/1, child_spec/6]). +-export([start_listener/6, stop_listener/1, child_spec/6, accept_ack/1]). %% @doc Start a listener for the given transport and protocol. %% @@ -61,6 +61,7 @@ stop_listener(Ref) -> 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 @@ -74,3 +75,11 @@ child_spec(Ref, NbAcceptors, Transport, TransOpts, Protocol, ProtoOpts) {{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. diff --git a/src/cowboy_acceptor.erl b/src/cowboy_acceptor.erl index f2b603e..4cb9fa7 100644 --- a/src/cowboy_acceptor.erl +++ b/src/cowboy_acceptor.erl @@ -40,7 +40,7 @@ acceptor(LSocket, Transport, Protocol, Opts, MaxConns, ListenerPid, ReqsSup) -> Transport:controlling_process(CSocket, Pid), {ok, NbConns} = cowboy_listener:add_connection(ListenerPid, default, Pid), - Pid ! shoot, + Pid ! {shoot, ListenerPid}, limit_reqs(ListenerPid, NbConns, MaxConns); {error, timeout} -> ignore; diff --git a/src/cowboy_http_protocol.erl b/src/cowboy_http_protocol.erl index 3a6522f..41ac909 100644 --- a/src/cowboy_http_protocol.erl +++ b/src/cowboy_http_protocol.erl @@ -77,7 +77,7 @@ init(ListenerPid, Socket, Transport, Opts) -> Timeout = proplists:get_value(timeout, Opts, 5000), URLDecDefault = {fun cowboy_http:urldecode/2, crash}, URLDec = proplists:get_value(urldecode, Opts, URLDecDefault), - receive shoot -> ok end, + ok = cowboy:accept_ack(ListenerPid), wait_request(#state{listener=ListenerPid, socket=Socket, transport=Transport, dispatch=Dispatch, max_empty_lines=MaxEmptyLines, max_line_length=MaxLineLength, timeout=Timeout, urldecode=URLDec}). diff --git a/src/cowboy_protocol.erl b/src/cowboy_protocol.erl index 9dc35d9..34bb1a1 100644 --- a/src/cowboy_protocol.erl +++ b/src/cowboy_protocol.erl @@ -24,9 +24,9 @@ %% starting the listener. The start_link/4 function must follow %% the supervisor start function specification. %% -%% After initializing your protocol, it is recommended to wait to -%% receive a message containing the atom 'shoot', as it will ensure -%% Cowboy has been able to fully initialize the socket. +%% After initializing your protocol, it is recommended to call the +%% function cowboy:accept_ack/1 with the ListenerPid as argument, +%% as it will ensure Cowboy has been able to fully initialize the socket. %% Anything you do past this point is up to you! %% %% If you need to change some socket options, like enabling raw mode -- cgit v1.2.3