In Files

Parent

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

host[R]

The host the memcached server is running on.

port[R]

The port the memcached server is listening on.

weight[R]

The weight given to the server.

retry[R]

The time of next retry if the connection is dead.

status[R]

A text status string describing the state of the server.

logger[R]

Public Class Methods

new(memcache, host, port = DEFAULT_PORT, weight = DEFAULT_WEIGHT) click to toggle source

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 1004
1004:     def initialize(memcache, host, port = DEFAULT_PORT, weight = DEFAULT_WEIGHT)
1005:       raise ArgumentError, "No host specified" if host.nil? or host.empty?
1006:       raise ArgumentError, "No port specified" if port.nil? or port.to_i.zero?
1007: 
1008:       @host   = host
1009:       @port   = port.to_i
1010:       @weight = weight.to_i
1011: 
1012:       @sock   = nil
1013:       @retry  = nil
1014:       @status = 'NOT CONNECTED'
1015:       @timeout = memcache.timeout
1016:       @logger = memcache.logger
1017: 
1018:       if defined?(EM) and EM.reactor_running? and defined?(MemCache::EventedServer)
1019:         self.extend(MemCache::EventedServer)
1020:       end
1021:     end

Public Instance Methods

alive?() click to toggle source

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 1036
1036:     def alive?
1037:       !!socket
1038:     end
close() click to toggle source

Close the connection to the memcached server targeted by this object. The server is not considered dead.

      # File lib/memcache.rb, line 1100
1100:     def close
1101:       @sock.close if @sock && !@sock.closed?
1102:       @sock   = nil
1103:       @retry  = nil
1104:       @status = "NOT CONNECTED"
1105:     end
connect_to(host, port, timeout=nil) click to toggle source
      # File lib/memcache.rb, line 1066
1066:     def connect_to(host, port, timeout=nil)
1067:       sock = nil
1068:       if timeout
1069:         MemCacheTimer.timeout(timeout) do
1070:           sock = TCPSocket.new(host, port)
1071:         end
1072:       else
1073:         sock = TCPSocket.new(host, port)
1074:       end
1075: 
1076:       io = MemCache::BufferedIO.new(sock)
1077:       io.read_timeout = timeout
1078:       # Getting reports from several customers, including 37signals,
1079:       # that the non-blocking timeouts in 1.7.5 don't seem to be reliable.
1080:       # It can't hurt to set the underlying socket timeout also, if possible.
1081:       if timeout
1082:         secs = Integer(timeout)
1083:         usecs = Integer((timeout - secs) * 1_000_000)
1084:         optval = [secs, usecs].pack("l_2")
1085:         begin
1086:           io.setsockopt Socket::SOL_SOCKET, Socket::SO_RCVTIMEO, optval
1087:           io.setsockopt Socket::SOL_SOCKET, Socket::SO_SNDTIMEO, optval
1088:         rescue Exception => ex
1089:           # Solaris, for one, does not like/support socket timeouts.
1090:           @logger.info "[memcache-client] Unable to use raw socket timeouts: #{ex.class.name}: #{ex.message}" if @logger
1091:         end
1092:       end
1093:       io
1094:     end
inspect() click to toggle source

Return a string representation of the server object.

      # File lib/memcache.rb, line 1026
1026:     def inspect
1027:       "<MemCache::Server: %s:%d [%d] (%s)>" % [@host, @port, @weight, @status]
1028:     end
mark_dead(error) click to toggle source

Mark the server as dead and close its socket.

      # File lib/memcache.rb, line 1110
1110:     def mark_dead(error)
1111:       close
1112:       @retry  = Time.now + RETRY_DELAY
1113: 
1114:       reason = "#{error.class.name}: #{error.message}"
1115:       @status = sprintf "%s:%s DEAD (%s), will retry at %s", @host, @port, reason, @retry
1116:       @logger.info { @status } if @logger
1117:     end
socket() click to toggle source

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 1044
1044:     def socket
1045:       return @sock if @sock and not @sock.closed?
1046: 
1047:       @sock = nil
1048: 
1049:       # If the host was dead, don't retry for a while.
1050:       return if @retry and @retry > Time.now
1051: 
1052:       # Attempt to connect if not already connected.
1053:       begin
1054:         @sock = connect_to(@host, @port, @timeout)
1055:         @sock.setsockopt Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1
1056:         @retry  = nil
1057:         @status = 'CONNECTED'
1058:       rescue SocketError, SystemCallError, IOError, Timeout::Error => err
1059:         logger.warn { "Unable to open socket: #{err.class.name}, #{err.message}" } if logger
1060:         mark_dead err
1061:       end
1062: 
1063:       return @sock
1064:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.