Merge pull request #10907 from HJianBo/refactor-on-stop-simple

feat: refactored some bridges to avoid leaking resources part.2
This commit is contained in:
JianBo He 2023-06-08 09:37:31 +08:00 committed by GitHub
commit 0b95bc1c1b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 19 additions and 59 deletions

View File

@ -143,12 +143,12 @@ on_start(
{error, Reason}
end.
on_stop(InstId, #{pool_name := PoolName}) ->
on_stop(InstId, _State) ->
?SLOG(info, #{
msg => "stopping_cassandra_connector",
connector => InstId
}),
emqx_resource_pool:stop(PoolName).
emqx_resource_pool:stop(InstId).
-type request() ::
% emqx_bridge.erl

View File

@ -274,12 +274,12 @@ connect(Options) ->
-spec on_stop(resource_id(), resource_state()) -> term().
on_stop(InstanceID, #{pool_name := PoolName}) ->
on_stop(InstanceID, _State) ->
?SLOG(info, #{
msg => "stopping clickouse connector",
connector => InstanceID
}),
emqx_resource_pool:stop(PoolName).
emqx_resource_pool:stop(InstanceID).
%% -------------------------------------------------------------------
%% on_get_status emqx_resouce callback and related functions

View File

@ -111,12 +111,12 @@ on_start(
Error
end.
on_stop(InstanceId, #{pool_name := PoolName}) ->
on_stop(InstanceId, _State) ->
?SLOG(info, #{
msg => "stopping_dynamo_connector",
connector => InstanceId
}),
emqx_resource_pool:stop(PoolName).
emqx_resource_pool:stop(InstanceId).
on_query(InstanceId, Query, State) ->
do_query(InstanceId, Query, State).

View File

@ -89,12 +89,12 @@ on_start(
Error
end.
on_stop(InstanceId, #{pool_name := PoolName} = _State) ->
on_stop(InstanceId, _State) ->
?SLOG(info, #{
msg => "stopping_opents_connector",
connector => InstanceId
}),
emqx_resource_pool:stop(PoolName).
emqx_resource_pool:stop(InstanceId).
on_query(InstanceId, Request, State) ->
on_batch_query(InstanceId, [Request], State).

View File

@ -261,12 +261,15 @@ on_start(
-spec on_stop(resource_id(), resource_state()) -> term().
on_stop(
ResourceID,
#{poolname := PoolName} = _State
_State
) ->
?SLOG(info, #{
msg => "stopping RabbitMQ connector",
connector => ResourceID
}),
stop_clients_and_pool(ResourceID).
stop_clients_and_pool(PoolName) ->
Workers = [Worker || {_WorkerName, Worker} <- ecpool:workers(PoolName)],
Clients = [
begin

View File

@ -108,7 +108,6 @@ on_start(
Prepares = parse_prepare_sql(Config),
State = Prepares#{pool_name => InstanceId, query_opts => query_opts(Config)},
ok = emqx_resource:allocate_resource(InstanceId, pool_name, InstanceId),
case emqx_resource_pool:start(InstanceId, ?MODULE, Options) of
ok ->
{ok, State};
@ -121,12 +120,7 @@ on_stop(InstanceId, _State) ->
msg => "stopping_tdengine_connector",
connector => InstanceId
}),
case emqx_resource:get_allocated_resources(InstanceId) of
#{pool_name := PoolName} ->
emqx_resource_pool:stop(PoolName);
_ ->
ok
end.
emqx_resource_pool:stop(InstanceId).
on_query(InstanceId, {query, SQL}, State) ->
do_query(InstanceId, SQL, State);

View File

@ -219,7 +219,6 @@ on_start(
base_path => BasePath,
request => preprocess_request(maps:get(request, Config, undefined))
},
ok = emqx_resource:allocate_resource(InstId, pool_name, InstId),
case ehttpc_sup:start_pool(InstId, PoolOpts) of
{ok, _} -> {ok, State};
{error, {already_started, _}} -> {ok, State};
@ -231,12 +230,7 @@ on_stop(InstId, _State) ->
msg => "stopping_http_connector",
connector => InstId
}),
case emqx_resource:get_allocated_resources(InstId) of
#{pool_name := PoolName} ->
ehttpc_sup:stop_pool(PoolName);
_ ->
ok
end.
ehttpc_sup:stop_pool(InstId).
on_query(InstId, {send_message, Msg}, State) ->
case maps:get(request, State, undefined) of

View File

@ -97,7 +97,6 @@ on_start(
{pool_size, PoolSize},
{auto_reconnect, ?AUTO_RECONNECT_INTERVAL}
],
ok = emqx_resource:allocate_resource(InstId, pool_name, InstId),
case emqx_resource_pool:start(InstId, ?MODULE, Opts ++ SslOpts) of
ok -> {ok, #{pool_name => InstId}};
{error, Reason} -> {error, Reason}
@ -108,12 +107,7 @@ on_stop(InstId, _State) ->
msg => "stopping_ldap_connector",
connector => InstId
}),
case emqx_resource:get_allocated_resources(InstId) of
#{pool_name := PoolName} ->
emqx_resource_pool:stop(PoolName);
_ ->
ok
end.
emqx_resource_pool:stop(InstId).
on_query(InstId, {search, Base, Filter, Attributes}, #{pool_name := PoolName} = State) ->
Request = {Base, Filter, Attributes},

View File

@ -183,7 +183,6 @@ on_start(
{worker_options, init_worker_options(maps:to_list(NConfig), SslOpts)}
],
Collection = maps:get(collection, Config, <<"mqtt">>),
ok = emqx_resource:allocate_resource(InstId, pool_name, InstId),
case emqx_resource_pool:start(InstId, ?MODULE, Opts) of
ok ->
{ok, #{
@ -200,12 +199,7 @@ on_stop(InstId, _State) ->
msg => "stopping_mongodb_connector",
connector => InstId
}),
case emqx_resource:get_allocated_resources(InstId) of
#{pool_name := PoolName} ->
emqx_resource_pool:stop(PoolName);
_ ->
ok
end.
emqx_resource_pool:stop(InstId).
on_query(
InstId,

View File

@ -124,7 +124,6 @@ on_start(
]
),
State = parse_prepare_sql(Config),
ok = emqx_resource:allocate_resource(InstId, pool_name, InstId),
case emqx_resource_pool:start(InstId, ?MODULE, Options ++ SslOpts) of
ok ->
{ok, init_prepare(State#{pool_name => InstId})};
@ -146,12 +145,7 @@ on_stop(InstId, _State) ->
msg => "stopping_mysql_connector",
connector => InstId
}),
case emqx_resource:get_allocated_resources(InstId) of
#{pool_name := PoolName} ->
emqx_resource_pool:stop(PoolName);
_ ->
ok
end.
emqx_resource_pool:stop(InstId).
on_query(InstId, {TypeOrKey, SQLOrKey}, State) ->
on_query(InstId, {TypeOrKey, SQLOrKey, [], default_timeout}, State);

View File

@ -121,7 +121,6 @@ on_start(
{pool_size, PoolSize}
],
State = parse_prepare_sql(Config),
ok = emqx_resource:allocate_resource(InstId, pool_name, InstId),
case emqx_resource_pool:start(InstId, ?MODULE, Options ++ SslOpts) of
ok ->
{ok, init_prepare(State#{pool_name => InstId, prepare_statement => #{}})};
@ -138,12 +137,7 @@ on_stop(InstId, _State) ->
msg => "stopping postgresql connector",
connector => InstId
}),
case emqx_resource:get_allocated_resources(InstId) of
#{pool_name := PoolName} ->
emqx_resource_pool:stop(PoolName);
_ ->
ok
end.
emqx_resource_pool:stop(InstId).
on_query(InstId, {TypeOrKey, NameOrSQL}, State) ->
on_query(InstId, {TypeOrKey, NameOrSQL, []}, State);

View File

@ -1,8 +0,0 @@
Refactored some bridges to avoid leaking resources during crashes at creation, including:
- TDEngine
- WebHook
- LDAP
- MongoDB
- MySQL
- PostgreSQL
- Redis

View File

@ -0,0 +1 @@
Refactored most of the bridges to avoid resource leaks during crashes during creation.