Skip to content

Commit

Permalink
feat: support cust spawn opts
Browse files Browse the repository at this point in the history
  • Loading branch information
qzhuyan committed Dec 9, 2024
1 parent a9687f4 commit 6e7d0a7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
16 changes: 13 additions & 3 deletions src/quicer_conn_acceptor_sup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -63,22 +63,32 @@ start_link(ListenerH, ConnOpts) ->
{ok, {SupFlags :: supervisor:sup_flags(), [ChildSpec :: supervisor:child_spec()]}}
| ignore.
init([ListenerH, Opts]) ->
OptsTab = init_opts_tab(Opts),
SupFlags = #{
strategy => simple_one_for_one,
intensity => 1,
period => 5
},

OneChild = #{
id => ignored,
start => {quicer_connection, start_link, [undefined, ListenerH, Opts]},
start => {quicer_connection, start_acceptor, [ListenerH, OptsTab]},
restart => temporary,
shutdown => 5000,
type => worker
},

{ok, {SupFlags, [OneChild]}}.

%%%===================================================================
%%% Internal functions
%%%===================================================================
init_opts_tab({LOpts, COpts, SOpts}) ->
SharedTab = ets:new(quicer_listener_tab, [set, {keypos, 1}, {read_concurrency, true}]),
true = store_config(SharedTab, l_opts, LOpts),
true = store_config(SharedTab, c_opts, COpts),
true = store_config(SharedTab, s_opts, SOpts),
SharedTab.

store_config(Tab, K, V) when is_list(V) ->
store_config(Tab, K, maps:from_list(V));
store_config(Tab, K, V) ->
true = ets:insert(Tab, {K, V}).
15 changes: 14 additions & 1 deletion src/quicer_connection.erl
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@
-export([
start_link/3,
%% for server
start_acceptor/3,
start_link/4,
get_cb_state/1,
merge_cb_state/2,
Expand Down Expand Up @@ -216,6 +217,13 @@ start_link(undefined, Listener, {_LOpts, COpts, _SOpts} = Opts, Sup) when is_map
start_link(CallbackModule, Listener, Opts, Sup) ->
gen_server:start_link(?MODULE, [CallbackModule, Listener, Opts, Sup], []).

-define(DEFAULT_SPAWN_OPTS, [{spawn_opt, [link]}]).
%% @doc start acceptor with shared Listener confs
start_acceptor(ListenHandle, Tab, Sup) when is_pid(Sup) ->
[{c_opts, #{conn_callback := CallbackModule} = Conf}] = ets:lookup(Tab, c_opts),
StartOpts = maps:get(spawn_opts, Conf, ?DEFAULT_SPAWN_OPTS),
gen_server:start(?MODULE, [CallbackModule, ListenHandle, Tab, Sup], StartOpts).

-spec get_cb_state(ConnPid :: pid()) -> cb_state() | {error, any()}.
get_cb_state(ConnPid) ->
gen_server:call(ConnPid, get_cb_state, infinity).
Expand Down Expand Up @@ -313,7 +321,12 @@ init([CallbackModule, Listener, {_LOpts, COpts, SOpts}, Sup]) when CallbackModul
%% ignore, {stop, Reason} ...
Other ->
Other
end.
end;
init([CallbackModule, Listener, ConfTab, Sup]) ->
[{l_opts, LOpts}] = ets:lookup(ConfTab, l_opts),
[{c_opts, COpts}] = ets:lookup(ConfTab, c_opts),
[{s_opts, SOpts}] = ets:lookup(ConfTab, s_opts),
init([CallbackModule, Listener, {LOpts, COpts, SOpts}, Sup]).

%%--------------------------------------------------------------------
%% @private
Expand Down

0 comments on commit 6e7d0a7

Please sign in to comment.