Class: MemCache::Server
- Defined in:
- lib/active_support/vendor/memcache-client-1.6.5/memcache.rb
Overview
This class represents a memcached server instance.
Constant Summary collapse
- CONNECT_TIMEOUT =
The amount of time to wait to establish a connection with a memcached server. If a connection cannot be established within this time limit, the server will be marked as down.
0.25
- 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.
-
#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.
759 760 761 762 763 764 765 766 767 768 769 770 771 772 |
# File 'lib/active_support/vendor/memcache-client-1.6.5/memcache.rb', line 759 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.
731 732 733 |
# File 'lib/active_support/vendor/memcache-client-1.6.5/memcache.rb', line 731 def host @host end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
753 754 755 |
# File 'lib/active_support/vendor/memcache-client-1.6.5/memcache.rb', line 753 def logger @logger end |
#port ⇒ Object (readonly)
The port the memcached server is listening on.
736 737 738 |
# File 'lib/active_support/vendor/memcache-client-1.6.5/memcache.rb', line 736 def port @port end |
#retry ⇒ Object (readonly)
The time of next retry if the connection is dead.
746 747 748 |
# File 'lib/active_support/vendor/memcache-client-1.6.5/memcache.rb', line 746 def retry @retry end |
#status ⇒ Object (readonly)
A text status string describing the state of the server.
751 752 753 |
# File 'lib/active_support/vendor/memcache-client-1.6.5/memcache.rb', line 751 def status @status end |
#weight ⇒ Object (readonly)
The weight given to the server.
741 742 743 |
# File 'lib/active_support/vendor/memcache-client-1.6.5/memcache.rb', line 741 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.
787 788 789 |
# File 'lib/active_support/vendor/memcache-client-1.6.5/memcache.rb', line 787 def alive? !!socket end |
#close ⇒ Object
Close the connection to the memcached server targeted by this object. The server is not considered dead.
824 825 826 827 828 829 |
# File 'lib/active_support/vendor/memcache-client-1.6.5/memcache.rb', line 824 def close @sock.close if @sock && !@sock.closed? @sock = nil @retry = nil @status = "NOT CONNECTED" end |
#inspect ⇒ Object
Return a string representation of the server object.
777 778 779 |
# File 'lib/active_support/vendor/memcache-client-1.6.5/memcache.rb', line 777 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.
834 835 836 837 838 839 840 841 842 |
# File 'lib/active_support/vendor/memcache-client-1.6.5/memcache.rb', line 834 def mark_dead(error) @sock.close if @sock && !@sock.closed? @sock = nil @retry = Time.now + RETRY_DELAY reason = "#{error.class.name}: #{error.}" @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.
795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 |
# File 'lib/active_support/vendor/memcache-client-1.6.5/memcache.rb', line 795 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 = @timeout ? TCPTimeoutSocket.new(@host, @port, @timeout) : TCPSocket.new(@host, @port) if Socket.constants.include? 'TCP_NODELAY' then @sock.setsockopt Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1 end @retry = nil @status = 'CONNECTED' rescue SocketError, SystemCallError, IOError, Timeout::Error => err logger.warn { "Unable to open socket: #{err.class.name}, #{err.}" } if logger mark_dead err end return @sock end |