fix network ets

This commit is contained in:
anlicheng 2026-05-01 15:38:14 +08:00
parent e931721728
commit c3c5873cc0

View File

@ -13,8 +13,6 @@
-behaviour(gen_server). -behaviour(gen_server).
-define(FLOW_REPORT_INTERVAL, 60 * 1000).
%% broadcast, "FF-FF-FF-FF-FF-FF" %% broadcast, "FF-FF-FF-FF-FF-FF"
-define(BROADCAST_MAC, <<16#FF,16#FF,16#FF,16#FF,16#FF,16#FF>>). -define(BROADCAST_MAC, <<16#FF,16#FF,16#FF,16#FF,16#FF,16#FF>>).
@ -64,9 +62,7 @@
%% %%
throttle_key :: atom(), throttle_key :: atom(),
endpoint_table :: ets:tid(), endpoint_table :: ets:tid()
%%
forward_bytes = 0
}). }).
%%%=================================================================== %%%===================================================================
@ -192,9 +188,6 @@ init([Id]) when is_integer(Id) ->
%% %%
sdlan_network_coordinator:attach(self(), ThrottleKey), sdlan_network_coordinator:attach(self(), ThrottleKey),
%%
erlang:start_timer(?FLOW_REPORT_INTERVAL, self(), flow_report_ticker),
sdlan_domain_regedit:insert(Domain), sdlan_domain_regedit:insert(Domain),
%% %%
@ -383,21 +376,14 @@ handle_cast({update_hole, SessionToken, ClientId, Mac, Peer, NatType, V6Info}, S
{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({timeout, _, flow_report_ticker}, State = #state{network_id = NetworkId, forward_bytes = ForwardBytes}) ->
erlang:start_timer(?FLOW_REPORT_INTERVAL, self(), flow_report_ticker),
case ForwardBytes > 0 of
true ->
catch sdlan_api:network_forward_report(NetworkId, ForwardBytes);
false ->
ok
end,
logger:debug("[sdlan_network] metrics: ~p", [network_metrics(State)]),
{noreply, State#state{forward_bytes = 0}};
%% Channel进程退出, hole里面的数据也需要清理 %% Channel进程退出, hole里面的数据也需要清理
handle_info({'DOWN', _MRef, process, ChannelPid, Reason}, State = #state{network_id = NetworkId, endpoint_table = EndpointTable}) -> handle_info({'DOWN', _MRef, process, ChannelPid, Reason}, State = #state{network_id = NetworkId, endpoint_table = EndpointTable}) ->
logger:notice("[sdlan_network] network_id: ~p, channel_pid: ~p, close with reason: ~p", [NetworkId, ChannelPid, Reason]), logger:notice("[sdlan_network] network_id: ~p, channel_pid: ~p, close with reason: ~p", [NetworkId, ChannelPid, Reason]),
remove_channel_endpoints(ChannelPid, EndpointTable), remove_channel_endpoints(ChannelPid, EndpointTable),
{noreply, State};
handle_info(Info, State) ->
logger:debug("[sdlan_network] get unknown info: ~p", [Info]),
{noreply, State}. {noreply, State}.
%% @private %% @private
@ -651,12 +637,11 @@ format_endpoint({Mac, #endpoint{client_id = ClientId, ip = Ip, hole = undefined,
v6_info => V6InfoMap v6_info => V6InfoMap
}. }.
network_metrics(#state{network_id = NetworkId, endpoint_table = EndpointTable, forward_bytes = ForwardBytes}) -> network_metrics(#state{network_id = NetworkId, endpoint_table = EndpointTable}) ->
ProcInfo = maps:from_list(process_info(self(), [message_queue_len, memory, reductions])), ProcInfo = maps:from_list(process_info(self(), [message_queue_len, memory, reductions])),
ProcInfo#{ ProcInfo#{
network_id => NetworkId, network_id => NetworkId,
endpoint_count => endpoint_count(EndpointTable), endpoint_count => endpoint_count(EndpointTable),
forward_bytes => ForwardBytes,
channel_metrics => channel_metrics(EndpointTable) channel_metrics => channel_metrics(EndpointTable)
}. }.