[英]Erlang spawn a supervisor with a parameter
I have a server which is created like this : 我有一个这样创建的服务器:
gateway.erl (supervisor of the supervisor) -> gateway_sup.erl (supervisor of gen_servers) -> gateway_serv.erl (Where every client are handled). gateway.erl(主管的主管)-> gateway_sup.erl(gen_servers的主管)-> gateway_serv.erl(处理每个客户端的位置)。
This is pretty basic as I saw over the internet, most people are doing like this. 正如我在互联网上看到的那样,这是非常基本的,大多数人都这样做。
The Listen Socket is created on the gateway_sup.erl, and I would like to listen over multiple sockets in case some client port restrictions. 侦听套接字是在gateway_sup.erl上创建的,在某些客户端端口限制的情况下,我想侦听多个套接字。
So here's my code so far. 到目前为止,这是我的代码。
gateway.erl 网关
-export([start_link/0, init/1, startWithPort/1]).
start_link() ->
supervisor:start_link({local, ?MODULE}, ?MODULE, []).
init([]) ->
spawn_link(?MODULE, startWithPort, [8080]),
spawn_link(?MODULE, startWithPort, [443]),
{ok, {{simple_one_for_one, 3600, 3600},
[{socket,
{gateway_sup, start_link, []},
permanent, 1000, supervisor, [gateway_sup]}
]}}.
startWithPort(Port) ->
io:fwrite("Starting...: ~p~n", [Port]),
supervisor:start_child(?MODULE, [Port]).
gateway_sup.erl gateway_sup.erl
-export([start_socket/0, init/1, start_link/1]).
start_link(Port) ->
io:fwrite("gateway_sup start_link Port: ~p // ~p~n", [list_to_atom(atom_to_list(?MODULE) ++ atom_to_list(Port)), Port])
supervisor:start_link({local, list_to_atom(atom_to_list(?MODULE) ++ atom_to_list(Port))}, ?MODULE, [Port]).
init([Port]) ->
io:fwrite("gateway_sup Init with port: ~p~n", [Port]),
R = ssl:listen(Port, ?SSL_OPTIONS),
{ok, LSocket} = R,
spawn_link(fun empty_listeners/0),
{ok, {{simple_one_for_one, 3600, 3600},
[{socket,
{gateway_serv, start_link, [LSocket]},
temporary, 1000, worker, [gateway_serv]}
]}}.
empty_listeners() ->
[start_socket() || _ <- lists:seq(1,128)],
ok.
start_socket() ->
supervisor:start_child(?MODULE, []).
the start_link() function on gateway_sup.erl is never called. 从不调用gateway_sup.erl上的start_link()函数。 If the gateway is one_for_one and I'm not trying to pass a parameter, everything works fine, but I only do listen over one hardcoded port. 如果网关是one_for_one并且我没有尝试传递参数,则一切正常,但是我只通过一个硬编码端口进行侦听。
I can't see why it won't call the gateway_sup:start_link/1 ? 我看不到为什么它不会调用gateway_sup:start_link / 1吗?
Ok, found it ! 好,找到了! took me over a night for such minor mistake ! 这么小的错误把我带了一个晚上!
The error is when I'm creating the role within the start_link 错误是当我在start_link中创建角色时
list_to_atom(atom_to_list(?MODULE) ++ atom_to_list(Port))
has been replaced with 已被替换为
list_to_atom(atom_to_list(?MODULE) ++ lists:flatten(io_lib:format("~B", [Port])))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.