具有退避功能的主管

发布于 2024-09-25 09:07:05 字数 406 浏览 1 评论 0原文

我有一个具有两个工作进程的主管:一个处理与远程服务器的连接的 TCP 客户端和一个处理连接协议的 FSM。

在子进程中处理 TCP 错误会使代码变得非常复杂。所以我宁愿“让它崩溃”,但这有一个不同的问题:当服务器无法访问时,重新启动的最大次数将很快达到,并且主管将与我的整个应用程序一起崩溃,这对于这个案例。

我想要的是有一个带有回退的重启策略;如果做不到这一点,如果主管能够知道它何时因崩溃而重新启动(即,将其作为参数传递给 init 函数),那就足够了。我找到了此邮件列表线程,但是有更官方/更好测试的解决方案吗?

I have a supervisor with two worker processes: a TCP client which handles connection to a remote server and an FSM which handles the connection protocol.

Handling TCP errors in the child process complicates code significantly. So I'd prefer to "let it crash", but this has a different problem: when the server is unreachable, the maximum number of restarts will be quickly reached and the supervisor will crash along with my entire application, which is quite undesirable for this case.

What I'd like is to have a restart strategy with back-off; failing that, it would be good enough if the supervisor was aware when it is restarted due to a crash (i.e. had it passed as a parameter to the init function). I've found this mailing list thread, but is there a more official/better tested solution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

太傻旳人生 2024-10-02 09:07:05

您可能会发现我们的主管垫是一个良好的起点。我使用它来减慢那些必须正在运行但在启动时很快失败的事情的重新启动(例如遇到资源问题的端口)。

You might find our supervisor cushion to be a good starting point. I use it slow down the restart on things that must be running, but are failing quickly on startup (such as ports that are encountering a resource problem).

静水深流 2024-10-02 09:07:05

我在使用 erlang 时多次遇到这个问题,并尝试了很多解决方案。我认为我发现的最好的办法是有一个由主管启动并启动可能崩溃的额外进程。

它在启动时启动子级,等待子级退出并重新启动子级(有延迟)或根据需要退出。我认为这比回退服务器(您链接到的服务器)更简单,因为您只需要保留有关单个子项的状态。

我使用的另一个解决方案是必须将子进程作为临时进程启动,并有一个单独的进程来轮询并重新启动任何已崩溃的进程。

I've had this problem many times working with erlang and tried many solutions. I think the best best I've found is to have an extra process that is started by the supervisor and starts the that might crash.

It starts the child on start-up, awaits child exits and restarts the child (with a delay) or exits as appropriate. I think this is simpler than the back-off server (which you link to) as you only need to keep state regarding a single child.

Another solution that I've used is to have to start the child processes as transient and have a separate process that polls and issues restarts to any processes that have crashed.

ぽ尐不点ル 2024-10-02 09:07:05

因此,首先您需要在 init 中使用 process_flag(trap_exit, true) 来捕获子进程的提前终止。

然后您需要决定要延迟重新启动多长时间,例如 10 秒,在

handle_info({'EXIT', _Pid, Reason}, State) ->
    erlang:send_after(10000, self(), {die, Reason}),  
    {noreply, State};

Lastly 中执行此操作,让进程终止

handle_info({die, Reason}, State) ->
    {stop, Reason, State};

So first you want to catch an early termination of the child by using a process_flag(trap_exit, true) in your init.

Then you need to decide how long you want to delay a restart by, for example 10 sec., do this in the

handle_info({'EXIT', _Pid, Reason}, State) ->
    erlang:send_after(10000, self(), {die, Reason}),  
    {noreply, State};

Lastly, let the process die with

handle_info({die, Reason}, State) ->
    {stop, Reason, State};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文