Class: Adocca::MemCache::Server
- Inherits:
-
Object
- Object
- Adocca::MemCache::Server
- Extended by:
- Forwardable
- Defined in:
- lib/am_memcache.rb
Overview
This class represents a memcached server instance.
Defined Under Namespace
Modules: TimeoutSocket
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.
1
- RETRY_DELAY =
The amount of time to wait before attempting to re-establish a connection with a server that is marked dead.
60
Instance Attribute Summary collapse
-
#host ⇒ Object
readonly
The host the memcached server is running on.
-
#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(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(reason = "Unknown 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(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.
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 |
# File 'lib/am_memcache.rb', line 381 def initialize(host, port = DEFAULT_PORT, weight = DEFAULT_WEIGHT) if host.nil? || host.empty? raise ArgumentError, "No host specified" elsif port.nil? || port.to_i.zero? raise ArgumentError, "No port specified" end @host = host @port = port.to_i @id = Integer("0x#{Digest::SHA1.hexdigest("#{host}:#{port}")}") @weight = weight.to_i @sock = nil @retry = nil @status = "NOT CONNECTED" end |
Instance Attribute Details
#host ⇒ Object (readonly)
The host the memcached server is running on.
365 366 367 |
# File 'lib/am_memcache.rb', line 365 def host @host end |
#port ⇒ Object (readonly)
The port the memcached server is listening on.
368 369 370 |
# File 'lib/am_memcache.rb', line 368 def port @port end |
#retry ⇒ Object (readonly)
The time of next retry if the connection is dead.
374 375 376 |
# File 'lib/am_memcache.rb', line 374 def retry @retry end |
#status ⇒ Object (readonly)
A text status string describing the state of the server.
377 378 379 |
# File 'lib/am_memcache.rb', line 377 def status @status end |
#weight ⇒ Object (readonly)
The weight given to the server.
371 372 373 |
# File 'lib/am_memcache.rb', line 371 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.
408 409 410 |
# File 'lib/am_memcache.rb', line 408 def alive? !self.socket.nil? end |
#close ⇒ Object
Close the connection to the memcached server targeted by this object. The server is not considered dead.
458 459 460 461 462 463 |
# File 'lib/am_memcache.rb', line 458 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.
399 400 401 402 |
# File 'lib/am_memcache.rb', line 399 def inspect sprintf("<MemCache::Server: %s:%d [%d] (%s)>", @host, @port, @weight, @status) end |
#mark_dead(reason = "Unknown error") ⇒ Object
Mark the server as dead and close its socket.
466 467 468 469 470 471 472 |
# File 'lib/am_memcache.rb', line 466 def mark_dead(reason = "Unknown error") @sock.close if @sock && !@sock.closed? @sock = nil @retry = Time::now + RETRY_DELAY @status = sprintf("DEAD: %s, will retry at %s", reason, @retry) 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.
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 |
# File 'lib/am_memcache.rb', line 433 def socket # Attempt to connect if not already connected. unless @sock || (!@sock.nil? && @sock.closed?) # If the host was dead, don't retry for a while. if @retry && (@retry > Time::now) @sock = nil else begin @sock = timeout(CONNECT_TIMEOUT) { TCPSocket::new(@host, @port) } @sock.extend(TimeoutSocket) @retry = nil @status = "CONNECTED" rescue Exception => err self.mark_dead(err.) MemCache.log_error(err) end end end @sock end |