我一直在阅读 Erlang and OTP In Action 这本书,并在第4章中尝试构建OTP应用程序的源代码。
有一个gen_server具有这些回调方法(full source):
%%%===================================================================
%%% gen_server callbacks
%%%===================================================================
init([Port]) ->
{ok, LSock} = gen_tcp:listen(Port, [{active, true}]),
{ok, #state{port = Port, lsock = LSock}, 0}.
handle_call(get_count, _From, State) ->
{reply, {ok, State#state.request_count}, State}.
handle_cast(stop, State) ->
{stop, normal, State}.
handle_info({tcp, Socket, RawData}, State) ->
do_rpc(Socket, RawData),
RequestCount = State#state.request_count,
{noreply, State#state{request_count = RequestCount + 1}};
handle_info(timeout, #state{lsock = LSock} = State) ->
{ok, _Sock} = gen_tcp:accept(LSock),
{noreply, State}.
terminate(_Reason, _State) ->
ok.
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
%%%===================================================================
%%% Internal functions
%%%===================================================================
主管init([])
方法如下所示(full source):
init([]) ->
Server = {tr_server, {tr_server, start_link, []},
permanent, 2000, worker, [tr_server]},
Children = [Server],
RestartStrategy = {one_for_one, 0, 1},
{ok, {RestartStrategy, Children}}.
我已使用application:start(tcp_rpc)
启动了该应用程序,然后将其移至应用程序中。当我退出telnet会话时,抛出了以下erlang错误:
=ERROR REPORT==== 11-Mar-2015::08:12:58 ===
** Generic server tr_server terminating
** Last message in was {tcp_closed,#Port<0.733>}
** When Server state == {state,1055,#Port<0.725>,5}
** Reason for termination ==
** {function_clause,[{tr_server,handle_info,
[{tcp_closed,#Port<0.733>},
{state,1055,#Port<0.725>,5}],
[{file,"src/tr_server.erl"},{line,87}]},
{gen_server,try_dispatch,4,
[{file,"gen_server.erl"},{line,593}]},
{gen_server,handle_msg,5,
[{file,"gen_server.erl"},{line,659}]},
{proc_lib,init_p_do_apply,3,
[{file,"proc_lib.erl"},{line,237}]}]}
=INFO REPORT==== 11-Mar-2015::08:12:58 ===
application: tcp_rpc
exited: shutdown
type: temporary
我可以看到应用程序崩溃了,因为它没有tcp_closed
的处理程序,但是我期望应用程序由主管重新启动,但它不是。
主管是否应该重新启动申请?
答案 0 :(得分:2)
您必须将重启策略更改为
RestartStrategy = {one_for_one, 10, 1},
因为0,1表示app根本无法重启。