AMQP 连接丢失不会终止 parent 进程,因此永远不会重新连接
AMQP connection lost doesn't kill parent process, so reconnection never happens
我的 GenServer 有初始化功能。 Supervisor 正在照看它,应该在退出时重新启动它。
def init(_opts) do
username = get_conf(:username)
password = get_conf(:password)
host = get_conf(:host)
port = get_conf(:port)
vhost = String.replace(get_conf(:vhost), "/", "%2f")
{:ok, conn} = Connection.open("amqp://#{username}:#{password}@#{host}:#{port}/#{vhost}")
{:ok, chan} = Channel.open(conn)
state = %State{
exchange: get_conf(:exchange),
channel: chan,
routing_key: get_conf(:routing_key)
}
{:ok, state}
end
当我重新启动 RabbitMQ 时 sudo service rabbitmq-server restart
未建立新连接。
在调试栏中,我看到以下内容:
当我单击连接 pid <0.417.0> 时,我收到消息说进程不再存在。似乎进程已死,parent AmqpTransport
对此一无所知。
我怎样才能让AmqpTransport
和它的child一起死Connection
?
通过添加 trapping exits
并链接到 Connection
进程修复了它。此外,我在连接期间出现模式匹配错误以避免 reached_max_restart_intensity
.
@restart_delay 2000 # 2 seconds
def init(_opts) do
Process.flag(:trap_exit, true)
send(self(), :connect)
{:ok, nil}
end
def handle_info(:connect, state) do
#...
case Connection.open("amqp://#{username}:#{password}@#{host}:#{port}/#{vhost}") do
{:ok, conn} ->
Process.link(conn.pid)
# ...
{:noreply, state}
{:error, :econnrefused} ->
Logger.error("amqp transport failed with connection refused")
Process.send_after(self(), :connect, @restart_delay)
{:noreply, nil}
end
end
def handle_info({:EXIT, pid, reason}, state) do
Logger.error("amqp transport failed with #{inspect(reason)}")
Process.unlink(pid)
Process.send_after(self(), :connect, @restart_delay)
{:noreply, nil}
end
我的 GenServer 有初始化功能。 Supervisor 正在照看它,应该在退出时重新启动它。
def init(_opts) do
username = get_conf(:username)
password = get_conf(:password)
host = get_conf(:host)
port = get_conf(:port)
vhost = String.replace(get_conf(:vhost), "/", "%2f")
{:ok, conn} = Connection.open("amqp://#{username}:#{password}@#{host}:#{port}/#{vhost}")
{:ok, chan} = Channel.open(conn)
state = %State{
exchange: get_conf(:exchange),
channel: chan,
routing_key: get_conf(:routing_key)
}
{:ok, state}
end
当我重新启动 RabbitMQ 时 sudo service rabbitmq-server restart
未建立新连接。
在调试栏中,我看到以下内容:
AmqpTransport
对此一无所知。
我怎样才能让AmqpTransport
和它的child一起死Connection
?
通过添加 trapping exits
并链接到 Connection
进程修复了它。此外,我在连接期间出现模式匹配错误以避免 reached_max_restart_intensity
.
@restart_delay 2000 # 2 seconds
def init(_opts) do
Process.flag(:trap_exit, true)
send(self(), :connect)
{:ok, nil}
end
def handle_info(:connect, state) do
#...
case Connection.open("amqp://#{username}:#{password}@#{host}:#{port}/#{vhost}") do
{:ok, conn} ->
Process.link(conn.pid)
# ...
{:noreply, state}
{:error, :econnrefused} ->
Logger.error("amqp transport failed with connection refused")
Process.send_after(self(), :connect, @restart_delay)
{:noreply, nil}
end
end
def handle_info({:EXIT, pid, reason}, state) do
Logger.error("amqp transport failed with #{inspect(reason)}")
Process.unlink(pid)
Process.send_after(self(), :connect, @restart_delay)
{:noreply, nil}
end