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.

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.



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

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.



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

def host
  @host
end

#portObject (readonly)

The port the memcached server is listening on.



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

def port
  @port
end

#retryObject (readonly)

The time of next retry if the connection is dead.



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

def retry
  @retry
end

#statusObject (readonly)

A text status string describing the state of the server.



378
379
380
# File 'lib/am_memcache.rb', line 378

def status
  @status
end

#weightObject (readonly)

The weight given to the server.



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

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)


409
410
411
# File 'lib/am_memcache.rb', line 409

def alive?
  !self.socket.nil?
end

#closeObject

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



439
440
441
442
443
444
# File 'lib/am_memcache.rb', line 439

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.



400
401
402
403
# File 'lib/am_memcache.rb', line 400

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.



447
448
449
450
451
452
453
# File 'lib/am_memcache.rb', line 447

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.



415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
# File 'lib/am_memcache.rb', line 415

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)
        }
        @retry  = nil
        @status = "CONNECTED"
      rescue Exception => err
        self.mark_dead(err.message)
        MemCache.log_error(err)
      end
    end
  end
  @sock
end