feat: refactor hstreamdb connector to to avoid resources leaking
This commit is contained in:
parent
b089fba100
commit
4ee44972b2
|
@ -33,6 +33,9 @@
|
||||||
desc/1
|
desc/1
|
||||||
]).
|
]).
|
||||||
|
|
||||||
|
%% Allocatable resources
|
||||||
|
-define(hstreamdb_client, hstreamdb_client).
|
||||||
|
|
||||||
-define(DEFAULT_GRPC_TIMEOUT, timer:seconds(30)).
|
-define(DEFAULT_GRPC_TIMEOUT, timer:seconds(30)).
|
||||||
-define(DEFAULT_GRPC_TIMEOUT_RAW, <<"30s">>).
|
-define(DEFAULT_GRPC_TIMEOUT_RAW, <<"30s">>).
|
||||||
|
|
||||||
|
@ -43,17 +46,22 @@ callback_mode() -> always_sync.
|
||||||
on_start(InstId, Config) ->
|
on_start(InstId, Config) ->
|
||||||
start_client(InstId, Config).
|
start_client(InstId, Config).
|
||||||
|
|
||||||
on_stop(InstId, #{client := Client, producer := Producer}) ->
|
on_stop(InstId, _State) ->
|
||||||
StopClientRes = hstreamdb:stop_client(Client),
|
case emqx_resource:get_allocated_resources(InstId) of
|
||||||
StopProducerRes = hstreamdb:stop_producer(Producer),
|
#{client := Client, producer := Producer} ->
|
||||||
?SLOG(info, #{
|
StopClientRes = hstreamdb:stop_client(Client),
|
||||||
msg => "stop hstreamdb connector",
|
StopProducerRes = hstreamdb:stop_producer(Producer),
|
||||||
connector => InstId,
|
?SLOG(info, #{
|
||||||
client => Client,
|
msg => "stop hstreamdb connector",
|
||||||
producer => Producer,
|
connector => InstId,
|
||||||
stop_client => StopClientRes,
|
client => Client,
|
||||||
stop_producer => StopProducerRes
|
producer => Producer,
|
||||||
}).
|
stop_client => StopClientRes,
|
||||||
|
stop_producer => StopProducerRes
|
||||||
|
});
|
||||||
|
_ ->
|
||||||
|
ok
|
||||||
|
end.
|
||||||
|
|
||||||
-define(FAILED_TO_APPLY_HRECORD_TEMPLATE,
|
-define(FAILED_TO_APPLY_HRECORD_TEMPLATE,
|
||||||
{error, {unrecoverable_error, failed_to_apply_hrecord_template}}
|
{error, {unrecoverable_error, failed_to_apply_hrecord_template}}
|
||||||
|
@ -237,6 +245,9 @@ start_producer(
|
||||||
),
|
),
|
||||||
record_template => record_template(Options)
|
record_template => record_template(Options)
|
||||||
},
|
},
|
||||||
|
ok = emqx_resource:allocate_resource(InstId, ?hstreamdb_client, #{
|
||||||
|
client => Client, producer => Producer
|
||||||
|
}),
|
||||||
{ok, State};
|
{ok, State};
|
||||||
{error, {already_started, Pid}} ->
|
{error, {already_started, Pid}} ->
|
||||||
?SLOG(info, #{
|
?SLOG(info, #{
|
||||||
|
|
Loading…
Reference in New Issue