Class MemCache::Server
In: lib/memcache.rb
Parent: Object

This class represents a memcached server instance.

Methods

alive?   close   connect_to   inspect   mark_dead   new   socket  

Constants

RETRY_DELAY = 30.0   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.
logger  [R] 
port  [R]  The port the memcached server is listening on.
retry  [R]  The time of next retry if the connection is dead.
status  [R]  A text status string describing the state of the server.
weight  [R]  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.

[Source]

# File lib/memcache.rb, line 960
    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
    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.

[Source]

# File lib/memcache.rb, line 988
    def alive?
      !!socket
    end

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

[Source]

# File lib/memcache.rb, line 1050
    def close
      @sock.close if @sock && !@sock.closed?
      @sock   = nil
      @retry  = nil
      @status = "NOT CONNECTED"
    end

[Source]

# File lib/memcache.rb, line 1018
    def connect_to(host, port, timeout=nil)
      addr = Socket.getaddrinfo(host, nil)
      sock = Socket.new(Socket.const_get(addr[0][0]), Socket::SOCK_STREAM, 0)

      if timeout
        secs = Integer(timeout)
        usecs = Integer((timeout - secs) * 1_000_000)
        optval = [secs, usecs].pack("l_2")
        sock.setsockopt Socket::SOL_SOCKET, Socket::SO_RCVTIMEO, optval
        sock.setsockopt Socket::SOL_SOCKET, Socket::SO_SNDTIMEO, optval

        # Socket timeouts don't work for more complex IO operations
        # like gets which lay on top of read.  We need to fall back to
        # the standard Timeout mechanism.
        sock.instance_eval "alias :blocking_gets :gets\ndef gets\nMemCacheTimer.timeout(\#{timeout}) do\nself.blocking_gets\nend\nend\n"
      end
      sock.connect(Socket.pack_sockaddr_in(port, addr[0][3]))
      sock
    end

Return a string representation of the server object.

[Source]

# File lib/memcache.rb, line 978
    def inspect
      "<MemCache::Server: %s:%d [%d] (%s)>" % [@host, @port, @weight, @status]
    end

Mark the server as dead and close its socket.

[Source]

# File lib/memcache.rb, line 1060
    def mark_dead(error)
      @sock.close if @sock && !@sock.closed?
      @sock   = nil
      @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.

[Source]

# File lib/memcache.rb, line 996
    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 => err
        logger.warn { "Unable to open socket: #{err.class.name}, #{err.message}" } if logger
        mark_dead err
      end

      return @sock
    end

[Validate]