简体   繁体   English

Erlang OTP管理员gen_tcp-{error,eaddrinuse}

[英]Erlang OTP supervisor gen_tcp - {error,eaddrinuse}

I'm failing to see how adding a supervisor to a crashing gen_tcp:listen-thread would actually restart that worker. 我看不到如何在崩溃的gen_tcp:listen-thread中添加管理程序实际上会重新启动该工作程序。 Since crashing would render the port I wanna listen on useless for a brief moment. 由于崩溃将使端口失效,因此我想在短时间内听不到任何使用。 When a crash occur and I'm trying to manually restart my application I receive "{error,eaddrinuse}". 当发生崩溃并尝试手动重新启动应用程序时,我收到“ {error,eaddrinuse}”。 I haven't implemented any supervisor for this worker yet since I fail to see how it would work. 由于尚未看到工作原理,因此我尚未为该工作人员配备任何主管。

How do I restart a gen_tcp:listen? 如何重新启动gen_tcp:listen?

In most cases, as the listen socket is linked to the controlling process (the process that created it), a termination of this process will close the socket nicely and allow you to listen again on the same port. 在大多数情况下,由于侦听套接字已链接到控制进程(创建它的进程),因此该进程的终止将很好地关闭套接字,并允许您在同一端口上再次侦听。

For all other cases, you should pass {reuseaddr, true} option to gen_tcp:listen/2 . 对于所有其他情况,应将{reuseaddr, true}选项传递给gen_tcp:listen/2 Indeed, the listen socket of your application remains active for a brief moment after a crash, and this option allows you to reuse the address during this period. 实际上,崩溃后,应用程序的侦听套接字会在短时间内保持活动状态,此选项使您可以在此期间重用该地址。

Is the process managing the gen_tcp socket a gen_server? 管理gen_tcp套接字的进程是gen_server吗? If so, it will make your life easier. 如果是这样,它将使您的生活更轻松。

If it is a gen_server, add process_flag(trap_exit, true) to your init function. 如果它是gen_server,则将process_flag(trap_exit, true)到您的init函数中。 This makes it so that when your process "crashes", it calls the terminate/2 callback function prior to actually exiting the process. 这样一来,当您的进程“崩溃”时,它会在实际退出进程之前调用terminate/2回调函数。 Using this method, you can manually close your listening socket in the terminate function, thereby avoiding the irritating port cleanup delay. 使用此方法,您可以在终止功能中手动关闭侦听套接字,从而避免了烦人的端口清理延迟。

If you are not using a gen_server, the same principle still applies, but you must be much more explicit about catching your errors. 如果您不使用gen_server,则同样的原则仍然适用,但是在捕获错误方面必须更加明确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM