Class: Adocca::MemCache::Server

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

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

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.



347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# File 'lib/am_memcache.rb', line 347

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
  @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.



331
332
333
# File 'lib/am_memcache.rb', line 331

def host
  @host
end

#portObject (readonly)

The port the memcached server is listening on.



334
335
336
# File 'lib/am_memcache.rb', line 334

def port
  @port
end

#retryObject (readonly)

The time of next retry if the connection is dead.



340
341
342
# File 'lib/am_memcache.rb', line 340

def retry
  @retry
end

#statusObject (readonly)

A text status string describing the state of the server.



343
344
345
# File 'lib/am_memcache.rb', line 343

def status
  @status
end

#weightObject (readonly)

The weight given to the server.



337
338
339
# File 'lib/am_memcache.rb', line 337

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.



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

def alive?
  !self.socket.nil?
end

#closeObject

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



402
403
404
405
406
407
# File 'lib/am_memcache.rb', line 402

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.



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

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.



410
411
412
413
414
415
416
# File 'lib/am_memcache.rb', line 410

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.



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

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 SystemCallError, IOError, Timeout::Error => err
        self.mark_dead(err.message)
      end
    end
  end
  @sock
end