class MemCache::Server
This class represents a memcached server instance.
Constants
- RETRY_DELAY
The amount of time to wait before attempting to re-establish a connection with a server that is marked dead.
Attributes
The host the memcached server is running on.
The port the memcached server is listening on.
The time of next retry if the connection is dead.
A text status string describing the state of the server.
The weight given to the server.
Public Class Methods
Create a new MemCache::Server object for the memcached instance listening on the given host and port, weighted by the given weight.
# File lib/memcache.rb, line 1003 def initialize(memcache, host, port = DEFAULT_PORT, weight = DEFAULT_WEIGHT) raise ArgumentError, "No host specified" if host.nil? or host.empty? raise ArgumentError, "No port specified" if port.nil? or port.to_i.zero? @host = host @port = port.to_i @weight = weight.to_i @sock = nil @retry = nil @status = 'NOT CONNECTED' @timeout = memcache.timeout @logger = memcache.logger if defined?(EM) and EM.reactor_running? and defined?(MemCache::EventedServer) self.extend(MemCache::EventedServer) end end
Public Instance Methods
Check whether the server connection is alive. This will cause the socket to attempt to connect if it isn't already connected and or if the server was previously marked as down and the retry time has been exceeded.
# File lib/memcache.rb, line 1035 def alive? !!socket end
Close the connection to the memcached server targeted by this object. The server is not considered dead.
# File lib/memcache.rb, line 1099 def close @sock.close if @sock && !@sock.closed? @sock = nil @retry = nil @status = "NOT CONNECTED" end
# File lib/memcache.rb, line 1065 def connect_to(host, port, timeout=nil) sock = nil if timeout MemCacheTimer.timeout(timeout) do sock = TCPSocket.new(host, port) end else sock = TCPSocket.new(host, port) end io = MemCache::BufferedIO.new(sock) io.read_timeout = timeout # Getting reports from several customers, including 37signals, # that the non-blocking timeouts in 1.7.5 don't seem to be reliable. # It can't hurt to set the underlying socket timeout also, if possible. if timeout secs = Integer(timeout) usecs = Integer((timeout - secs) * 1_000_000) optval = [secs, usecs].pack("l_2") begin io.setsockopt Socket::SOL_SOCKET, Socket::SO_RCVTIMEO, optval io.setsockopt Socket::SOL_SOCKET, Socket::SO_SNDTIMEO, optval rescue Exception => ex # Solaris, for one, does not like/support socket timeouts. @logger.info "[memcache-client] Unable to use raw socket timeouts: #{ex.class.name}: #{ex.message}" if @logger end end io end
Return a string representation of the server object.
# File lib/memcache.rb, line 1025 def inspect "<MemCache::Server: %s:%d [%d] (%s)>" % [@host, @port, @weight, @status] end
Mark the server as dead and close its socket.
# File lib/memcache.rb, line 1109 def mark_dead(error) close @retry = Time.now + RETRY_DELAY reason = "#{error.class.name}: #{error.message}" @status = sprintf "%s:%s DEAD (%s), will retry at %s", @host, @port, reason, @retry @logger.info { @status } if @logger end
Try to connect to the memcached server targeted by this object. Returns the connected socket object on success or nil on failure.
# File lib/memcache.rb, line 1043 def socket return @sock if @sock and not @sock.closed? @sock = nil # If the host was dead, don't retry for a while. return if @retry and @retry > Time.now # Attempt to connect if not already connected. begin @sock = connect_to(@host, @port, @timeout) @sock.setsockopt Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1 @retry = nil @status = 'CONNECTED' rescue SocketError, SystemCallError, IOError, Timeout::Error => err logger.warn { "Unable to open socket: #{err.class.name}, #{err.message}" } if logger mark_dead err end return @sock end