Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save maxlapshin/156999 to your computer and use it in GitHub Desktop.
Save maxlapshin/156999 to your computer and use it in GitHub Desktop.
From 7bb6d8424ad931aee5b016c5986dcb4d0e12f5d2 Mon Sep 17 00:00:00 2001
From: Max Lapshin <max@maxidoors.ru>
Date: Tue, 28 Jul 2009 11:10:38 +0400
Subject: [PATCH] Added support for server redirection.
http://www.rabbitmq.com/clustering.html
---
lib/amqp/client.rb | 18 ++++++++++++++++++
1 files changed, 18 insertions(+), 0 deletions(-)
diff --git a/lib/amqp/client.rb b/lib/amqp/client.rb
index cbb9056..ff4d6bf 100644
--- a/lib/amqp/client.rb
+++ b/lib/amqp/client.rb
@@ -32,12 +32,30 @@ module AMQP
:capabilities => '',
:insist => @settings[:insist])
+ when Protocol::Connection::Redirect
+ host, port = method.host.split(":")
+ @settings[:host] = host if host
+ @settings[:port] = port.to_i if port
+ STDERR.puts "Redirect to #{@settings[:host]}:#{@settings[:port]}"
+ reconnect(true)
+
when Protocol::Connection::OpenOk
+ @settings[:known_hosts] = method.known_hosts.split(",")
+ # STDERR.puts "Got known hosts: #{@settings[:known_hosts].inspect}"
succeed(self)
when Protocol::Connection::Close
# raise Error, "#{method.reply_text} in #{Protocol.classes[method.class_id].methods[method.method_id]}"
STDERR.puts "#{method.reply_text} in #{Protocol.classes[method.class_id].methods[method.method_id]}"
+ if @settings[:known_hosts] && @settings[:known_hosts].size > 1
+ next_host = @settings[:known_hosts].shift
+ @settings[:known_hosts].push(next_host)
+ host, port = next_host.split(":")
+ @settings[:host] = host if host
+ @settings[:port] = port.to_i if port
+ STDERR.puts "Trying next host: #{next_host}"
+ reconnect(true)
+ end
when Protocol::Connection::CloseOk
@on_disconnect.call if @on_disconnect
--
1.5.4.3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment