Class: RedisConnection
- Inherits:
-
EventMachine::Connection
- Object
- EventMachine::Connection
- RedisConnection
- Defined in:
- lib/redis-proxy/redis_connection.rb
Instance Method Summary collapse
- #client_unbind ⇒ Object
- #connection_completed ⇒ Object
-
#initialize(config, client, request) ⇒ RedisConnection
constructor
A new instance of RedisConnection.
- #proxy_target_unbound ⇒ Object
- #receive_data(data) ⇒ Object
- #unbind ⇒ Object
Constructor Details
#initialize(config, client, request) ⇒ RedisConnection
Returns a new instance of RedisConnection.
3 4 5 6 7 8 9 10 11 12 13 |
# File 'lib/redis-proxy/redis_connection.rb', line 3 def initialize(config, client, request) @@nodes = config[:nodes] || [] @@reconnect_limit = config[:reconnect_limit] @@reconnect_delay = config[:reconnect_delay] @node_id = 0; @last_alive_node = 0; @reconnect_count = 0; @client = client @request = request end |
Instance Method Details
#client_unbind ⇒ Object
60 61 62 63 64 65 |
# File 'lib/redis-proxy/redis_connection.rb', line 60 def client_unbind puts "RedisConnection: client unbind" EventMachine.disable_proxy self @client_unbind = true close_connection end |
#connection_completed ⇒ Object
15 16 17 18 |
# File 'lib/redis-proxy/redis_connection.rb', line 15 def connection_completed puts "RedisConnection: connected to redis node: #{@node_id}" send_data "*1\r\n$4\r\nINFO\r\n" end |
#proxy_target_unbound ⇒ Object
34 35 36 37 |
# File 'lib/redis-proxy/redis_connection.rb', line 34 def proxy_target_unbound puts "RedisConnection: proxy target unbound" close_connection end |
#receive_data(data) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/redis-proxy/redis_connection.rb', line 20 def receive_data(data) if data =~ /role:master/ puts "RedisConnection: connected to a master node, proxying" @last_alive_node = @node_id @reconnect_count = 0 EventMachine.enable_proxy self, @client send_data @request else puts "RedisConnection: connected to a slave node, reconnecting" close_connection end end |
#unbind ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/redis-proxy/redis_connection.rb', line 39 def unbind puts "RedisConnection: connection unbound" EventMachine.disable_proxy self unless @client_unbind @node_id = (@node_id + 1) % @@nodes.count @reconnect_count += 1 if @reconnect_count == @@reconnect_limit @client.close_connection_after_writing else if @node_id == @last_alive_node puts "RedisConnection: finished one reconnect cycle, sleeping before trying again" sleep @@reconnect_delay end reconnect @@nodes[@node_id][:host], @@nodes[@node_id][:port] end end end |