Class: MemCache::Server

Inherits:
Object show all
Defined in:
lib/active_support/vendor/memcache-client-1.5.1/memcache.rb

Overview

This class represents a memcached server instance.

Constant Summary collapse

CONNECT_TIMEOUT =

The amount of time to wait to establish a connection with a memcached server. If a connection cannot be established within this time limit, the server will be marked as down.

0.25
RETRY_DELAY =

The amount of time to wait before attempting to re-establish a connection with a server that is marked dead.

30.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(memcache, host, port = DEFAULT_PORT, weight = DEFAULT_WEIGHT) ⇒ Server

Create a new MemCache::Server object for the memcached instance listening on the given host and port, weighted by the given weight.

Raises:

  • (ArgumentError)


749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
# File 'lib/active_support/vendor/memcache-client-1.5.1/memcache.rb', line 749

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?

  @memcache = memcache
  @host   = host
  @port   = port.to_i
  @weight = weight.to_i

  @multithread = @memcache.multithread
  @mutex = Mutex.new

  @sock   = nil
  @retry  = nil
  @status = 'NOT CONNECTED'
end

Instance Attribute Details

#hostObject (readonly)

The host the memcached server is running on.



723
724
725
# File 'lib/active_support/vendor/memcache-client-1.5.1/memcache.rb', line 723

def host
  @host
end

#portObject (readonly)

The port the memcached server is listening on.



728
729
730
# File 'lib/active_support/vendor/memcache-client-1.5.1/memcache.rb', line 728

def port
  @port
end

#retryObject (readonly)

The time of next retry if the connection is dead.



738
739
740
# File 'lib/active_support/vendor/memcache-client-1.5.1/memcache.rb', line 738

def retry
  @retry
end

#statusObject (readonly)

A text status string describing the state of the server.



743
744
745
# File 'lib/active_support/vendor/memcache-client-1.5.1/memcache.rb', line 743

def status
  @status
end

#weightObject (readonly)

The weight given to the server.



733
734
735
# File 'lib/active_support/vendor/memcache-client-1.5.1/memcache.rb', line 733

def weight
  @weight
end

Instance Method Details

#alive?Boolean

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.

Returns:

  • (Boolean)


779
780
781
# File 'lib/active_support/vendor/memcache-client-1.5.1/memcache.rb', line 779

def alive?
  !!socket
end

#closeObject

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



819
820
821
822
823
824
825
826
827
# File 'lib/active_support/vendor/memcache-client-1.5.1/memcache.rb', line 819

def close
  @mutex.lock if @multithread
  @sock.close if @sock && !@sock.closed?
  @sock   = nil
  @retry  = nil
  @status = "NOT CONNECTED"
ensure
  @mutex.unlock if @multithread
end

#inspectObject

Return a string representation of the server object.



769
770
771
# File 'lib/active_support/vendor/memcache-client-1.5.1/memcache.rb', line 769

def inspect
  "<MemCache::Server: %s:%d [%d] (%s)>" % [@host, @port, @weight, @status]
end

#socketObject

Try to connect to the memcached server targeted by this object. Returns the connected socket object on success or nil on failure.



787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
# File 'lib/active_support/vendor/memcache-client-1.5.1/memcache.rb', line 787

def socket
  @mutex.lock if @multithread
  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 = timeout CONNECT_TIMEOUT do
      TCPSocket.new @host, @port
    end
    if Socket.constants.include? 'TCP_NODELAY' then
      @sock.setsockopt Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1
    end
    @retry  = nil
    @status = 'CONNECTED'
  rescue SocketError, SystemCallError, IOError, Timeout::Error => err
    mark_dead err.message
  end

  return @sock
ensure
  @mutex.unlock if @multithread
end