Class | MemCache::Server |
In: |
vendor/rails/activesupport/lib/active_support/vendor/memcache-client-1.7.4/memcache.rb
|
Parent: | Object |
This class represents a memcached server instance.
RETRY_DELAY | = | 30.0 | The amount of time to wait before attempting to re-establish a connection with a server that is marked dead. |
host | [R] | The host the memcached server is running on. |
logger | [R] | |
port | [R] | The port the memcached server is listening on. |
retry | [R] | The time of next retry if the connection 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 by the given weight.
# File vendor/rails/activesupport/lib/active_support/vendor/memcache-client-1.7.4/memcache.rb, line 946 946: def initialize(memcache, host, port = DEFAULT_PORT, weight = DEFAULT_WEIGHT) 947: raise ArgumentError, "No host specified" if host.nil? or host.empty? 948: raise ArgumentError, "No port specified" if port.nil? or port.to_i.zero? 949: 950: @host = host 951: @port = port.to_i 952: @weight = weight.to_i 953: 954: @sock = nil 955: @retry = nil 956: @status = 'NOT CONNECTED' 957: @timeout = memcache.timeout 958: @logger = memcache.logger 959: end
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.
# File vendor/rails/activesupport/lib/active_support/vendor/memcache-client-1.7.4/memcache.rb, line 974 974: def alive? 975: !!socket 976: end
Close the connection to the memcached server targeted by this object. The server is not considered dead.
# File vendor/rails/activesupport/lib/active_support/vendor/memcache-client-1.7.4/memcache.rb, line 1014 1014: def close 1015: @sock.close if @sock && !@sock.closed? 1016: @sock = nil 1017: @retry = nil 1018: @status = "NOT CONNECTED" 1019: end
# File vendor/rails/activesupport/lib/active_support/vendor/memcache-client-1.7.4/memcache.rb, line 1004 1004: def connect_to(host, port, timeout=nil) 1005: io = MemCache::BufferedIO.new(TCPSocket.new(host, port)) 1006: io.read_timeout = timeout 1007: io 1008: end
Return a string representation of the server object.
# File vendor/rails/activesupport/lib/active_support/vendor/memcache-client-1.7.4/memcache.rb, line 964 964: def inspect 965: "<MemCache::Server: %s:%d [%d] (%s)>" % [@host, @port, @weight, @status] 966: end
Mark the server as dead and close its socket.
# File vendor/rails/activesupport/lib/active_support/vendor/memcache-client-1.7.4/memcache.rb, line 1024 1024: def mark_dead(error) 1025: @sock.close if @sock && !@sock.closed? 1026: @sock = nil 1027: @retry = Time.now + RETRY_DELAY 1028: 1029: reason = "#{error.class.name}: #{error.message}" 1030: @status = sprintf "%s:%s DEAD (%s), will retry at %s", @host, @port, reason, @retry 1031: @logger.info { @status } if @logger 1032: end
Try to connect to the memcached server targeted by this object. Returns the connected socket object on success or nil on failure.
# File vendor/rails/activesupport/lib/active_support/vendor/memcache-client-1.7.4/memcache.rb, line 982 982: def socket 983: return @sock if @sock and not @sock.closed? 984: 985: @sock = nil 986: 987: # If the host was dead, don't retry for a while. 988: return if @retry and @retry > Time.now 989: 990: # Attempt to connect if not already connected. 991: begin 992: @sock = connect_to(@host, @port, @timeout) 993: @sock.setsockopt Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1 994: @retry = nil 995: @status = 'CONNECTED' 996: rescue SocketError, SystemCallError, IOError => err 997: logger.warn { "Unable to open socket: #{err.class.name}, #{err.message}" } if logger 998: mark_dead err 999: end 1000: 1001: return @sock 1002: end