aboutsummaryrefslogtreecommitdiffstats
path: root/src/cowboy.erl
diff options
context:
space:
mode:
authorLoïc Hoguin <[email protected]>2012-01-30 09:34:29 +0100
committerLoïc Hoguin <[email protected]>2012-01-31 08:49:15 +0100
commite5aef5c1d7be8cffedfedda77d035fa30d5513dc (patch)
treeae6d7e64779299ab6a26208d83f1c56d345c7587 /src/cowboy.erl
parent830cfc002e992126a2c605594f25d40a5a193968 (diff)
downloadcowboy-e5aef5c1d7be8cffedfedda77d035fa30d5513dc.tar.gz
cowboy-e5aef5c1d7be8cffedfedda77d035fa30d5513dc.tar.bz2
cowboy-e5aef5c1d7be8cffedfedda77d035fa30d5513dc.zip
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.
Diffstat (limited to 'src/cowboy.erl')
-rw-r--r--src/cowboy.erl32
1 files changed, 31 insertions, 1 deletions
diff --git a/src/cowboy.erl b/src/cowboy.erl
index 6defeea..7963df2 100644
--- a/src/cowboy.erl
+++ b/src/cowboy.erl
@@ -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.