From 7006c50c3ed6c3cbcb24e9e88a76ebd1aaf3a5f8 Mon Sep 17 00:00:00 2001 From: "j.uhlig" Date: Mon, 9 Apr 2018 12:53:02 +0200 Subject: Add suspend/resume of listeners and update of transport options This allows graceful draining of connections, updating transport options on a running listener without having to drop connections and other similar scenarios. Note that when updating transport options the listener must be suspended which means that new connections will be rejected until the listener is resumed. --- src/ranch_server.erl | 41 ++++++++++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 13 deletions(-) (limited to 'src/ranch_server.erl') diff --git a/src/ranch_server.erl b/src/ranch_server.erl index 89c508c..80f82d6 100644 --- a/src/ranch_server.erl +++ b/src/ranch_server.erl @@ -17,7 +17,7 @@ %% API. -export([start_link/0]). --export([set_new_listener_opts/4]). +-export([set_new_listener_opts/5]). -export([cleanup_listener_opts/1]). -export([set_connections_sup/2]). -export([get_connections_sup/1]). @@ -29,6 +29,8 @@ -export([get_addr/1]). -export([set_max_connections/2]). -export([get_max_connections/1]). +-export([set_transport_options/2]). +-export([get_transport_options/1]). -export([set_protocol_options/2]). -export([get_protocol_options/1]). -export([get_listener_start_args/1]). @@ -55,15 +57,16 @@ start_link() -> gen_server:start_link({local, ?MODULE}, ?MODULE, [], []). --spec set_new_listener_opts(ranch:ref(), ranch:max_conns(), any(), [any()]) -> ok. -set_new_listener_opts(Ref, MaxConns, ProtoOpts, StartArgs) -> - gen_server:call(?MODULE, {set_new_listener_opts, Ref, MaxConns, ProtoOpts, StartArgs}). +-spec set_new_listener_opts(ranch:ref(), ranch:max_conns(), any(), any(), [any()]) -> ok. +set_new_listener_opts(Ref, MaxConns, TransOpts, ProtoOpts, StartArgs) -> + gen_server:call(?MODULE, {set_new_listener_opts, Ref, MaxConns, TransOpts, ProtoOpts, StartArgs}). -spec cleanup_listener_opts(ranch:ref()) -> ok. cleanup_listener_opts(Ref) -> _ = ets:delete(?TAB, {addr, Ref}), _ = ets:delete(?TAB, {max_conns, Ref}), - _ = ets:delete(?TAB, {opts, Ref}), + _ = ets:delete(?TAB, {trans_opts, Ref}), + _ = ets:delete(?TAB, {proto_opts, Ref}), _ = ets:delete(?TAB, {listener_start_args, Ref}), %% We also remove the pid of the connections supervisor. %% Depending on the timing, it might already have been deleted @@ -103,11 +106,11 @@ get_listener_sup(Ref) -> get_listener_sups() -> [{Ref, Pid} || [Ref, Pid] <- ets:match(?TAB, {{listener_sup, '$1'}, '$2'})]. --spec set_addr(ranch:ref(), {inet:ip_address(), inet:port_number()}) -> ok. +-spec set_addr(ranch:ref(), {inet:ip_address(), inet:port_number()} | {undefined, undefined}) -> ok. set_addr(Ref, Addr) -> gen_server:call(?MODULE, {set_addr, Ref, Addr}). --spec get_addr(ranch:ref()) -> {inet:ip_address(), inet:port_number()}. +-spec get_addr(ranch:ref()) -> {inet:ip_address(), inet:port_number()} | {undefined, undefined}. get_addr(Ref) -> ets:lookup_element(?TAB, {addr, Ref}, 2). @@ -119,13 +122,21 @@ set_max_connections(Ref, MaxConnections) -> get_max_connections(Ref) -> ets:lookup_element(?TAB, {max_conns, Ref}, 2). +-spec set_transport_options(ranch:ref(), any()) -> ok. +set_transport_options(Ref, TransOpts) -> + gen_server:call(?MODULE, {set_trans_opts, Ref, TransOpts}). + +-spec get_transport_options(ranch:ref()) -> any(). +get_transport_options(Ref) -> + ets:lookup_element(?TAB, {trans_opts, Ref}, 2). + -spec set_protocol_options(ranch:ref(), any()) -> ok. set_protocol_options(Ref, ProtoOpts) -> - gen_server:call(?MODULE, {set_opts, Ref, ProtoOpts}). + gen_server:call(?MODULE, {set_proto_opts, Ref, ProtoOpts}). -spec get_protocol_options(ranch:ref()) -> any(). get_protocol_options(Ref) -> - ets:lookup_element(?TAB, {opts, Ref}, 2). + ets:lookup_element(?TAB, {proto_opts, Ref}, 2). -spec get_listener_start_args(ranch:ref()) -> [any()]. get_listener_start_args(Ref) -> @@ -144,9 +155,10 @@ init([]) -> [Ref, Pid] <- ets:match(?TAB, {{listener_sup, '$1'}, '$2'})], {ok, #state{monitors=ConnMonitors++ListenerMonitors}}. -handle_call({set_new_listener_opts, Ref, MaxConns, ProtoOpts, StartArgs}, _, State) -> +handle_call({set_new_listener_opts, Ref, MaxConns, TransOpts, ProtoOpts, StartArgs}, _, State) -> ets:insert(?TAB, {{max_conns, Ref}, MaxConns}), - ets:insert(?TAB, {{opts, Ref}, ProtoOpts}), + ets:insert(?TAB, {{trans_opts, Ref}, TransOpts}), + ets:insert(?TAB, {{proto_opts, Ref}, ProtoOpts}), ets:insert(?TAB, {{listener_start_args, Ref}, StartArgs}), {reply, ok, State}; handle_call({set_connections_sup, Ref, Pid}, _, @@ -177,8 +189,11 @@ handle_call({set_max_conns, Ref, MaxConns}, _, State) -> ConnsSup = get_connections_sup(Ref), ConnsSup ! {set_max_conns, MaxConns}, {reply, ok, State}; -handle_call({set_opts, Ref, Opts}, _, State) -> - ets:insert(?TAB, {{opts, Ref}, Opts}), +handle_call({set_trans_opts, Ref, Opts}, _, State) -> + ets:insert(?TAB, {{trans_opts, Ref}, Opts}), + {reply, ok, State}; +handle_call({set_proto_opts, Ref, Opts}, _, State) -> + ets:insert(?TAB, {{proto_opts, Ref}, Opts}), ConnsSup = get_connections_sup(Ref), ConnsSup ! {set_opts, Opts}, {reply, ok, State}; -- cgit v1.2.3