Permalink
Browse files

Remove dead variables, handle possible race condition with servers_in…

…_use
  • Loading branch information...
1 parent 0f3ad9e commit 1b1b2b7db078d08d919afef0e797d782bd1af051 @mperham mperham committed Dec 5, 2012
Showing with 67 additions and 61 deletions.
  1. +59 −56 lib/dalli/client.rb
  2. +1 −1 lib/dalli/server.rb
  3. +7 −4 test/test_dalli.rb
View
@@ -61,77 +61,80 @@ def get_multi(*keys)
options = nil
options = keys.pop if keys.last.is_a?(Hash) || keys.last.nil?
ring.lock do
- servers = self.servers_in_use = Set.new
-
- keys.flatten.each do |key|
- begin
- perform(:getkq, key)
- rescue DalliError, NetworkError => e
- Dalli.logger.debug { e.inspect }
- Dalli.logger.debug { "unable to get key #{key}" }
- end
- end
-
- values = {}
- return values if servers.empty?
-
- servers.each do |server|
- next unless server.alive?
- begin
- server.multi_response_start
- rescue DalliError, NetworkError => e
- Dalli.logger.debug { e.inspect }
- Dalli.logger.debug { "results from this server will be missing" }
- servers.delete(server)
+ begin
+ servers = self.servers_in_use = Set.new
+
+ keys.flatten.each do |key|
+ begin
+ perform(:getkq, key)
+ rescue DalliError, NetworkError => e
+ Dalli.logger.debug { e.inspect }
+ Dalli.logger.debug { "unable to get key #{key}" }
+ end
end
- end
- start = Time.now
- loop do
- # remove any dead servers
- servers.delete_if{ |s| s.sock.nil? }
- break if servers.empty?
-
- # calculate remaining timeout
- elapsed = Time.now - start
- timeout = servers.first.options[:socket_timeout]
- if elapsed > timeout
- readable = nil
- else
- sockets = servers.map(&:sock)
- readable, _ = IO.select(sockets, nil, nil, timeout - elapsed)
+ values = {}
+ return values if servers.empty?
+
+ servers.each do |server|
+ next unless server.alive?
+ begin
+ server.multi_response_start
+ rescue DalliError, NetworkError => e
+ Dalli.logger.debug { e.inspect }
+ Dalli.logger.debug { "results from this server will be missing" }
+ servers.delete(server)
+ end
end
- if readable.nil?
- # no response within timeout; abort pending connections
- servers.each do |server|
- server.multi_response_abort
+ start = Time.now
+ loop do
+ # remove any dead servers
+ servers.delete_if { |s| s.sock.nil? }
+ break if servers.empty?
+
+ # calculate remaining timeout
+ elapsed = Time.now - start
+ timeout = servers.first.options[:socket_timeout]
+ if elapsed > timeout
+ readable = nil
+ else
+ sockets = servers.map(&:sock)
+ readable, _ = IO.select(sockets, nil, nil, timeout - elapsed)
end
- break
- else
- readable.each do |sock|
- server = sock.server
+ if readable.nil?
+ # no response within timeout; abort pending connections
+ servers.each do |server|
+ puts "Abort!"
+ server.multi_response_abort
+ end
+ break
- begin
- server.multi_response_nonblock.each do |key, value|
- values[key_without_namespace(key)] = value
- end
+ else
+ readable.each do |sock|
+ server = sock.server
+
+ begin
+ server.multi_response_nonblock.each do |key, value|
+ values[key_without_namespace(key)] = value
+ end
- if server.multi_response_completed?
+ if server.multi_response_completed?
+ servers.delete(server)
+ end
+ rescue NetworkError
servers.delete(server)
end
- rescue NetworkError => e
- servers.delete(server)
end
end
end
- end
- values
+ values
+ ensure
+ self.servers_in_use = nil
+ end
end
- ensure
- self.servers_in_use = nil
end
def fetch(key, ttl=nil, options=nil)
View
@@ -155,7 +155,7 @@ def multi_response_nonblock
begin
values[key] = deserialize(value, flags)
- rescue DalliError => e
+ rescue DalliError
end
else
View
@@ -184,18 +184,18 @@
resp = dc.get_multi(%w(a b c d e f))
assert_equal({ 'a' => 'foo', 'b' => 123, 'c' => %w(a b c) }, resp)
- # Perform a huge multi-get with 10,000 elements.
+ # Perform a big multi-get with 1000 elements.
arr = []
dc.multi do
- 10_000.times do |idx|
+ 1000.times do |idx|
dc.set idx, idx
arr << idx
end
end
result = dc.get_multi(arr)
- assert_equal(10_000, result.size)
- assert_equal(1000, result['1000'])
+ assert_equal(1000, result.size)
+ assert_equal(50, result['50'])
end
end
@@ -410,6 +410,9 @@
assert_equal 0, inc % 5
cache.decr('cat', 5)
assert_equal 11, cache.get('b')
+
+ assert_equal %w(a b), cache.get_multi('a', 'b', 'c').keys.sort
+
end
end
end

0 comments on commit 1b1b2b7

Please sign in to comment.