fix
This commit is contained in:
parent
f7e827df38
commit
ffd786b96f
@ -196,7 +196,7 @@ handle_call({request_param, ReceiverPid}, _From, State = #state{socket = Socket,
|
|||||||
{noreply, NewState :: #state{}, timeout() | hibernate} |
|
{noreply, NewState :: #state{}, timeout() | hibernate} |
|
||||||
{stop, Reason :: term(), NewState :: #state{}}).
|
{stop, Reason :: term(), NewState :: #state{}}).
|
||||||
handle_cast({send_metric_data, Fields, Tags}, State = #state{socket = Socket}) ->
|
handle_cast({send_metric_data, Fields, Tags}, State = #state{socket = Socket}) ->
|
||||||
Body = jiffy:encode(#{<<"data">> => Fields, <<"tag">> => Tags}, [force_utf8]),
|
Body = jiffy:encode(#{<<"fields">> => Fields, <<"tags">> => Tags}, [force_utf8]),
|
||||||
Packet = <<0:32, ?PACKET_METRIC_DATA, Body/binary>>,
|
Packet = <<0:32, ?PACKET_METRIC_DATA, Body/binary>>,
|
||||||
ok = gen_tcp:send(Socket, Packet),
|
ok = gen_tcp:send(Socket, Packet),
|
||||||
|
|
||||||
|
|||||||
@ -20,7 +20,8 @@
|
|||||||
%% API
|
%% API
|
||||||
-export([start_link/2]).
|
-export([start_link/2]).
|
||||||
-export([get_name/1, get_pid/1, start_service/1, stop_service/1, attach_channel/2]).
|
-export([get_name/1, get_pid/1, start_service/1, stop_service/1, attach_channel/2]).
|
||||||
-export([push_arguments/2, push_metrics/2]).
|
-export([push_arguments/2, push_metrics/2, request_arguments/1, request_metrics/1]).
|
||||||
|
-export([metric_data/3, send_log/2, send_event/3, send_ai_event/3]).
|
||||||
|
|
||||||
%% gen_server callbacks
|
%% gen_server callbacks
|
||||||
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
|
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
|
||||||
@ -70,6 +71,24 @@ push_arguments(Pid, Args) when is_pid(Pid), is_binary(Args) ->
|
|||||||
push_metrics(Pid, Metrics) when is_pid(Pid), is_binary(Metrics) ->
|
push_metrics(Pid, Metrics) when is_pid(Pid), is_binary(Metrics) ->
|
||||||
gen_server:call(Pid, {push_metrics, Metrics}).
|
gen_server:call(Pid, {push_metrics, Metrics}).
|
||||||
|
|
||||||
|
request_arguments(Pid) when is_pid(Pid) ->
|
||||||
|
gen_server:call(Pid, request_arguments).
|
||||||
|
|
||||||
|
request_metrics(Pid) when is_pid(Pid) ->
|
||||||
|
gen_server:call(Pid, request_metrics).
|
||||||
|
|
||||||
|
metric_data(Pid, Fields, Tags) ->
|
||||||
|
ok.
|
||||||
|
|
||||||
|
send_log(Pid, Log) ->
|
||||||
|
ok.
|
||||||
|
|
||||||
|
send_event(Pid, EventType, Params) ->
|
||||||
|
ok.
|
||||||
|
|
||||||
|
send_ai_event(Pid, EventType, Params) ->
|
||||||
|
ok.
|
||||||
|
|
||||||
-spec attach_channel(pid(), pid()) -> ok | {error, Reason :: binary()}.
|
-spec attach_channel(pid(), pid()) -> ok | {error, Reason :: binary()}.
|
||||||
attach_channel(Pid, ChannelPid) when is_pid(Pid), is_pid(ChannelPid) ->
|
attach_channel(Pid, ChannelPid) when is_pid(Pid), is_pid(ChannelPid) ->
|
||||||
gen_server:call(Pid, {attach_channel, ChannelPid}).
|
gen_server:call(Pid, {attach_channel, ChannelPid}).
|
||||||
|
|||||||
@ -1,110 +1,240 @@
|
|||||||
%%%-------------------------------------------------------------------
|
%%%-------------------------------------------------------------------
|
||||||
%%% @author licheng5
|
%%% @author anlicheng
|
||||||
%%% @copyright (C) 2020, <COMPANY>
|
%%% @copyright (C) 2025, <COMPANY>
|
||||||
%%% @doc
|
%%% @doc
|
||||||
%%%
|
%%%
|
||||||
%%% @end
|
%%% @end
|
||||||
%%% Created : 10. 12月 2020 上午11:17
|
%%% Created : 30. 4月 2025 09:22
|
||||||
%%%-------------------------------------------------------------------
|
%%%-------------------------------------------------------------------
|
||||||
-module(efka_tcp_channel).
|
-module(efka_tcp_channel).
|
||||||
-author("licheng5").
|
-author("anlicheng").
|
||||||
-behaviour(gen_server).
|
|
||||||
|
|
||||||
%% 心跳包监测机制
|
-behaviour(gen_server).
|
||||||
-define(PING_TICKER, 15000).
|
|
||||||
|
|
||||||
%% API
|
%% API
|
||||||
-export([start_link/1]).
|
-export([start_link/1]).
|
||||||
-export([publish_command/4, send_event/3, stop/2, move_network/3]).
|
-export([push_metric/2, push_param/2]).
|
||||||
|
|
||||||
|
%% gen_server callbacks
|
||||||
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
|
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
|
||||||
|
|
||||||
|
-define(SERVER, ?MODULE).
|
||||||
|
|
||||||
|
%% 消息类型
|
||||||
|
|
||||||
|
%% 服务注册
|
||||||
|
-define(PACKET_REGISTER, 16).
|
||||||
|
%% 上传数据
|
||||||
|
-define(PACKET_METRIC_DATA, 3).
|
||||||
|
%% 消息响应
|
||||||
|
-define(PACKET_RESPONSE, 7).
|
||||||
|
%% efka下发给微服务参数
|
||||||
|
-define(PACKET_PUSH_PARAM, 5).
|
||||||
|
%% efka下发给微服务采集项
|
||||||
|
-define(PACKET_PUSH_METRIC, 6).
|
||||||
|
|
||||||
|
%% 微服务给efka发送log消息
|
||||||
|
-define(PACKET_LOG, 9).
|
||||||
|
|
||||||
|
%% 微服务从efka获取自身的采集项
|
||||||
|
-define(PACKET_REQUEST_METRIC, 10).
|
||||||
|
%% 微服务从efka获取自身的参数
|
||||||
|
-define(PACKET_REQUEST_PARAM, 12).
|
||||||
|
%% 微服务事件上报
|
||||||
|
-define(PACKET_EVENT, 15).
|
||||||
|
-define(PACKET_AI_EVENT, 16).
|
||||||
|
|
||||||
-record(state, {
|
-record(state, {
|
||||||
socket,
|
packet_id = 1,
|
||||||
%% 标记是否已经注册
|
socket :: gen_tcp:socket(),
|
||||||
is_registered = false,
|
service_id :: undefined | binary(),
|
||||||
%% 记录ping的次数
|
service_pid :: undefined | pid(),
|
||||||
ping_counter = 0,
|
is_registered = false :: boolean(),
|
||||||
%% 发送消息对应的id
|
|
||||||
packet_id = 1 :: integer(),
|
%% 请求响应的对应关系, #{packet_id => {ReceiverPid, Ref}}
|
||||||
%% 请求响应的对应关系
|
|
||||||
inflight = #{}
|
inflight = #{}
|
||||||
}).
|
}).
|
||||||
|
|
||||||
%% 向通道中写入消息
|
%%%===================================================================
|
||||||
-spec publish_command(Pid :: pid(), ReceiverPid :: pid(), CommandType :: integer(), Msg :: binary()) -> Ref :: reference().
|
%%% API
|
||||||
publish_command(Pid, ReceiverPid, CommandType, Msg) when is_pid(Pid), is_pid(ReceiverPid), is_integer(CommandType), is_binary(Msg) ->
|
%%%===================================================================
|
||||||
|
|
||||||
|
-spec push_param(ChannelPid :: pid(), Params :: binary()) -> {ok, Ref :: reference()}.
|
||||||
|
push_param(ChannelPid, Params) when is_pid(ChannelPid), is_binary(Params) ->
|
||||||
Ref = make_ref(),
|
Ref = make_ref(),
|
||||||
Pid ! {publish_command, ReceiverPid, Ref, CommandType, Msg},
|
gen_server:cast(ChannelPid, {push_param, Ref, self(), Params}),
|
||||||
Ref.
|
{ok, Ref}.
|
||||||
|
|
||||||
%% 网络迁移是一种特殊的指令信息,需要单独处理
|
-spec push_metric(ChannelPid :: pid(), Metrics :: binary()) -> {ok, Ref :: reference()}.
|
||||||
-spec move_network(Pid :: pid(), ReceiverPid :: pid(), NetworkPid :: pid()) -> Ref :: reference().
|
push_metric(ChannelPid, Metrics) when is_pid(ChannelPid), is_binary(Metrics) ->
|
||||||
move_network(Pid, ReceiverPid, NetworkPid) when is_pid(Pid), is_pid(ReceiverPid), is_pid(NetworkPid) ->
|
|
||||||
Ref = make_ref(),
|
Ref = make_ref(),
|
||||||
Pid ! {move_network, ReceiverPid, Ref, NetworkPid},
|
gen_server:cast(ChannelPid, {push_metric, Ref, self(), Metrics}),
|
||||||
Ref.
|
{ok, Ref}.
|
||||||
|
|
||||||
%% 向通道中写入消息
|
|
||||||
-spec send_event(Pid :: pid(), EventType :: integer(), Event :: binary()) -> no_return().
|
|
||||||
send_event(Pid, EventType, Event) when is_pid(Pid), is_integer(EventType), is_binary(Event) ->
|
|
||||||
Pid ! {send_event, EventType, Event}.
|
|
||||||
|
|
||||||
%% 关闭方法
|
|
||||||
-spec stop(Pid :: pid(), Reason :: any()) -> no_return().
|
|
||||||
stop(undefined, _Reason) ->
|
|
||||||
ok;
|
|
||||||
stop(Pid, Reason) when is_pid(Pid) ->
|
|
||||||
Pid ! {stop, Reason}.
|
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
|
||||||
%% esockd callback
|
|
||||||
%%--------------------------------------------------------------------
|
|
||||||
|
|
||||||
|
%% @doc Spawns the server and registers the local name (unique)
|
||||||
|
-spec(start_link(Socket :: gen_tcp:socket()) ->
|
||||||
|
{ok, Pid :: pid()} | ignore | {error, Reason :: term()}).
|
||||||
start_link(Socket) ->
|
start_link(Socket) ->
|
||||||
gen_server:start_link(?MODULE, [Socket], []).
|
gen_server:start_link(?MODULE, [Socket], []).
|
||||||
|
|
||||||
init([Socket]) ->
|
%%%===================================================================
|
||||||
efka_logger:debug("[sdlan_channel] get a new connection: ~p", [Socket]),
|
%%% gen_server callbacks
|
||||||
gen_tcp:controlling_process(Socket, self()),
|
%%%===================================================================
|
||||||
ok = inet:setopts(Socket, [binary, {active, true}, {packet, 4}]),
|
|
||||||
% erlang:start_timer(?PING_TICKER, self(), ping_ticker),
|
|
||||||
gen_server:enter_loop(?MODULE, [], #state{socket = Socket}).
|
|
||||||
|
|
||||||
handle_call(_Request, _From, State) ->
|
%% @private
|
||||||
|
%% @doc Initializes the server
|
||||||
|
-spec(init(Args :: term()) ->
|
||||||
|
{ok, State :: #state{}} | {ok, State :: #state{}, timeout() | hibernate} |
|
||||||
|
{stop, Reason :: term()} | ignore).
|
||||||
|
init([Socket]) ->
|
||||||
|
ok = inet:setopts(Socket, [{active, true}]),
|
||||||
|
lager:debug("[tcp_channel] get new socket: ~p", [Socket]),
|
||||||
|
{ok, #state{socket = Socket}}.
|
||||||
|
|
||||||
|
%% @private
|
||||||
|
%% @doc Handling call messages
|
||||||
|
-spec(handle_call(Request :: term(), From :: {pid(), Tag :: term()},
|
||||||
|
State :: #state{}) ->
|
||||||
|
{reply, Reply :: term(), NewState :: #state{}} |
|
||||||
|
{reply, Reply :: term(), NewState :: #state{}, timeout() | hibernate} |
|
||||||
|
{noreply, NewState :: #state{}} |
|
||||||
|
{noreply, NewState :: #state{}, timeout() | hibernate} |
|
||||||
|
{stop, Reason :: term(), Reply :: term(), NewState :: #state{}} |
|
||||||
|
{stop, Reason :: term(), NewState :: #state{}}).
|
||||||
|
handle_call(_Request, _From, State = #state{}) ->
|
||||||
{reply, ok, State}.
|
{reply, ok, State}.
|
||||||
|
|
||||||
handle_cast(_Msg, State) ->
|
%% @private
|
||||||
|
%% @doc Handling cast messages
|
||||||
|
-spec(handle_cast(Request :: term(), State :: #state{}) ->
|
||||||
|
{noreply, NewState :: #state{}} |
|
||||||
|
{noreply, NewState :: #state{}, timeout() | hibernate} |
|
||||||
|
{stop, Reason :: term(), NewState :: #state{}}).
|
||||||
|
%% 推送参数项目
|
||||||
|
handle_cast({push_param, Ref, ReceiverPid, Params}, State = #state{socket = Socket, packet_id = PacketId, inflight = Inflight}) ->
|
||||||
|
ok = gen_tcp:send(Socket, <<PacketId:32, ?PACKET_PUSH_PARAM:8, Params/binary>>),
|
||||||
|
{noreply, State#state{inflight = maps:put(PacketId, {ReceiverPid, Ref}, Inflight)}};
|
||||||
|
|
||||||
|
%% 推送采集项目
|
||||||
|
handle_cast({push_metric, Ref, ReceiverPid, Metrics}, State = #state{socket = Socket, packet_id = PacketId, inflight = Inflight}) ->
|
||||||
|
ok = gen_tcp:send(Socket, <<PacketId:32, ?PACKET_PUSH_METRIC:8, Metrics/binary>>),
|
||||||
|
{noreply, State#state{inflight = maps:put(PacketId, {ReceiverPid, Ref}, Inflight)}};
|
||||||
|
|
||||||
|
handle_cast(_Request, State = #state{}) ->
|
||||||
{noreply, State}.
|
{noreply, State}.
|
||||||
|
|
||||||
%% 网络流量统计
|
%% @private
|
||||||
handle_info({tcp, _Sock, Body}, State = #state{}) ->
|
%% @doc Handling all non call/cast messages
|
||||||
efka_logger:debug("[sdlan_channel] read body: ~p", [Body]),
|
-spec(handle_info(Info :: timeout() | term(), State :: #state{}) ->
|
||||||
|
{noreply, NewState :: #state{}} |
|
||||||
|
{noreply, NewState :: #state{}, timeout() | hibernate} |
|
||||||
|
{stop, Reason :: term(), NewState :: #state{}}).
|
||||||
|
%% 注册
|
||||||
|
handle_info({tcp, Socket, <<PacketId:32, ?PACKET_REGISTER:8, ServiceId/binary>>}, State = #state{socket = Socket}) ->
|
||||||
|
case efka_micro_service:get_pid(ServiceId) of
|
||||||
|
undefined ->
|
||||||
|
ok = gen_tcp:send(Socket, <<PacketId:32, ?PACKET_RESPONSE:8, 0:8, "service not running">>);
|
||||||
|
Pid when is_pid(Pid) ->
|
||||||
|
case efka_micro_service:attach_channel(Pid, self()) of
|
||||||
|
ok ->
|
||||||
|
ok = gen_tcp:send(Socket, <<PacketId:32, ?PACKET_RESPONSE:8, 1:8>>),
|
||||||
|
{noreply, State#state{service_id = ServiceId, service_pid = Pid, is_registered = true}};
|
||||||
|
{error, Reason} ->
|
||||||
|
ok = gen_tcp:send(Socket, <<PacketId:32, ?PACKET_RESPONSE:8, 10:8, Reason/binary>>),
|
||||||
|
{stop, normal, State}
|
||||||
|
end
|
||||||
|
end;
|
||||||
|
|
||||||
|
%% 请求参数
|
||||||
|
handle_info({tcp, Socket, <<PacketId:32, ?PACKET_REQUEST_PARAM:8>>}, State = #state{socket = Socket, service_pid = ServicePid, is_registered = true}) ->
|
||||||
|
{ok, Args} = efka_micro_service:request_arguments(ServicePid),
|
||||||
|
ok = gen_tcp:send(Socket, <<PacketId:32, ?PACKET_RESPONSE:8, Args/binary>>),
|
||||||
{noreply, State};
|
{noreply, State};
|
||||||
|
|
||||||
handle_info({tcp_error, Sock, Reason}, State = #state{socket = Sock}) ->
|
%% 请求采集项目
|
||||||
efka_logger:notice("[sdlan_channel] tcp_error: ~p", [Reason]),
|
handle_info({tcp, Socket, <<PacketId:32, ?PACKET_REQUEST_METRIC:8>>}, State = #state{socket = Socket, service_pid = ServicePid, is_registered = true}) ->
|
||||||
{stop, normal, State};
|
{ok, Metrics} = efka_micro_service:request_metrics(ServicePid),
|
||||||
|
ok = gen_tcp:send(Socket, <<PacketId:32, ?PACKET_RESPONSE:8, Metrics/binary>>),
|
||||||
|
{noreply, State};
|
||||||
|
|
||||||
handle_info({tcp_closed, Sock}, State = #state{socket = Sock}) ->
|
%% 数据项
|
||||||
efka_logger:notice("[sdlan_channel] tcp_closed", []),
|
handle_info({tcp, Socket, <<0:32, ?PACKET_METRIC_DATA:8, Data/binary>>}, State = #state{socket = Socket, service_pid = ServicePid, is_registered = true}) ->
|
||||||
{stop, normal, State};
|
case catch jiffy:decode(Data, [return_maps]) of
|
||||||
|
#{<<"fields">> := Fields, <<"tag">> := Tags} ->
|
||||||
|
efka_micro_service:metric_data(ServicePid, Fields, Tags);
|
||||||
|
_ ->
|
||||||
|
ok
|
||||||
|
end,
|
||||||
|
{noreply, State};
|
||||||
|
|
||||||
%% 关闭当前通道
|
%% 远程日志
|
||||||
handle_info({stop, Reason}, State) ->
|
handle_info({tcp, Socket, <<0:32, ?PACKET_LOG:8, Log/binary>>}, State = #state{socket = Socket, service_pid = ServicePid, is_registered = true}) ->
|
||||||
{stop, Reason, State};
|
efka_micro_service:send_log(ServicePid, Log),
|
||||||
|
{noreply, State};
|
||||||
|
|
||||||
handle_info(Info, State) ->
|
%% Event事件
|
||||||
efka_logger:debug("[sdlan_channel] get a unknown message: ~p, channel will closed", [Info]),
|
handle_info({tcp, Socket, <<0:32, ?PACKET_EVENT:8, EventData/binary>>}, State = #state{socket = Socket, service_pid = ServicePid, is_registered = true}) ->
|
||||||
|
case catch jiffy:decode(EventData, [return_maps]) of
|
||||||
|
#{<<"event_type">> := EventType, <<"params">> := Params} ->
|
||||||
|
efka_micro_service:send_event(ServicePid, EventType, Params);
|
||||||
|
_ ->
|
||||||
|
ok
|
||||||
|
end,
|
||||||
|
{noreply, State};
|
||||||
|
|
||||||
|
%% AIEvent事件
|
||||||
|
handle_info({tcp, Socket, <<0:32, ?PACKET_AI_EVENT:8, EventData/binary>>}, State = #state{socket = Socket, service_pid = ServicePid, is_registered = true}) ->
|
||||||
|
case catch jiffy:decode(EventData, [return_maps]) of
|
||||||
|
#{<<"event_type">> := EventType, <<"params">> := Params} ->
|
||||||
|
efka_micro_service:send_ai_event(ServicePid, EventType, Params);
|
||||||
|
_ ->
|
||||||
|
ok
|
||||||
|
end,
|
||||||
|
{noreply, State};
|
||||||
|
|
||||||
|
%% 收到端上的响应
|
||||||
|
handle_info({tcp, Socket, <<PacketId:32, ?PACKET_RESPONSE:8, Response/binary>>}, State = #state{socket = Socket, inflight = Inflight}) ->
|
||||||
|
case maps:take(PacketId, Inflight) of
|
||||||
|
error ->
|
||||||
|
lager:warning("[tcp_channel] get unknown publish response message: ~p, packet_id: ~p", [Response, PacketId]),
|
||||||
|
{noreply, State};
|
||||||
|
{{ReceiverPid, Ref}, NInflight} ->
|
||||||
|
case is_pid(ReceiverPid) andalso is_process_alive(ReceiverPid) of
|
||||||
|
true ->
|
||||||
|
case Response of
|
||||||
|
<<1:8, Result/binary>> ->
|
||||||
|
ReceiverPid ! {channel_reply, Ref, {ok, Result}};
|
||||||
|
<<0:8, Error/binary>> ->
|
||||||
|
ReceiverPid ! {channel_reply, Ref, {error, Error}}
|
||||||
|
end;
|
||||||
|
false ->
|
||||||
|
lager:warning("[tcp_channel] get publish response message: ~p, packet_id: ~p, but receiver_pid is deaded", [Response, PacketId])
|
||||||
|
end,
|
||||||
|
{noreply, State#state{inflight = NInflight}}
|
||||||
|
end;
|
||||||
|
|
||||||
|
handle_info(Info, State = #state{}) ->
|
||||||
|
lager:debug("[tcp_channel] get info: ~p", [Info]),
|
||||||
{noreply, State}.
|
{noreply, State}.
|
||||||
|
|
||||||
terminate(Reason, #state{}) ->
|
%% @private
|
||||||
efka_logger:notice("[sdlan_channel] stop with reason: ~p", [Reason]),
|
%% @doc This function is called by a gen_server when it is about to
|
||||||
|
%% terminate. It should be the opposite of Module:init/1 and do any
|
||||||
|
%% necessary cleaning up. When it returns, the gen_server terminates
|
||||||
|
%% with Reason. The return value is ignored.
|
||||||
|
-spec(terminate(Reason :: (normal | shutdown | {shutdown, term()} | term()),
|
||||||
|
State :: #state{}) -> term()).
|
||||||
|
terminate(_Reason, _State = #state{}) ->
|
||||||
ok.
|
ok.
|
||||||
|
|
||||||
code_change(_OldVsn, State, _Extra) ->
|
%% @private
|
||||||
|
%% @doc Convert process state when code is changed
|
||||||
|
-spec(code_change(OldVsn :: term() | {down, term()}, State :: #state{},
|
||||||
|
Extra :: term()) ->
|
||||||
|
{ok, NewState :: #state{}} | {error, Reason :: term()}).
|
||||||
|
code_change(_OldVsn, State = #state{}, _Extra) ->
|
||||||
{ok, State}.
|
{ok, State}.
|
||||||
|
|
||||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
%%%===================================================================
|
||||||
%% helper methods
|
%%% Internal functions
|
||||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
%%%===================================================================
|
||||||
|
|||||||
@ -1,203 +0,0 @@
|
|||||||
%%%-------------------------------------------------------------------
|
|
||||||
%%% @author anlicheng
|
|
||||||
%%% @copyright (C) 2025, <COMPANY>
|
|
||||||
%%% @doc
|
|
||||||
%%%
|
|
||||||
%%% @end
|
|
||||||
%%% Created : 30. 4月 2025 09:22
|
|
||||||
%%%-------------------------------------------------------------------
|
|
||||||
-module(tcp_channel).
|
|
||||||
-author("anlicheng").
|
|
||||||
|
|
||||||
-behaviour(gen_server).
|
|
||||||
|
|
||||||
%% API
|
|
||||||
-export([start_link/1]).
|
|
||||||
-export([push_metric/2, push_param/2, poll/2]).
|
|
||||||
|
|
||||||
%% gen_server callbacks
|
|
||||||
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
|
|
||||||
|
|
||||||
-define(SERVER, ?MODULE).
|
|
||||||
|
|
||||||
%% 消息类型
|
|
||||||
|
|
||||||
%% 服务注册
|
|
||||||
-define(PACKET_REGISTER, 16).
|
|
||||||
%% 上传数据
|
|
||||||
-define(PACKET_TYPE_METRIC_DATA, 3).
|
|
||||||
%% 消息响应
|
|
||||||
-define(PACKET_TYPE_RESPONSE, 7).
|
|
||||||
%% efka下发给微服务参数
|
|
||||||
-define(PACKET_TYPE_PUSH_PARAM, 5).
|
|
||||||
%% efka下发给微服务采集项
|
|
||||||
-define(PACKET_TYPE_PUSH_METRIC, 6).
|
|
||||||
%% 设备状态轮询: 增加日期: 2025-4-16
|
|
||||||
-define(PACKET_TYPE_POLL, 20).
|
|
||||||
%% 微服务给efka发送log消息
|
|
||||||
-define(PACKET_TYPE_LOG, 9).
|
|
||||||
%% 微服务从efka获取自身的采集项
|
|
||||||
-define(PACKET_TYPE_REQUEST_METRIC, 10).
|
|
||||||
%% 微服务从efka获取自身的参数
|
|
||||||
-define(PACKET_TYPE_REQUEST_PARAM, 12).
|
|
||||||
%% 微服务事件上报
|
|
||||||
-define(PACKET_TYPE_EVENT, 15).
|
|
||||||
|
|
||||||
-record(state, {
|
|
||||||
packet_id = 1,
|
|
||||||
socket :: gen_tcp:socket(),
|
|
||||||
micro_service_id :: undefined | binary(),
|
|
||||||
is_registered = false :: boolean(),
|
|
||||||
|
|
||||||
%% 请求响应的对应关系, #{packet_id => {ReceiverPid, Ref}}
|
|
||||||
inflight = #{}
|
|
||||||
}).
|
|
||||||
|
|
||||||
%%%===================================================================
|
|
||||||
%%% API
|
|
||||||
%%%===================================================================
|
|
||||||
|
|
||||||
-spec push_param(ChannelPid :: pid(), Params :: binary()) -> {ok, Ref :: reference()}.
|
|
||||||
push_param(ChannelPid, Params) when is_pid(ChannelPid), is_binary(Params) ->
|
|
||||||
Ref = make_ref(),
|
|
||||||
gen_server:cast(ChannelPid, {push_param, Ref, self(), Params}),
|
|
||||||
{ok, Ref}.
|
|
||||||
|
|
||||||
-spec push_metric(ChannelPid :: pid(), Metrics :: binary()) -> {ok, Ref :: reference()}.
|
|
||||||
push_metric(ChannelPid, Metrics) when is_pid(ChannelPid), is_binary(Metrics) ->
|
|
||||||
Ref = make_ref(),
|
|
||||||
gen_server:cast(ChannelPid, {push_metric, Ref, self(), Metrics}),
|
|
||||||
{ok, Ref}.
|
|
||||||
|
|
||||||
-spec poll(ChannelPid :: pid(), Command :: binary()) -> {ok, Ref :: reference()}.
|
|
||||||
poll(ChannelPid, Command) when is_pid(ChannelPid), is_binary(Command) ->
|
|
||||||
Ref = make_ref(),
|
|
||||||
gen_server:cast(ChannelPid, {poll, Ref, self(), Command}),
|
|
||||||
{ok, Ref}.
|
|
||||||
|
|
||||||
%% @doc Spawns the server and registers the local name (unique)
|
|
||||||
-spec(start_link(Socket :: gen_tcp:socket()) ->
|
|
||||||
{ok, Pid :: pid()} | ignore | {error, Reason :: term()}).
|
|
||||||
start_link(Socket) ->
|
|
||||||
gen_server:start_link(?MODULE, [Socket], []).
|
|
||||||
|
|
||||||
%%%===================================================================
|
|
||||||
%%% gen_server callbacks
|
|
||||||
%%%===================================================================
|
|
||||||
|
|
||||||
%% @private
|
|
||||||
%% @doc Initializes the server
|
|
||||||
-spec(init(Args :: term()) ->
|
|
||||||
{ok, State :: #state{}} | {ok, State :: #state{}, timeout() | hibernate} |
|
|
||||||
{stop, Reason :: term()} | ignore).
|
|
||||||
init([Socket]) ->
|
|
||||||
ok = inet:setopts(Socket, [{active, true}]),
|
|
||||||
lager:debug("[tcp_channel] get new socket: ~p", [Socket]),
|
|
||||||
{ok, #state{socket = Socket}}.
|
|
||||||
|
|
||||||
%% @private
|
|
||||||
%% @doc Handling call messages
|
|
||||||
-spec(handle_call(Request :: term(), From :: {pid(), Tag :: term()},
|
|
||||||
State :: #state{}) ->
|
|
||||||
{reply, Reply :: term(), NewState :: #state{}} |
|
|
||||||
{reply, Reply :: term(), NewState :: #state{}, timeout() | hibernate} |
|
|
||||||
{noreply, NewState :: #state{}} |
|
|
||||||
{noreply, NewState :: #state{}, timeout() | hibernate} |
|
|
||||||
{stop, Reason :: term(), Reply :: term(), NewState :: #state{}} |
|
|
||||||
{stop, Reason :: term(), NewState :: #state{}}).
|
|
||||||
handle_call(_Request, _From, State = #state{}) ->
|
|
||||||
{reply, ok, State}.
|
|
||||||
|
|
||||||
%% @private
|
|
||||||
%% @doc Handling cast messages
|
|
||||||
-spec(handle_cast(Request :: term(), State :: #state{}) ->
|
|
||||||
{noreply, NewState :: #state{}} |
|
|
||||||
{noreply, NewState :: #state{}, timeout() | hibernate} |
|
|
||||||
{stop, Reason :: term(), NewState :: #state{}}).
|
|
||||||
%% 推送参数项目
|
|
||||||
handle_cast({push_param, Ref, ReceiverPid, Params}, State = #state{socket = Socket, packet_id = PacketId, inflight = Inflight}) ->
|
|
||||||
ok = gen_tcp:send(Socket, <<PacketId:32, ?PACKET_TYPE_PUSH_PARAM:8, Params/binary>>),
|
|
||||||
{noreply, State#state{inflight = maps:put(PacketId, {ReceiverPid, Ref}, Inflight)}};
|
|
||||||
%% 推送采集项目
|
|
||||||
handle_cast({push_metric, Ref, ReceiverPid, Metrics}, State = #state{socket = Socket, packet_id = PacketId, inflight = Inflight}) ->
|
|
||||||
ok = gen_tcp:send(Socket, <<PacketId:32, ?PACKET_TYPE_PUSH_METRIC:8, Metrics/binary>>),
|
|
||||||
{noreply, State#state{inflight = maps:put(PacketId, {ReceiverPid, Ref}, Inflight)}};
|
|
||||||
%% 推送poll请求
|
|
||||||
handle_cast({poll, Ref, ReceiverPid, Command}, State = #state{socket = Socket, packet_id = PacketId, inflight = Inflight}) ->
|
|
||||||
ok = gen_tcp:send(Socket, <<PacketId:32, ?PACKET_TYPE_POLL:8, Command/binary>>),
|
|
||||||
{noreply, State#state{inflight = maps:put(PacketId, {ReceiverPid, Ref}, Inflight)}};
|
|
||||||
|
|
||||||
handle_cast(_Request, State = #state{}) ->
|
|
||||||
{noreply, State}.
|
|
||||||
|
|
||||||
%% @private
|
|
||||||
%% @doc Handling all non call/cast messages
|
|
||||||
-spec(handle_info(Info :: timeout() | term(), State :: #state{}) ->
|
|
||||||
{noreply, NewState :: #state{}} |
|
|
||||||
{noreply, NewState :: #state{}, timeout() | hibernate} |
|
|
||||||
{stop, Reason :: term(), NewState :: #state{}}).
|
|
||||||
%% 注册
|
|
||||||
handle_info({tcp, Socket, <<PacketId:32, ?PACKET_REGISTER:8, MicroServiceId/binary>>}, State = #state{socket = Socket}) ->
|
|
||||||
ok = gen_tcp:send(Socket, <<PacketId:32, ?PACKET_TYPE_RESPONSE:8, 1:8>>),
|
|
||||||
{noreply, State#state{micro_service_id = MicroServiceId, is_registered = true}};
|
|
||||||
|
|
||||||
handle_info({tcp, Socket, <<PacketId:32, ?PACKET_TYPE_METRIC_DATA:8, Body/binary>>}, State = #state{socket = Socket}) ->
|
|
||||||
lager:debug("[tcp_channel] get metric data: ~p", [Body]),
|
|
||||||
|
|
||||||
ok = gen_tcp:send(Socket, <<PacketId:32, ?PACKET_TYPE_RESPONSE:8, 1:8>>),
|
|
||||||
{noreply, State};
|
|
||||||
|
|
||||||
handle_info({tcp, Socket, <<PacketId:32, ?PACKET_TYPE_REQUEST_METRIC:8>>}, State = #state{socket = Socket}) ->
|
|
||||||
ok = gen_tcp:send(Socket, <<PacketId:32, ?PACKET_TYPE_RESPONSE:8, 1:8>>),
|
|
||||||
{noreply, State};
|
|
||||||
|
|
||||||
handle_info({tcp, Socket, <<PacketId:32, ?PACKET_TYPE_REQUEST_PARAM:8>>}, State = #state{socket = Socket}) ->
|
|
||||||
ok = gen_tcp:send(Socket, <<PacketId:32, ?PACKET_TYPE_RESPONSE:8, 1:8>>),
|
|
||||||
{noreply, State};
|
|
||||||
|
|
||||||
%% 收到端上的响应
|
|
||||||
handle_info({tcp, Socket, <<PacketId:32, ?PACKET_TYPE_RESPONSE:8, Response/binary>>}, State = #state{socket = Socket, inflight = Inflight}) ->
|
|
||||||
case maps:take(PacketId, Inflight) of
|
|
||||||
error ->
|
|
||||||
lager:warning("[tcp_channel] get unknown publish response message: ~p, packet_id: ~p", [Response, PacketId]),
|
|
||||||
{noreply, State};
|
|
||||||
{{ReceiverPid, Ref}, NInflight} ->
|
|
||||||
case is_pid(ReceiverPid) andalso is_process_alive(ReceiverPid) of
|
|
||||||
true ->
|
|
||||||
case Response of
|
|
||||||
<<1:8, Result/binary>> ->
|
|
||||||
ReceiverPid ! {channel_reply, Ref, {ok, Result}};
|
|
||||||
<<0:8, Error/binary>> ->
|
|
||||||
ReceiverPid ! {channel_reply, Ref, {error, Error}}
|
|
||||||
end;
|
|
||||||
false ->
|
|
||||||
lager:warning("[tcp_channel] get publish response message: ~p, packet_id: ~p, but receiver_pid is deaded", [Response, PacketId])
|
|
||||||
end,
|
|
||||||
{noreply, State#state{inflight = NInflight}}
|
|
||||||
end;
|
|
||||||
|
|
||||||
handle_info(Info, State = #state{}) ->
|
|
||||||
lager:debug("[tcp_channel] get info: ~p", [Info]),
|
|
||||||
{noreply, State}.
|
|
||||||
|
|
||||||
%% @private
|
|
||||||
%% @doc This function is called by a gen_server when it is about to
|
|
||||||
%% terminate. It should be the opposite of Module:init/1 and do any
|
|
||||||
%% necessary cleaning up. When it returns, the gen_server terminates
|
|
||||||
%% with Reason. The return value is ignored.
|
|
||||||
-spec(terminate(Reason :: (normal | shutdown | {shutdown, term()} | term()),
|
|
||||||
State :: #state{}) -> term()).
|
|
||||||
terminate(_Reason, _State = #state{}) ->
|
|
||||||
ok.
|
|
||||||
|
|
||||||
%% @private
|
|
||||||
%% @doc Convert process state when code is changed
|
|
||||||
-spec(code_change(OldVsn :: term() | {down, term()}, State :: #state{},
|
|
||||||
Extra :: term()) ->
|
|
||||||
{ok, NewState :: #state{}} | {error, Reason :: term()}).
|
|
||||||
code_change(_OldVsn, State = #state{}, _Extra) ->
|
|
||||||
{ok, State}.
|
|
||||||
|
|
||||||
%%%===================================================================
|
|
||||||
%%% Internal functions
|
|
||||||
%%%===================================================================
|
|
||||||
Loading…
x
Reference in New Issue
Block a user