Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Federation queue link should reconnect #788

Merged
merged 4 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions spec/upstream_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,35 @@ describe LavinMQ::Federation::Upstream do
end
end

it "should reconnect queue link after upstream disconnect" do
with_amqp_server do |s|
upstream, upstream_vhost, downstream_vhost = UpstreamSpecHelpers.setup_federation(s, "ef test upstream restart", nil, "upstream_q")
UpstreamSpecHelpers.start_link(upstream, "downstream_q", "queues")
s.users.add_permission("guest", "upstream", /.*/, /.*/, /.*/)
s.users.add_permission("guest", "downstream", /.*/, /.*/, /.*/)

upstream_vhost.declare_exchange("upstream_ex", "topic", true, false)
upstream_vhost.declare_queue("upstream_q", true, false)
downstream_vhost.declare_exchange("downstream_ex", "topic", true, false)
downstream_vhost.declare_queue("downstream_q", true, false)

wait_for { downstream_vhost.queues["downstream_q"].policy.try(&.name) == "FE" }
wait_for { upstream.links.first?.try &.state.running? }
sleep 0.1.seconds

# Disconnect the federation link
upstream_vhost.connections.each do |conn|
next unless conn.client_name.starts_with?("Federation link")
conn.close
end
sleep 0.1.seconds

# wait for federation link to be reconnected
wait_for { upstream.links.first?.try &.state.running? }
wait_for { upstream.links.first?.try { |l| l.@upstream_connection.try &.closed? == false } }
end
end

it "should continue after upstream restart" do
with_amqp_server do |s|
upstream, upstream_vhost, downstream_vhost = UpstreamSpecHelpers.setup_federation(s, "ef test upstream restart", "upstream_ex")
Expand Down
9 changes: 8 additions & 1 deletion src/lavinmq/federation/link.cr
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,14 @@ module LavinMQ
state(State::Running)
unless @federated_q.immediate_delivery?
@log.debug { "Waiting for consumers" }
@consumer_available.receive?
loop do
select
when @consumer_available.receive?
break
when timeout(1.second)
return if @upstream_connection.try &.closed?
end
end
end
q_name = q[:queue_name]
upstream_channel.basic_consume(q_name, no_ack: no_ack, tag: @upstream.consumer_tag, block: true) do |msg|
Expand Down
Loading