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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: refactor influxdb connector to to avoid resources leaking #10924

Merged
merged 1 commit into from
Jun 5, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{application, emqx_bridge_influxdb, [
{description, "EMQX Enterprise InfluxDB Bridge"},
{vsn, "0.1.1"},
{vsn, "0.1.2"},
{registered, []},
{applications, [kernel, stdlib, influxdb]},
{env, []},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@

-type ts_precision() :: ns | us | ms | s.

%% Allocatable resources
-define(influx_client, influx_client).

-define(INFLUXDB_DEFAULT_PORT, 8086).

%% influxdb servers don't need parse
Expand All @@ -53,10 +56,20 @@
callback_mode() -> async_if_possible.

on_start(InstId, Config) ->
%% InstID as pool would be handled by influxdb client
%% so there is no need to allocate pool_name here
%% ehttpc for influxdb-v1/v2,
%% ecpool for influxdb-udp
%% See: influxdb:start_client/1
start_client(InstId, Config).

on_stop(_InstId, #{client := Client}) ->
influxdb:stop_client(Client).
on_stop(InstId, _State) ->
case emqx_resource:get_allocated_resources(InstId) of
#{?influx_client := Client} ->
influxdb:stop_client(Client);
JimMoen marked this conversation as resolved.
Show resolved Hide resolved
_ ->
ok
end.

on_query(InstId, {send_message, Data}, _State = #{write_syntax := SyntaxLines, client := Client}) ->
case data_to_points(Data, SyntaxLines) of
Expand Down Expand Up @@ -220,8 +233,12 @@ start_client(InstId, Config) ->
config => emqx_utils:redact(Config),
client_config => emqx_utils:redact(ClientConfig)
}),
try
do_start_client(InstId, ClientConfig, Config)
try do_start_client(InstId, ClientConfig, Config) of
Res = {ok, #{client := Client}} ->
ok = emqx_resource:allocate_resource(InstId, ?influx_client, Client),
Res;
{error, Reason} ->
{error, Reason}
catch
E:R:S ->
?tp(influxdb_connector_start_exception, #{error => {E, R}}),
Expand Down
1 change: 1 addition & 0 deletions changes/ee/feat-10924.en.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Refactored influxdb bridge connector to avoid resource leaks during crashes at creation.