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

Support for passing Connection::Blocked frames to downstream clients #148

Merged
merged 3 commits into from
Mar 9, 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
2 changes: 1 addition & 1 deletion shard.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ shards:

amqp-client:
git: https://github.com/cloudamqp/amqp-client.cr.git
version: 1.2.1
version: 1.2.2

24 changes: 24 additions & 0 deletions spec/amqproxy_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -201,4 +201,28 @@ describe AMQProxy::Server do
s.stop_accepting_clients
end
end

it "passes connection blocked frames to clients" do
s = AMQProxy::Server.new("127.0.0.1", 5672, false)
done = Channel(Nil).new
begin
spawn { s.listen("127.0.0.1", 5673) }
Fiber.yield
AMQP::Client.start("amqp://localhost:5673") do |conn|
conn.on_blocked do
done.send nil
system("#{MAYBE_SUDO}rabbitmqctl set_vm_memory_high_watermark 0.8 > /dev/null").should be_true
end
conn.on_unblocked do
done.send nil
end
ch = conn.channel
system("#{MAYBE_SUDO}rabbitmqctl set_vm_memory_high_watermark 0.001 > /dev/null").should be_true
ch.basic_publish "foobar", "amq.fanout"
2.times { done.receive }
end
ensure
s.stop_accepting_clients
end
end
end
2 changes: 1 addition & 1 deletion src/amqproxy/client.cr
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ module AMQProxy
capabilities: {
consumer_priorities: true,
exchange_exchange_bindings: true,
"connection.blocked": false,
"connection.blocked": true,
authentication_failure_close: true,
per_consumer_qos: true,
"basic.nack": true,
Expand Down
22 changes: 20 additions & 2 deletions src/amqproxy/upstream.cr
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ module AMQProxy
end

# Frames from upstream (to client)
private def read_loop(socket, remote_address : String)
private def read_loop(socket, remote_address : String) # ameba:disable Metrics/CyclomaticComplexity
Log.context.set(remote_address: remote_address)
loop do
case frame = AMQ::Protocol::Frame.from_io(socket, IO::ByteFormat::NetworkEndian)
Expand All @@ -87,6 +87,9 @@ module AMQProxy
end
return
when AMQ::Protocol::Frame::Connection::CloseOk then return
when AMQ::Protocol::Frame::Connection::Blocked,
AMQ::Protocol::Frame::Connection::Unblocked
send_to_all_clients(frame)
when AMQ::Protocol::Frame::Channel::OpenOk # we assume it always succeeds
when AMQ::Protocol::Frame::Channel::CloseOk # when channel pool requested channel close
else
Expand Down Expand Up @@ -125,6 +128,21 @@ module AMQProxy
end
end

private def send_to_all_clients(frame : AMQ::Protocol::Frame::Connection)
Log.debug { "Sending broadcast frame to all client connections" }
clients = Set(Client).new
@channels_lock.synchronize do
@channels.each_value do |downstream_channel|
if dc = downstream_channel
clients << dc.client
end
end
end
clients.each do |client|
client.write frame
end
end

# Forward frames from client to upstream
def write(frame : AMQ::Protocol::Frame) : Nil
case frame
Expand Down Expand Up @@ -232,7 +250,7 @@ module AMQProxy
capabilities: {
consumer_priorities: true,
exchange_exchange_bindings: true,
"connection.blocked": false,
"connection.blocked": true,
authentication_failure_close: true,
per_consumer_qos: true,
"basic.nack": true,
Expand Down