Class | MemCache::Server |
In: |
lib/memcache.rb
(CVS)
|
Parent: | Object |
A Multiton datatype to represent a potential memcached server connection.
ConnectTimeout | = | 0.25 | Default timeout for connections to memcached servers. |
connect_timeout | [RW] | The number of (floating-point) seconds before a connection fails. |
host | [R] | The host the memcached server is running on |
port | [R] | The port the memcached is listening on |
retry | [R] | The Time of next connection retry if the object is dead. |
status | [R] | A text status string describing the state of the server. |
weight | [R] | The weight given to the server |
Create a new MemCache::Server object for the memcached instance listening on the given host and port, weighted with the given weight.
# File lib/memcache.rb, line 1184 def initialize( host, port=11211, weight=DefaultServerWeight, timeout=ConnectTimeout ) 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 @connect_timeout = timeout @sock = nil @retry = nil @status = "not yet connected" end
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.
# File lib/memcache.rb, line 1240 def alive? return !self.socket.nil? end
Return a string representation of the server object.
# File lib/memcache.rb, line 1227 def inspect return "<MemCache::Server: %s:%d [%d] (%s)>" % [ @host, @port, @weight, @status, ] end
Mark the server as dead for 30 seconds and close its socket. The specified reason will be used to construct an appropriate status message.
# File lib/memcache.rb, line 1278 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
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.
# File lib/memcache.rb, line 1248 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( @connect_timeout ) { TCPSocket::new( @host, @port ) } @status = "connected" rescue SystemCallError, IOError, TimeoutError => err # $deferr.puts "Error while connecting to %s:%d: %s" % # [ @host, @port, err.message ] self.mark_dead( err.message ) end end return @sock end