0
Fork 0
mirror of https://github.com/ninenines/cowboy.git synced 2025-07-15 20:50:24 +00:00

Add cowboy:get_protocol_options/1 and cowboy_set_protocol_options/2

This allows any application to upgrade the protocol options without
having to restart the listener. This is most useful to update the
dispatch list of HTTP servers, for example.

The upgrade is done at the acceptor level, meaning only new connections
receive the new protocol options.
This commit is contained in:
Loïc Hoguin 2012-01-30 09:34:29 +01:00
parent 830cfc002e
commit e5aef5c1d7
4 changed files with 115 additions and 41 deletions

View file

@ -15,7 +15,8 @@
%% @doc Cowboy API to start and stop listeners.
-module(cowboy).
-export([start_listener/6, stop_listener/1, child_spec/6, accept_ack/1]).
-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.
%%
@ -83,3 +84,32 @@ child_spec(Ref, NbAcceptors, Transport, TransOpts, Protocol, ProtoOpts)
-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.