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).
%% API
-export([start_link/2]).
-export([get_name/1]).
-export([start_link/1]).
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
@ -32,15 +31,11 @@
%%% 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)
-spec(start_link(Name :: atom(), Socket :: inet:socket()) ->
-spec(start_link(Socket :: inet:socket()) ->
{ok, Pid :: pid()} | ignore | {error, Reason :: term()}).
start_link(Name, Socket) when is_atom(Name) ->
gen_server:start_link({local, Name}, ?MODULE, [Socket], []).
start_link(Socket) ->
gen_server:start_link(?MODULE, [Socket], []).
%%%===================================================================
%%% gen_server callbacks

View File

@ -70,10 +70,9 @@ init([]) ->
{sndbuf, 5 * 1024 * 1024}
],
Workers = lists:map(fun(Id) ->
Name = sdlan_stun:get_name(Id),
Workers = lists:map(fun(_Id) ->
{ok, Socket} = gen_udp:open(Port, Opts),
{ok, Pid} = sdlan_stun:start_link(Name, Socket),
{ok, Pid} = sdlan_stun:start_link(Socket),
{Socket, Pid}
end, lists:seq(1, AcceptorNums)),
{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
@ -73,17 +73,4 @@ pools() ->
{ok, Pools} = application:get_env(sdlan, pools),
lists:map(fun({Name, PoolArgs, WorkerArgs}) ->
poolboy:child_spec(Name, [{name, {local, Name}}|PoolArgs], WorkerArgs)
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).
end, Pools).

View File

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