fix stun server

This commit is contained in:
anlicheng 2026-01-26 18:00:03 +08:00
parent c9adbb5e16
commit 6cce8f7e5e
4 changed files with 9 additions and 28 deletions

View File

@ -16,8 +16,7 @@
-behaviour(gen_server). -behaviour(gen_server).
%% API %% API
-export([start_link/2]). -export([start_link/1]).
-export([get_name/1]).
%% 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]).
@ -32,15 +31,11 @@
%%% API %%% API
%%%=================================================================== %%%===================================================================
-spec get_name(Id :: integer()) -> atom().
get_name(Id) when is_integer(Id) ->
list_to_atom("sdlan_stun:" ++ integer_to_list(Id)).
%% @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(), Socket :: inet:socket()) -> -spec(start_link(Socket :: inet:socket()) ->
{ok, Pid :: pid()} | ignore | {error, Reason :: term()}). {ok, Pid :: pid()} | ignore | {error, Reason :: term()}).
start_link(Name, Socket) when is_atom(Name) -> start_link(Socket) ->
gen_server:start_link({local, Name}, ?MODULE, [Socket], []). gen_server:start_link(?MODULE, [Socket], []).
%%%=================================================================== %%%===================================================================
%%% gen_server callbacks %%% gen_server callbacks

View File

@ -70,10 +70,9 @@ init([]) ->
{sndbuf, 5 * 1024 * 1024} {sndbuf, 5 * 1024 * 1024}
], ],
Workers = lists:map(fun(Id) -> Workers = lists:map(fun(_Id) ->
Name = sdlan_stun:get_name(Id),
{ok, Socket} = gen_udp:open(Port, Opts), {ok, Socket} = gen_udp:open(Port, Opts),
{ok, Pid} = sdlan_stun:start_link(Name, Socket), {ok, Pid} = sdlan_stun:start_link(Socket),
{Socket, Pid} {Socket, Pid}
end, lists:seq(1, AcceptorNums)), end, lists:seq(1, AcceptorNums)),
{ok, #state{workers = list_to_tuple(Workers), idx = 1, num = length(Workers)}}. {ok, #state{workers = list_to_tuple(Workers), idx = 1, num = length(Workers)}}.

View File

@ -65,7 +65,7 @@ init([]) ->
} }
], ],
{ok, {SupFlags, pools() ++ Specs ++ stun_specs()}}. {ok, {SupFlags, pools() ++ Specs}}.
%% internal functions %% internal functions
@ -74,16 +74,3 @@ pools() ->
lists:map(fun({Name, PoolArgs, WorkerArgs}) -> lists:map(fun({Name, PoolArgs, WorkerArgs}) ->
poolboy:child_spec(Name, [{name, {local, Name}}|PoolArgs], WorkerArgs) poolboy:child_spec(Name, [{name, {local, Name}}|PoolArgs], WorkerArgs)
end, Pools). end, Pools).
stun_specs() ->
{ok, StunServers} = application:get_env(sdlan, stun_servers),
lists:map(fun({Name, Port}) ->
#{
id => Name,
start => {sdlan_stun, start_link, [Name, Port]},
restart => permanent,
shutdown => 2000,
type => worker,
modules => ['sdlan_stun']
}
end, StunServers).

View File

@ -3,7 +3,7 @@
{http_server, [ {http_server, [
{port, 18082}, {port, 18082},
{acceptors, 500}, {acceptors, 1},
{max_connections, 10240}, {max_connections, 10240},
{backlog, 10240} {backlog, 10240}
]}, ]},