Class: Adocca::MemCache::Server

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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.



380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
# File 'lib/am_memcache.rb', line 380

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

#hostObject (readonly)

The host the memcached server is running on.



364
365
366
# File 'lib/am_memcache.rb', line 364

def host
  @host
end

#portObject (readonly)

The port the memcached server is listening on.



367
368
369
# File 'lib/am_memcache.rb', line 367

def port
  @port
end

#retryObject (readonly)

The time of next retry if the connection is dead.



373
374
375
# File 'lib/am_memcache.rb', line 373

def retry
  @retry
end

#statusObject (readonly)

A text status string describing the state of the server.



376
377
378
# File 'lib/am_memcache.rb', line 376

def status
  @status
end

#weightObject (readonly)

The weight given to the server.



370
371
372
# File 'lib/am_memcache.rb', line 370

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.

Returns:

  • (Boolean)


407
408
409
# File 'lib/am_memcache.rb', line 407

def alive?
  !self.socket.nil?
end

#closeObject

Close the connection to the memcached server targeted by this object. The server is not considered dead.



457
458
459
460
461
462
# File 'lib/am_memcache.rb', line 457

def close
  @sock.close if @sock &&!@sock.closed?
  @sock   = nil
  @retry  = nil
  @status = "NOT CONNECTED"
end

#inspectObject

Return a string representation of the server object.



398
399
400
401
# File 'lib/am_memcache.rb', line 398

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.



465
466
467
468
469
470
471
# File 'lib/am_memcache.rb', line 465

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

#socketObject

Try to connect to the memcached server targeted by this object. Returns the connected socket object on success or nil on failure.



432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
# File 'lib/am_memcache.rb', line 432

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.message)
        MemCache.log_error(err)
      end
    end
  end
  @sock
end