Erlang:OTP - 正确传递儿童论点;工人退出例外

时间:2015-09-06 06:05:04

标签: erlang otp

我有一个主管/工作树,如下组织:

game_super -> {keyserver: worker, loginserver: super}
loginserver -> loginlistener: worker (socket pool)

密钥服务器工作正常。它只返回它用于构造登录令牌的随机字节字符串。它会定期更换。

我的登录服务器用于在R16下工作。然后我升级到R18,因为我想使用地图。 IDK,如果这是问题......我怀疑它。我做的另一个改变是我需要地图的原因是为了方便设置孩子。

我有多个redis服务器,每个服务器都有自己的用途。一个是限速。这是目前唯一正在运营的人。我在game_core:redis_init()中致电game_supervisor:init([])。它返回{ok,{#MapPids, #MapScriptKeys}。其中#MapScriptKeys值是包含LUA脚本的所有Sha哈希值的元组。

我得到exception exit: {shutdown,{failed_to_start_child,login,<0.178.0>}} Pids明显改变,但这是调用game_supervisor:start_inshell()后的最后一个结果。

我在io:format("~p\n",[State]), {ok, State}之前插入login_listener:init(State)并打印出所有内容......一次。在返回之前考虑到了......它是如何失败的?

代码基于:http://learnyousomeerlang.com/buckets-of-sockets

这类似于:Erlang supervisor exception on starting worker,其中同名的名字被重用于儿童?

游戏主管

-module(game_supervisor).
-behavior(supervisor).

-include("constants.hrl").

-export([start/0, start/1, start_inshell/0, init/1]).

start(Args) ->
    spawn(fun() ->
            supervisor:start_link({local, ?MODULE}, ?MODULE, Args)
        end).

start_inshell() ->
    {ok, Pid} = supervisor:start_link({local, ?MODULE}, ?MODULE, []),
    unlink(Pid).

start() -> start([]).

init([]) ->
    {ok, RedisState} = game_core:redis_init(),
    {ok, {
        {one_for_one, 2, 10}, [ %more than 2 restarts w/i 10 seconds all killed
        {key,
            {key_server, start_link, []},
            permanent,
            10000,
            worker,
            [key_server]},
        {login,
            {login_server, start_link, [RedisState]},
            permanent,
            10000,
            supervisor,
            [login_server]}%,
%       {game,
%           {game_server, start_link, [RedisState]},
%           permanent,
%           10000,
%           supervisor,
%           [game_server]}
    ]}}.

登录服务器

-module(login_server).
-behavior(supervisor).

-include("constants.hrl").

-export([start_link/0, start_link/1, init/1, start_socket/0]).

start_link(Args) ->
    spawn(fun() ->
            supervisor:start_link({local, ?MODULE}, ?MODULE, Args)
    end).

start_link() -> start_link([]).

init({Pid,Scripts}) ->
    process_flag(trap_exit, true),
    {ok, Socket} = gen_tcp:listen(?PORT_LISTEN_LOGIN, [{active,once}, {reuseaddr, true}]),

    %Construct socket pool
    spawn_link(fun start_pool/0),

    %Put all redis information for login clients here
    RedisState = {maps:get(limiter,Pid),maps:get(limiter,Scripts)},

    %Child specification
    {ok, {
        {simple_one_for_one, 2, 10}, [  %more than 2 restarts w/i 10 seconds all killed
        {listener,
            {login_listener, start_link, [{Socket, RedisState}]},
            temporary,
            1000,
            worker,
            [login_listener]}
    ]}}.

start_pool() ->
    [start_socket() || _ <- lists:seq(1,32)],
    ok.

start_socket() -> supervisor:start_child(?MODULE, []).

登录处理程序

-module(login_listener).
-behavior(gen_server).

-include("constants.hrl").

-export([start_link/1]).

%required callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).

start_link(State) ->
    gen_server:start_link(?MODULE, State,[]).

init(State) ->
    process_flag(trap_exit, true),
    gen_server:cast(self(), accept),
    {ok, State}.

handle_cast(accept, State) ->                   %Param(s): Msg, State
    {Socket, RedisState} = State,

    {ok, AcceptSocket} = gen_tcp:accept(Socket),
    login_server:start_socket(),
    {noreply, {AcceptSocket, RedisState}}.

handle_info({tcp, Socket, Str}, State) ->           %Param(s): Info
    %%Login
    {_, {PidLimiter,{ShaLimiter}}} = State,
    {ok, {ClientIP, _ }} = inet:peername(Socket),

    {ok, Result} = game_core:rate_limit(PidLimiter, ShaLimiter, ClientIP),

    ok = inet:setopts(Socket, [{active, once}]),
    {noreply, State}.

%%Surpress warnings
handle_call(_, _, State) -> {noreply, State}.       %Param(s): Request, From
terminate(_, _) -> ok.                              %Param(s): Reason, State
code_change(_, State,_) -> {ok, State}.         %Param(s): OldVsn, Extra

1 个答案:

答案 0 :(得分:2)

为什么你的主管开始调用spawn/1的函数?对于game_supervisor,请更改此项:

start(Args) ->
    spawn(fun() ->
            supervisor:start_link({local, ?MODULE}, ?MODULE, Args)
        end).

到此:

start(Args) ->
    supervisor:start_link({local, ?MODULE}, ?MODULE, Args).

对于login_server,请更改此内容:

start_link(Args) ->
    spawn(fun() ->
            supervisor:start_link({local, ?MODULE}, ?MODULE, Args)
    end).

到此:

start_link(Args) ->
    supervisor:start_link({local, ?MODULE}, ?MODULE, Args).