In Files

Parent

Class Index [+]

Quicksearch

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 945
945:     def initialize(memcache, host, port = DEFAULT_PORT, weight = DEFAULT_WEIGHT)
946:       raise ArgumentError, "No host specified" if host.nil? or host.empty?
947:       raise ArgumentError, "No port specified" if port.nil? or port.to_i.zero?
948: 
949:       @host   = host
950:       @port   = port.to_i
951:       @weight = weight.to_i
952: 
953:       @sock   = nil
954:       @retry  = nil
955:       @status = 'NOT CONNECTED'
956:       @timeout = memcache.timeout
957:       @logger = memcache.logger
958:     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 973
973:     def alive?
974:       !!socket
975:     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 1013
1013:     def close
1014:       @sock.close if @sock && !@sock.closed?
1015:       @sock   = nil
1016:       @retry  = nil
1017:       @status = "NOT CONNECTED"
1018:     end
connect_to(host, port, timeout=nil) click to toggle source
      # File lib/memcache.rb, line 1003
1003:     def connect_to(host, port, timeout=nil)
1004:       io = MemCache::BufferedIO.new(TCPSocket.new(host, port))
1005:       io.read_timeout = timeout
1006:       io
1007:     end
inspect() click to toggle source

Return a string representation of the server object.

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

Mark the server as dead and close its socket.

      # File lib/memcache.rb, line 1023
1023:     def mark_dead(error)
1024:       @sock.close if @sock && !@sock.closed?
1025:       @sock   = nil
1026:       @retry  = Time.now + RETRY_DELAY
1027: 
1028:       reason = "#{error.class.name}: #{error.message}"
1029:       @status = sprintf "%s:%s DEAD (%s), will retry at %s", @host, @port, reason, @retry
1030:       @logger.info { @status } if @logger
1031:     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 981
 981:     def socket
 982:       return @sock if @sock and not @sock.closed?
 983: 
 984:       @sock = nil
 985: 
 986:       # If the host was dead, don't retry for a while.
 987:       return if @retry and @retry > Time.now
 988: 
 989:       # Attempt to connect if not already connected.
 990:       begin
 991:         @sock = connect_to(@host, @port, @timeout)
 992:         @sock.setsockopt Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1
 993:         @retry  = nil
 994:         @status = 'CONNECTED'
 995:       rescue SocketError, SystemCallError, IOError => err
 996:         logger.warn { "Unable to open socket: #{err.class.name}, #{err.message}" } if logger
 997:         mark_dead err
 998:       end
 999: 
1000:       return @sock
1001:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.