Class: MemCache::Server
- Inherits:
-
Object
- Object
- MemCache::Server
- Defined in:
- lib/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
-
#host ⇒ Object
readonly
The host the memcached server is running on.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#port ⇒ Object
readonly
The port the memcached server is listening on.
-
#retry ⇒ Object
readonly
The time of next retry if the connection is dead.
-
#status ⇒ Object
readonly
A text status string describing the state of the server.
-
#weight ⇒ Object
readonly
The weight given to the server.
Instance Method Summary collapse
-
#alive? ⇒ Boolean
Check whether the server connection is alive.
-
#close ⇒ Object
Close the connection to the memcached server targeted by this object.
- #connect_to(host, port, timeout = nil) ⇒ Object
-
#initialize(memcache, host, port = DEFAULT_PORT, weight = DEFAULT_WEIGHT) ⇒ Server
constructor
Create a new MemCache::Server object for the memcached instance listening on the given host and port, weighted by the given weight.
-
#inspect ⇒ Object
Return a string representation of the server object.
-
#mark_dead(error) ⇒ Object
Mark the server as dead and close its socket.
-
#socket ⇒ Object
Try to connect to the memcached server targeted by this object.
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.
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 |
# File 'lib/memcache.rb', line 1003 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
#host ⇒ Object (readonly)
The host the memcached server is running on.
975 976 977 |
# File 'lib/memcache.rb', line 975 def host @host end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
997 998 999 |
# File 'lib/memcache.rb', line 997 def logger @logger end |
#port ⇒ Object (readonly)
The port the memcached server is listening on.
980 981 982 |
# File 'lib/memcache.rb', line 980 def port @port end |
#retry ⇒ Object (readonly)
The time of next retry if the connection is dead.
990 991 992 |
# File 'lib/memcache.rb', line 990 def retry @retry end |
#status ⇒ Object (readonly)
A text status string describing the state of the server.
995 996 997 |
# File 'lib/memcache.rb', line 995 def status @status end |
#weight ⇒ Object (readonly)
The weight given to the server.
985 986 987 |
# File 'lib/memcache.rb', line 985 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.
1031 1032 1033 |
# File 'lib/memcache.rb', line 1031 def alive? !!socket end |
#close ⇒ Object
Close the connection to the memcached server targeted by this object. The server is not considered dead.
1095 1096 1097 1098 1099 1100 |
# File 'lib/memcache.rb', line 1095 def close @sock.close if @sock && !@sock.closed? @sock = nil @retry = nil @status = "NOT CONNECTED" end |
#connect_to(host, port, timeout = nil) ⇒ Object
1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 |
# File 'lib/memcache.rb', line 1061 def connect_to(host, port, timeout=nil) sock = nil if timeout MemCacheTimer.timeout(timeout) do sock = TCPSocket.new(host, port) end else sock = TCPSocket.new(host, port) end io = MemCache::BufferedIO.new(sock) io.read_timeout = timeout # Getting reports from several customers, including 37signals, # that the non-blocking timeouts in 1.7.5 don't seem to be reliable. # It can't hurt to set the underlying socket timeout also, if possible. if timeout secs = Integer(timeout) usecs = Integer((timeout - secs) * 1_000_000) optval = [secs, usecs].pack("l_2") begin io.setsockopt Socket::SOL_SOCKET, Socket::SO_RCVTIMEO, optval io.setsockopt Socket::SOL_SOCKET, Socket::SO_SNDTIMEO, optval rescue Exception => ex # Solaris, for one, does not like/support socket timeouts. @logger.info "[memcache-client] Unable to use raw socket timeouts: #{ex.class.name}: #{ex.message}" if @logger end end io end |
#inspect ⇒ Object
Return a string representation of the server object.
1021 1022 1023 |
# File 'lib/memcache.rb', line 1021 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.
1105 1106 1107 1108 1109 1110 1111 1112 1113 |
# File 'lib/memcache.rb', line 1105 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 |
#socket ⇒ Object
Try to connect to the memcached server targeted by this object. Returns the connected socket object on success or nil on failure.
1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 |
# File 'lib/memcache.rb', line 1039 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, Timeout::Error => err logger.warn { "Unable to open socket: #{err.class.name}, #{err.message}" } if logger mark_dead err end return @sock end |