Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0498fbb

Browse files
authored
Merge pull request #9 from shiftky/fix_connection_cache
Fix connection cache
2 parents 87562fd + 9f3e0e3 commit 0498fbb

File tree

2 files changed

+32
-14
lines changed

2 files changed

+32
-14
lines changed

‎mrblib/mrb_rediscluster.rb‎

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,14 @@ def get_cluster_nodes(nodes)
4949
host, port = ip_port.split(':')
5050
flags = flags.split(',')
5151
flags.delete('myself')
52-
ret[id] = {
53-
host: host,
54-
port: port.to_i,
55-
name: "#{host}:#{port}",
56-
flags: flags
57-
}
52+
if flags.include?('master') or flags.include?('slave')
53+
ret[id] = {
54+
host: host,
55+
port: port.to_i,
56+
name: "#{host}:#{port}",
57+
flags: flags
58+
}
59+
end
5860
end
5961
return ret
6062
end
@@ -117,6 +119,7 @@ def send_cluster_command(argv)
117119
raise e
118120
end
119121
rescue Redis::ConnectionError => e
122+
close_connection(redis)
120123
try_random_connection = true
121124
end
122125
end
@@ -132,7 +135,7 @@ def get_random_connection
132135
node = @nodes[node_id]
133136
conn = Redis.new(node[:host], node[:port])
134137
if conn.ping == "PONG"
135-
close_existing_connection
138+
close_existing_connections
136139
@connections[node_id] = conn
137140
return conn
138141
else
@@ -142,7 +145,8 @@ def get_random_connection
142145
return conn if conn.ping == "PONG"
143146
end
144147
rescue => e
145-
# Just try with the next node.
148+
# Try with the next node
149+
close_connection(conn) unless conn.nil?
146150
end
147151
end
148152
raise "Error: failed to get random connection (#{e})"
@@ -152,25 +156,35 @@ def get_connection_by(slot)
152156
node_id = @slots[slot]
153157
return get_random_connection if node_id.nil?
154158

155-
if ! @connections[node_id]
156-
close_existing_connection
159+
unless @connections[node_id]
160+
close_existing_connections
157161
node = @nodes[node_id]
158-
@connections[node_id] = Redis.new(node[:host], node[:port])
162+
begin
163+
@connections[node_id] = Redis.new(node[:host], node[:port])
164+
rescue
165+
return get_random_connection
166+
end
159167
end
160168

161169
@connections[node_id]
162170
end
163171

164-
def close_existing_connection
172+
def close_connection(conn)
173+
raise TypeError unless conn.instance_of?(Redis)
174+
@connections.delete_if { |i, c| c.host == conn.host && c.port == conn.port }
175+
conn.close
176+
end
177+
178+
def close_existing_connections
165179
while @connections.length > @max_cached_connections
166180
id, conn = @connections.shift
167-
conn.close
181+
close_connection(conn)
168182
end
169183
end
170184

171185
def close_all_connections
172186
@connections.each do |id, conn|
173-
conn.close
187+
close_connection(conn)
174188
end
175189
@connections.clear
176190
end

‎test/mrb_rediscluster.rb‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,7 @@
3232
assert_equal rc.hash_slot('foo{{bar}}zap'), rc.hash_slot('{bar')
3333
assert_equal rc.hash_slot('foo{bar}{zap}'), rc.hash_slot('bar')
3434
end
35+
36+
assert('RedisCluster#close_connection') do
37+
assert_raise(TypeError) { RedisCluster.new(HOSTS).close_connection("test") }
38+
end

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /