Skip to content

Commit

Permalink
Unprotected with socket managementfor now.
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien Guimont committed Feb 20, 2010
1 parent 4794d95 commit c81b888
Showing 1 changed file with 45 additions and 44 deletions.
89 changes: 45 additions & 44 deletions lib/memcachedb.rb
Expand Up @@ -630,6 +630,51 @@ def stats
def []=(key, value)
set key, value
end


##
# Gets or creates a socket connected to the given server, and yields it
# to the block, wrapped in a mutex synchronization if @multithread is true.
#
# If a socket error (SocketError, SystemCallError, IOError) or protocol error
# (MemCacheDbError) is raised by the block, closes the socket, attempts to
# connect again, and retries the block (once). If an error is again raised,
# reraises it as MemCacheDbError.
#
# If unable to connect to the server (or if in the reconnect wait period),
# raises MemCacheDbError. Note that the socket connect code marks a server
# dead for a timeout period, so retrying does not apply to connection attempt
# failures (but does still apply to unexpectedly lost connections etc.).

def with_socket_management(server, &block)
check_multithread_status!
@mutex.lock if @multithread
retried = false

begin
socket = server.socket
# Raise an IndexError to show this server is out of whack. If were inside
# a with_server block, we'll catch it and attempt to restart the operation.

raise IndexError, "No connection to server (#{server.status})" if socket.nil?

block.call(socket)

rescue SocketError, Errno::EAGAIN, Timeout::Error => err

logger.warn { "Socket failure: #{err.message}" } if logger
server.mark_dead(err)
handle_error(server, err)

rescue MemCacheDbError, SystemCallError, IOError => err
logger.warn { "Generic failure: #{err.class.name}: #{err.message}" } if logger
handle_error(server, err) if retried || socket.nil?
retried = true
retry
end
ensure
@mutex.unlock if @multithread
end

protected unless $TESTING

Expand Down Expand Up @@ -804,50 +849,6 @@ def cache_incr(server, cache_key, amount)
end
end

##
# Gets or creates a socket connected to the given server, and yields it
# to the block, wrapped in a mutex synchronization if @multithread is true.
#
# If a socket error (SocketError, SystemCallError, IOError) or protocol error
# (MemCacheDbError) is raised by the block, closes the socket, attempts to
# connect again, and retries the block (once). If an error is again raised,
# reraises it as MemCacheDbError.
#
# If unable to connect to the server (or if in the reconnect wait period),
# raises MemCacheDbError. Note that the socket connect code marks a server
# dead for a timeout period, so retrying does not apply to connection attempt
# failures (but does still apply to unexpectedly lost connections etc.).

def with_socket_management(server, &block)
check_multithread_status!
@mutex.lock if @multithread
retried = false

begin
socket = server.socket
# Raise an IndexError to show this server is out of whack. If were inside
# a with_server block, we'll catch it and attempt to restart the operation.

raise IndexError, "No connection to server (#{server.status})" if socket.nil?

block.call(socket)

rescue SocketError, Errno::EAGAIN, Timeout::Error => err

logger.warn { "Socket failure: #{err.message}" } if logger
server.mark_dead(err)
handle_error(server, err)

rescue MemCacheDbError, SystemCallError, IOError => err
logger.warn { "Generic failure: #{err.class.name}: #{err.message}" } if logger
handle_error(server, err) if retried || socket.nil?
retried = true
retry
end
ensure
@mutex.unlock if @multithread
end

def with_server(key, read = false)
retried = false
begin
Expand Down

0 comments on commit c81b888

Please sign in to comment.