Class: MemCache::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/memcache.rb

Overview

A Multiton datatype to represent a potential memcached server connection.

Constant Summary collapse

ConnectTimeout =

Default timeout for connections to memcached servers.

0.25

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port = 11211, weight = DefaultServerWeight) ⇒ Server

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



1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
# File 'lib/memcache.rb', line 1136

def initialize( host, port=11211, weight=DefaultServerWeight )
	if host.nil? || host.empty?
		raise ArgumentError, "Illegal host %p" % host
	elsif port.nil? || port.to_i.zero?
		raise ArgumentError, "Illegal port %p" % port
	end

	@host 	= host
	@port 	= port
	@weight = weight

	@sock 	= nil
	@retry 	= nil
	@status	= "not yet connected"
end

Instance Attribute Details

#hostObject (readonly)

The host the memcached server is running on



1158
1159
1160
# File 'lib/memcache.rb', line 1158

def host
  @host
end

#portObject (readonly)

The port the memcached is listening on



1161
1162
1163
# File 'lib/memcache.rb', line 1161

def port
  @port
end

#retryObject (readonly)

The Time of next connection retry if the object is dead.



1167
1168
1169
# File 'lib/memcache.rb', line 1167

def retry
  @retry
end

#statusObject (readonly)

A text status string describing the state of the server.



1170
1171
1172
# File 'lib/memcache.rb', line 1170

def status
  @status
end

#weightObject (readonly)

The weight given to the server



1164
1165
1166
# File 'lib/memcache.rb', line 1164

def weight
  @weight
end

Instance Method Details

#alive?Boolean

Test the server for aliveness, returning true if the object was able to connect. This will cause the socket connection to be opened if it isn’t already.

Returns:

  • (Boolean)


1187
1188
1189
# File 'lib/memcache.rb', line 1187

def alive?
	return !self.socket.nil?
end

#inspectObject

Return a string representation of the server object.



1174
1175
1176
1177
1178
1179
1180
1181
# File 'lib/memcache.rb', line 1174

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

#mark_dead(reason = "Unknown error") ⇒ Object

Mark the server as dead for 30 seconds and close its socket. The specified reason will be used to construct an appropriate status message.



1223
1224
1225
1226
1227
1228
1229
# File 'lib/memcache.rb', line 1223

def mark_dead( reason="Unknown error" )
	@sock.close if @sock && !@sock.closed?
	@sock = nil
	@retry = Time::now + ( 30 + rand(10) )
	@status = "DEAD: %s: Will retry at %s" %
		[ reason, @retry ]
end

#socketObject

Try to connect to the memcached targeted by this object. Returns the connected socket object on success; sets @dead and returns nil on any failure.



1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
# File 'lib/memcache.rb', line 1195

def socket

	# Connect if not already connected
	unless @sock || (!@sock.nil? && @sock.closed?)

		# If the host was dead, don't retry for a while
		if @retry
			return nil if @retry > Time::now
		end

		# Attempt to connect, 
		begin
			@sock = timeout( ConnectTimeout ) {
				TCPSocket::new( @host, @port )
			}
			@status = "connected"
		rescue SystemCallError, IOError, TimeoutError => err
			self.mark_dead( err.message )
		end
	end

	return @sock
end