Skip to content

Commit

Permalink
Rejigger evented support to extend server instances rather than
Browse files Browse the repository at this point in the history
monkeypatch classes.  This makes it much easier and predictable to test.
  • Loading branch information
mperham committed Apr 3, 2010
1 parent d9cf45d commit 3ed20bf
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 16 deletions.
2 changes: 1 addition & 1 deletion History.rdoc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
= HEAD
= 1.8.2 (2010-04-03)

* Fix concurrency issues with eventmachine support.

Expand Down
9 changes: 7 additions & 2 deletions lib/memcache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class MemCache
# Please note this feature only works in memcached 1.2.5 and later. Earlier
# versions will reply with "ERROR".
attr_reader :no_reply

##
# Accepts a list of +servers+ and a list of +opts+. +servers+ may be
# omitted. See +servers=+ for acceptable server list arguments.
Expand Down Expand Up @@ -154,10 +154,11 @@ def initialize(*args)
raise ArgumentError, "wrong number of arguments (#{args.length} for 2)"
end

@evented = defined?(EM) && EM.reactor_running?
opts = DEFAULT_OPTIONS.merge opts
@namespace = opts[:namespace]
@readonly = opts[:readonly]
@multithread = opts[:multithread]
@multithread = opts[:multithread] && !@evented
@autofix_keys = opts[:autofix_keys]
@timeout = opts[:timeout]
@failover = opts[:failover]
Expand All @@ -170,6 +171,7 @@ def initialize(*args)
logger.info { "memcache-client #{VERSION} #{Array(servers).inspect}" } if logger

Thread.current[:memcache_client] = self.object_id if !@multithread


self.servers = servers
end
Expand Down Expand Up @@ -946,6 +948,7 @@ def entry_count_for(server, total_servers, total_weight)

def check_multithread_status!
return if @multithread
return if @evented

if Thread.current[:memcache_client] != self.object_id
raise MemCacheError, <<-EOM
Expand Down Expand Up @@ -1011,6 +1014,8 @@ def initialize(memcache, host, port = DEFAULT_PORT, weight = DEFAULT_WEIGHT)
@status = 'NOT CONNECTED'
@timeout = memcache.timeout
@logger = memcache.logger

self.extend(MemCache::EventedServer) if defined?(EM) and EM.reactor_running?
end

##
Expand Down
19 changes: 6 additions & 13 deletions lib/memcache/event_machine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,15 @@ class MemCache

# Since we are working in a single Thread, multiple Fiber environment,
# disable the multithread Mutex as it will not work.
DEFAULT_OPTIONS[:multithread] = false
# DEFAULT_OPTIONS[:multithread] = false

def check_multithread_status!
end

class Server
alias :blocking_socket :socket
module EventedServer

def fiber_key
@fiber_key ||= "memcached-#{@host}-#{@port}"
end

def socket
# Support plain old TCP socket connections if the user
# has not setup EM. Much easier to deal with in irb,
# script/console, etc.
return blocking_socket if !EM.reactor_running?

sock = Thread.current[fiber_key]
return sock if sock and not sock.closed?

Expand Down Expand Up @@ -58,8 +49,10 @@ def socket

def close
sock = Thread.current[fiber_key]
sock.close if sock && !sock.closed?
Thread.current[fiber_key] = nil
if sock
sock.close if !sock.closed?
Thread.current[fiber_key] = nil
end
@retry = nil
@status = "NOT CONNECTED"
end
Expand Down
1 change: 1 addition & 0 deletions test/test_mem_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1201,6 +1201,7 @@ def test_crazy_multithreaded_access
# Use a null logger to verify logging doesn't blow up at runtime
cache = MemCache.new(['localhost:11211', '127.0.0.1:11211'], :logger => Logger.new('/dev/null'))
cache.flush_all
assert_equal true, cache.multithread
workers = []

cache.set('f', 'zzz')
Expand Down

0 comments on commit 3ed20bf

Please sign in to comment.