Class: MemCache::Server

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

Overview

This class represents a memcached server instance.

Constant Summary collapse

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)


945
946
947
948
949
950
951
952
953
954
955
956
957
958
# File 'lib/active_support/vendor/memcache-client-1.7.4/memcache.rb', line 945

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

Instance Attribute Details

#hostObject (readonly)

The host the memcached server is running on.



917
918
919
# File 'lib/active_support/vendor/memcache-client-1.7.4/memcache.rb', line 917

def host
  @host
end

#loggerObject (readonly)

Returns the value of attribute logger.



939
940
941
# File 'lib/active_support/vendor/memcache-client-1.7.4/memcache.rb', line 939

def logger
  @logger
end

#portObject (readonly)

The port the memcached server is listening on.



922
923
924
# File 'lib/active_support/vendor/memcache-client-1.7.4/memcache.rb', line 922

def port
  @port
end

#retryObject (readonly)

The time of next retry if the connection is dead.



932
933
934
# File 'lib/active_support/vendor/memcache-client-1.7.4/memcache.rb', line 932

def retry
  @retry
end

#statusObject (readonly)

A text status string describing the state of the server.



937
938
939
# File 'lib/active_support/vendor/memcache-client-1.7.4/memcache.rb', line 937

def status
  @status
end

#weightObject (readonly)

The weight given to the server.



927
928
929
# File 'lib/active_support/vendor/memcache-client-1.7.4/memcache.rb', line 927

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)


973
974
975
# File 'lib/active_support/vendor/memcache-client-1.7.4/memcache.rb', line 973

def alive?
  !!socket
end

#closeObject

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



1013
1014
1015
1016
1017
1018
# File 'lib/active_support/vendor/memcache-client-1.7.4/memcache.rb', line 1013

def close
  @sock.close if @sock && !@sock.closed?
  @sock   = nil
  @retry  = nil
  @status = "NOT CONNECTED"
end

#connect_to(host, port, timeout = nil) ⇒ Object



1003
1004
1005
1006
1007
# File 'lib/active_support/vendor/memcache-client-1.7.4/memcache.rb', line 1003

def connect_to(host, port, timeout=nil)
  io = MemCache::BufferedIO.new(TCPSocket.new(host, port))
  io.read_timeout = timeout
  io
end

#inspectObject

Return a string representation of the server object.



963
964
965
# File 'lib/active_support/vendor/memcache-client-1.7.4/memcache.rb', line 963

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

#mark_dead(error) ⇒ Object

Mark the server as dead and close its socket.



1023
1024
1025
1026
1027
1028
1029
1030
1031
# File 'lib/active_support/vendor/memcache-client-1.7.4/memcache.rb', line 1023

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

#socketObject

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



981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
# File 'lib/active_support/vendor/memcache-client-1.7.4/memcache.rb', line 981

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