fix endpoint

This commit is contained in:
anlicheng 2024-05-07 22:09:13 +08:00
parent a3c428f408
commit 9f66f6c5ba
3 changed files with 7 additions and 14 deletions

View File

@ -18,12 +18,12 @@
-export_type([buffer/0]). -export_type([buffer/0]).
-record(buffer, { -record(buffer, {
endpoint, endpoint :: #endpoint{},
next_id = 1 :: integer(), next_id = 1 :: integer(),
%% %%
cursor = 0 :: integer(), cursor = 0 :: integer(),
%% ets存储的引用 %% ets存储的引用
tid :: reference(), tid :: ets:tid(),
%% %%
timer_pid :: pid(), timer_pid :: pid(),
%% %%
@ -49,7 +49,7 @@ new(Endpoint = #endpoint{id = Id}, WindowSize) when is_integer(WindowSize), Wind
EtsName = list_to_atom("endpoint_buffer_ets:" ++ integer_to_list(Id)), EtsName = list_to_atom("endpoint_buffer_ets:" ++ integer_to_list(Id)),
Tid = ets:new(EtsName, [ordered_set, private]), Tid = ets:new(EtsName, [ordered_set, private]),
%% %%
TimerPid = endpoint_timer:start_link(?RETRY_INTERVAL), {ok, TimerPid} = endpoint_timer:start_link(?RETRY_INTERVAL),
#buffer{cursor = 0, tid = Tid, timer_pid = TimerPid, endpoint = Endpoint, window_size = WindowSize}. #buffer{cursor = 0, tid = Tid, timer_pid = TimerPid, endpoint = Endpoint, window_size = WindowSize}.

View File

@ -30,7 +30,7 @@
%%%=================================================================== %%%===================================================================
%% @doc Spawns the server and registers the local name (unique) %% @doc Spawns the server and registers the local name (unique)
-spec(start_link(Name :: atom(), Endpoint :: #http_endpoint{}) -> -spec(start_link(Name :: atom(), Endpoint :: #endpoint{}) ->
{ok, Pid :: pid()} | ignore | {error, Reason :: term()}). {ok, Pid :: pid()} | ignore | {error, Reason :: term()}).
start_link(Name, Endpoint = #endpoint{config = #http_endpoint{}}) when is_atom(Name) -> start_link(Name, Endpoint = #endpoint{config = #http_endpoint{}}) when is_atom(Name) ->
gen_server:start_link({local, Name}, ?MODULE, [Endpoint], []). gen_server:start_link({local, Name}, ?MODULE, [Endpoint], []).
@ -82,7 +82,7 @@ handle_cast(cleanup, State = #state{buffer = Buffer}) ->
{noreply, NewState :: #state{}} | {noreply, NewState :: #state{}} |
{noreply, NewState :: #state{}, timeout() | hibernate} | {noreply, NewState :: #state{}, timeout() | hibernate} |
{stop, Reason :: term(), NewState :: #state{}}). {stop, Reason :: term(), NewState :: #state{}}).
handle_info({next_data, Id, _LocationCode, Body}, State = #state{buffer = Buffer, endpoint = #http_endpoint{url = Url}}) -> handle_info({next_data, Id, _LocationCode, Body}, State = #state{buffer = Buffer, endpoint = #endpoint{config = #http_endpoint{url = Url}}}) ->
Headers = [ Headers = [
{<<"content-type">>, <<"application/json">>} {<<"content-type">>, <<"application/json">>}
], ],
@ -91,7 +91,7 @@ handle_info({next_data, Id, _LocationCode, Body}, State = #state{buffer = Buffer
{ok, RespBody} = hackney:body(ClientRef), {ok, RespBody} = hackney:body(ClientRef),
hackney:close(ClientRef), hackney:close(ClientRef),
lager:debug("[http_postman] url: ~p, response is: ~p", [Url, RespBody]), lager:debug("[http_postman] url: ~p, response is: ~p", [Url, RespBody]),
NBuffer = endpoint_buffer:ack(Buffer, Id), NBuffer = endpoint_buffer:ack(Id, Buffer),
{noreply, State#state{buffer = NBuffer}}; {noreply, State#state{buffer = NBuffer}};
{ok, HttpCode, _, ClientRef} -> {ok, HttpCode, _, ClientRef} ->

View File

@ -9,7 +9,7 @@
-include("endpoint.hrl"). -include("endpoint.hrl").
-export([start_link/0]). -export([start_link/0]).
-export([ensured_endpoint_started/1, delete_endpoint/1, stat/0]). -export([ensured_endpoint_started/1, delete_endpoint/1]).
-export([init/1]). -export([init/1]).
@ -45,13 +45,6 @@ ensured_endpoint_started(Endpoint = #endpoint{}) ->
{error, Error} {error, Error}
end. end.
stat() ->
Children = supervisor:which_children(?MODULE),
lists:foreach(fun({Id, Pid, _, _}) ->
Stat = catch endpoint:get_stat(Pid),
lager:debug("[iot_endpoint] id: ~p, stat: ~p", [Id, Stat])
end, Children).
delete_endpoint(Id) when is_integer(Id) -> delete_endpoint(Id) when is_integer(Id) ->
Name = endpoint:get_name(Id), Name = endpoint:get_name(Id),
supervisor:terminate_child(?MODULE, Name), supervisor:terminate_child(?MODULE, Name),