Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor cleanups #4441

Merged
merged 6 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions big_tests/tests/component_helper.erl
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,15 @@ component_start_stream(Conn = #client{props = Props}, []) ->

ComponentHost = <<Component/binary, ".", Server/binary>>,
StreamStart = component_stream_start(ComponentHost, false),
ct:log("sent~n~p~n", [StreamStart]),
ok = escalus_connection:send(Conn, StreamStart),
StreamStartRep = escalus_connection:get_stanza(Conn, wait_for_stream),
ct:log("received~n~p~n", [StreamStartRep]),

#xmlstreamstart{attrs = Attrs} = StreamStartRep,
Id = proplists:get_value(<<"id">>, Attrs),

{Conn#client{props = [{sid, Id}|Props]}, []}.
{Conn#client{props = [{sid, Id} | Props]}, []}.

component_stream_start(Component, IsSubdomain) ->
Attrs1 = [{<<"to">>, Component},
Expand All @@ -91,9 +93,10 @@ component_handshake(Conn = #client{props = Props}, []) ->
{sid, SID} = lists:keyfind(sid, 1, Props),

Handshake = component_handshake_el(SID, Password),
ct:log("sent~n~p~n", [Handshake]),
ok = escalus_connection:send(Conn, Handshake),

HandshakeRep = escalus_connection:get_stanza(Conn, handshake),
ct:log("received~n~p~n", [HandshakeRep]),
case HandshakeRep of
#xmlel{name = <<"handshake">>, children = []} ->
{Conn, []};
Expand All @@ -110,8 +113,10 @@ component_start_stream_subdomain(Conn = #client{props = Props}, []) ->
{component, Component} = lists:keyfind(component, 1, Props),

StreamStart = component_stream_start(Component, true),
ct:log("sent~n~p~n", [StreamStart]),
ok = escalus_connection:send(Conn, StreamStart),
StreamStartRep = escalus_connection:get_stanza(Conn, wait_for_stream),
ct:log("received~n~p~n", [StreamStartRep]),

#xmlstreamstart{attrs = Attrs} = StreamStartRep,
Id = proplists:get_value(<<"id">>, Attrs),
Expand Down
36 changes: 7 additions & 29 deletions src/c2s/mongoose_c2s_listener.erl
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@
-behaviour(ranch_protocol).
-export([start_link/3]).

-behaviour(supervisor).
-export([start_link/1, init/1]).
-ignore_xref([start_link/1]).

%% Hook handlers
-export([handle_user_open_session/3]).

Expand All @@ -32,11 +28,14 @@ instrumentation(_Opts) ->

%% mongoose_listener
-spec start_listener(options()) -> ok.
start_listener(Opts) ->
start_listener(#{module := Module} = Opts) ->
HostTypes = ?ALL_HOST_TYPES,
TransportOpts = prepare_socket_opts(Opts),
ListenerId = mongoose_listener_config:listener_id(Opts),
ChildSpec = listener_child_spec(ListenerId, Opts),
mongoose_listener_sup:start_child(ChildSpec),
ok.
maybe_add_access_check(HostTypes, Opts, ListenerId),
ChildSpec = ranch:child_spec(ListenerId, ranch_tcp, TransportOpts, Module, Opts),
ChildSpec1 = ChildSpec#{id := ListenerId, modules => [?MODULE, ranch_embedded_sup]},
mongoose_listener_sup:start_child(ChildSpec1).

%% Hooks and handlers
-spec handle_user_open_session(mongoose_acc:t(), mongoose_c2s_hooks:params(), gen_hook:extra()) ->
Expand Down Expand Up @@ -67,19 +66,6 @@ start_link(Ref, Transport, Opts = #{hibernate_after := HibernateAfterTimeout}) -
mongoose_c2s:start_link({mongoose_c2s_ranch, {Transport, Ref}, Opts}, [{hibernate_after, HibernateAfterTimeout}]).

%% supervisor
-spec start_link(options()) -> any().
start_link(Opts) ->
supervisor:start_link(?MODULE, Opts).

-spec init(options()) -> {ok, {supervisor:sup_flags(), [supervisor:child_spec()]}}.
init(#{module := Module} = Opts) ->
HostTypes = ?ALL_HOST_TYPES,
TransportOpts = prepare_socket_opts(Opts),
ListenerId = mongoose_listener_config:listener_id(Opts),
maybe_add_access_check(HostTypes, Opts, ListenerId),
Child = ranch:child_spec(ListenerId, ranch_tcp, TransportOpts, Module, Opts),
{ok, {#{strategy => one_for_one, intensity => 100, period => 1}, [Child]}}.

maybe_add_access_check(_, #{access := all}, _) ->
ok;
maybe_add_access_check(HostTypes, _, ListenerId) ->
Expand All @@ -88,14 +74,6 @@ maybe_add_access_check(HostTypes, _, ListenerId) ->
|| HostType <- HostTypes ],
gen_hook:add_handlers(AclHooks).

listener_child_spec(ListenerId, Opts) ->
#{id => ListenerId,
start => {?MODULE, start_link, [Opts]},
restart => permanent,
shutdown => infinity,
type => supervisor,
modules => [?MODULE]}.

prepare_socket_opts(#{port := Port,
ip_version := IPVersion,
ip_tuple := IPTuple,
Expand Down
2 changes: 1 addition & 1 deletion src/component/mongoose_component.erl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
-module(mongoose_component).

%% API
-export([has_component/1,
dirty_get_all_components/1,
Expand All @@ -11,7 +12,6 @@
-export([node_cleanup/3]).

-include("mongoose.hrl").
-include("jlib.hrl").
-include("external_component.hrl").

-type domain() :: jid:server().
Expand Down
2 changes: 1 addition & 1 deletion src/ejabberd_service.erl
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@

-spec start_link(socket(), options()) -> ignore | {error, _} | {ok, pid()}.
start_link(SockData, Opts) ->
p1_fsm:start_link(ejabberd_service, [SockData, Opts],
p1_fsm:start_link(?MODULE, [SockData, Opts],

Check warning on line 139 in src/ejabberd_service.erl

View check run for this annotation

Codecov / codecov/patch

src/ejabberd_service.erl#L139

Added line #L139 was not covered by tests
fsm_limit_opts(Opts) ++ ?FSMOPTS).

-spec start_listener(options()) -> ok.
Expand Down
2 changes: 1 addition & 1 deletion src/ejabberd_sm.erl
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ get_vh_session_list(Server) ->
-spec get_node_sessions_number() -> non_neg_integer().
get_node_sessions_number() ->
Children = supervisor:which_children(mongoose_listener_sup),
Listeners = [Ref || {Ref, _, _, [mongoose_c2s_listener]} <- Children],
Listeners = [Ref || {Ref, _, _, [mongoose_c2s_listener | _]} <- Children],
lists:sum([maps:get(active_connections, ranch:info(Ref)) || Ref <- Listeners]).

-spec get_full_session_list() -> [session()].
Expand Down
28 changes: 17 additions & 11 deletions src/ejabberd_sup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

-export([start_link/0, init/1]).
-export([start_child/1, start_child/2, stop_child/1]).
-export([create_ets_table/2]).
-export([create_ets_table/2, template_supervisor_spec/2]).

-export([start_linked_child/2]).
-ignore_xref([start_linked_child/2]).
Expand All @@ -56,17 +56,17 @@ init([]) ->
ShaperSup = mongoose_shaper:child_spec(),
DomainSup = supervisor_spec(mongoose_domain_sup),
ReceiverSupervisor =
ejabberd_tmp_sup_spec(mongoose_transport_sup, [mongoose_transport_sup, mongoose_transport]),
template_supervisor_spec(mongoose_transport_sup, mongoose_transport),
C2SSupervisor =
ejabberd_tmp_sup_spec(mongoose_c2s_sup, [mongoose_c2s_sup, mongoose_c2s]),
template_supervisor_spec(mongoose_c2s_sup, mongoose_c2s),
S2SInSupervisor =
ejabberd_tmp_sup_spec(ejabberd_s2s_in_sup, [ejabberd_s2s_in_sup, ejabberd_s2s_in]),
template_supervisor_spec(ejabberd_s2s_in_sup, ejabberd_s2s_in),
S2SOutSupervisor =
ejabberd_tmp_sup_spec(ejabberd_s2s_out_sup, [ejabberd_s2s_out_sup, ejabberd_s2s_out]),
template_supervisor_spec(ejabberd_s2s_out_sup, ejabberd_s2s_out),
ServiceSupervisor =
ejabberd_tmp_sup_spec(ejabberd_service_sup, [ejabberd_service_sup, ejabberd_service]),
template_supervisor_spec(ejabberd_service_sup, ejabberd_service),
IQSupervisor =
ejabberd_tmp_sup_spec(ejabberd_iq_sup, [ejabberd_iq_sup, mongoose_iq_worker]),
template_supervisor_spec(ejabberd_iq_sup, mongoose_iq_worker),
{ok, {{one_for_one, 10, 1},
[StartIdServer,
PG,
Expand Down Expand Up @@ -112,10 +112,16 @@ stop_child(Proc) ->
supervisor:delete_child(ejabberd_sup, Proc),
ok.

ejabberd_tmp_sup_spec(Name, Args) ->
{Name,
{ejabberd_tmp_sup, start_link, Args},
permanent, infinity, supervisor, [ejabberd_tmp_sup]}.
-spec template_supervisor_spec(atom(), module()) -> supervisor:child_spec().
template_supervisor_spec(Name, Module) ->
#{
id => Name,
start => {mongoose_template_sup, start_link, [Name, Module]},
restart => permanent,
shutdown => infinity,
type => supervisor,
modules => [mongoose_template_sup]
}.

supervisor_spec(Mod) ->
{Mod, {Mod, start_link, []}, permanent, infinity, supervisor, [Mod]}.
Expand Down
40 changes: 0 additions & 40 deletions src/ejabberd_tmp_sup.erl

This file was deleted.

7 changes: 3 additions & 4 deletions src/jlib.erl
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,8 @@
-include("mongoose_rsm.hrl").

%% Stream types defined in exml/include/exml_stream.hrl
-type xmlstreamstart() :: #xmlstreamstart{}.
-type xmlstreamend() :: #xmlstreamend{}.
-type xmlstreamel() :: exml:element() | xmlstreamstart() | xmlstreamend().
-type xmlstreamerror() :: #xmlstreamerror{}.
-type xmlstreamel() :: exml:element() | exml_stream:start() | exml_stream:stop() | xmlstreamerror().

-type xmlcdata() :: #xmlcdata{}.

Expand All @@ -79,7 +78,7 @@
%% Copied from calendar:rfc3339_string() (because it is not exported)
-type rfc3339_string() :: [byte(), ...].

-export_type([xmlstreamstart/0, xmlstreamend/0, xmlstreamel/0,
-export_type([xmlstreamel/0, xmlstreamerror/0,
rsm_in/0, rsm_out/0,
xmlcdata/0,
xmlch/0,
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
13 changes: 3 additions & 10 deletions src/mod_bosh_socket.erl
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,7 @@ start_link(HostType, Sid, Peer, PeerCert, Opts) ->
-spec start_supervisor() -> {ok, pid()} | {error, any()}.
start_supervisor() ->
ChildId = ?BOSH_SOCKET_SUP,
ChildSpec =
{ChildId,
{ejabberd_tmp_sup, start_link,
[ChildId, ?MODULE]},
transient,
infinity,
supervisor,
[ejabberd_tmp_sup]},
ChildSpec = ejabberd_sup:template_supervisor_spec(ChildId, ?MODULE),
case supervisor:start_child(ejabberd_sup, ChildSpec) of
{ok, undefined} ->
{error, undefined};
Expand Down Expand Up @@ -903,7 +896,7 @@ get_attr(Attr, Element, Default) ->
end.


-spec stream_start(binary(), binary()) -> jlib:xmlstreamstart().
-spec stream_start(binary(), binary()) -> exml_stream:start().
stream_start(From, To) ->
#xmlstreamstart{name = <<"stream:stream">>,
attrs = [{<<"from">>, From},
Expand Down Expand Up @@ -954,7 +947,7 @@ is_stream_event(_) ->


%% @doc Bosh body for a session creation response.
-spec bosh_stream_start_body(jlib:xmlstreamstart(), state()) -> exml:element().
-spec bosh_stream_start_body(exml_stream:start(), state()) -> exml:element().
bosh_stream_start_body(#xmlstreamstart{attrs = Attrs}, #state{} = S) ->
#xmlel{name = <<"body">>,
attrs = [{<<"wait">>, integer_to_binary(S#state.wait)},
Expand Down
10 changes: 10 additions & 0 deletions src/mongoose_modules.erl
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
%% @doc Behaviour for sub-systems scoped to a `t:mongooseim:host_type/0'.
%%
%% This plays in counterpart to `m:mongoose_service', which enables sub-systems that
%% are global to the node.
%%
%% Examples of modules are
%% <ul>
%% <li>`m:mod_mam'</li>
%% <li>`m:mod_carboncopy'</li>
%% </ul>
-module(mongoose_modules).

-include("mongoose.hrl").
Expand Down
22 changes: 8 additions & 14 deletions src/mongoose_service.erl
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
%%==============================================================================
%% Copyright 2018 Erlang Solutions Ltd.
%% @doc Behaviour for sub-systems that are global to the node.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%% This plays in counterpart to `m:mongoose_modules', which enables sub-systems that
%% are scoped to a specific `t:mongooseim:host_type/0'.
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%==============================================================================

%% Examples of services are
%% <ul>
%% <li>`m:service_domain_db'</li>
%% <li>`m:service_mongoose_system_metrics'</li>
%% </ul>
-module(mongoose_service).

-include("mongoose.hrl").
Expand Down
24 changes: 24 additions & 0 deletions src/mongoose_template_sup.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
-module(mongoose_template_sup).

-behaviour(supervisor).

-export([start_link/2, init/1]).

-ignore_xref([init/1, start_link/2]).

-spec start_link(atom(), module()) -> supervisor:startlink_ret().
start_link(Name, Module) ->
supervisor:start_link({local, Name}, ?MODULE, Module).

-spec init(module()) -> {ok, {supervisor:sup_flags(), [supervisor:child_spec()]}}.
init(Module) ->
SupFlags = #{strategy => simple_one_for_one, intensity => 10, period => 1},
Spec = #{
id => undefined,
start => {Module, start_link, []},
restart => temporary,
shutdown => brutal_kill,
type => worker,
modules => [Module]
},
{ok, {SupFlags, [Spec]}}.
4 changes: 2 additions & 2 deletions src/mongoose_transport.erl
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
%%----------------------------------------------------------------------

-type send_xml_input() :: {xmlstreamelement, exml:element()}
| jlib:xmlstreamstart()
| jlib:xmlstreamend().
| exml_stream:start()
| exml_stream:stop().
-type peer() :: {inet:ip_address(), inet:port_number()}.
-type peername_return() :: {ok, peer()} | {error, inet:posix()}.
-type peercert_return() :: no_peer_cert | {ok, #'Certificate'{}}.
Expand Down
7 changes: 1 addition & 6 deletions src/muc/mod_muc.erl
Original file line number Diff line number Diff line change
Expand Up @@ -602,12 +602,7 @@ start_supervisor(HostType) ->

sup_spec(HostType) ->
Proc = gen_mod:get_module_proc(HostType, ejabberd_mod_muc_sup),
{Proc,
{ejabberd_tmp_sup, start_link, [Proc, mod_muc_room]},
permanent,
infinity,
supervisor,
[ejabberd_tmp_sup]}.
ejabberd_sup:template_supervisor_spec(Proc, mod_muc_room).

-spec stop_supervisor(jid:server()) -> ok | {error, Reason}
when Reason :: not_found | restarting | running | simple_one_for_one.
Expand Down