Class: Dalli::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/dalli/server.rb

Constant Summary collapse

DEFAULTS =
{
  # seconds between trying to contact a remote server
  :down_retry_delay => 1,
  # connect/read/write timeout for socket operations
  :socket_timeout => 0.5,
  # times a socket operation may fail before considering the server dead
  :socket_max_failures => 2,
  # amount of time to sleep between retries when a failure occurs
  :socket_failure_delay => 0.01
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attribs, options = {}) ⇒ Server

Returns a new instance of Server.



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/dalli/server.rb', line 23

def initialize(attribs, options = {})
  (@hostname, @port, @weight) = attribs.split(':')
  @port ||= 11211
  @port = Integer(@port)
  @weight ||= 1
  @weight = Integer(@weight)
  @fail_count = 0
  @down_at = nil
  @last_down_at = nil
  @options = DEFAULTS.merge(options)
end

Instance Attribute Details

#hostnameObject

Returns the value of attribute hostname.



7
8
9
# File 'lib/dalli/server.rb', line 7

def hostname
  @hostname
end

#optionsObject

Returns the value of attribute options.



10
11
12
# File 'lib/dalli/server.rb', line 10

def options
  @options
end

#portObject

Returns the value of attribute port.



8
9
10
# File 'lib/dalli/server.rb', line 8

def port
  @port
end

#weightObject

Returns the value of attribute weight.



9
10
11
# File 'lib/dalli/server.rb', line 9

def weight
  @weight
end

Instance Method Details

#alive?Boolean

Returns:

  • (Boolean)


52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/dalli/server.rb', line 52

def alive?
  return true if @sock

  if @last_down_at && @last_down_at + options[:down_retry_delay] >= Time.now
    time = @last_down_at + options[:down_retry_delay] - Time.now
    Dalli.logger.debug { "down_retry_delay not reached for #{hostname}:#{port} (%.3f seconds left)" % time }
    return false
  end

  connect
  !!@sock
rescue Dalli::NetworkError
  false
end

#closeObject



67
68
69
70
71
# File 'lib/dalli/server.rb', line 67

def close
  return unless @sock
  @sock.close rescue nil
  @sock = nil
end

#lock!Object



73
74
# File 'lib/dalli/server.rb', line 73

def lock!
end

#request(op, *args) ⇒ Object

Chokepoint method for instrumentation



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/dalli/server.rb', line 36

def request(op, *args)
  raise Dalli::NetworkError, "#{hostname}:#{port} is down: #{@error} #{@msg}" unless alive?
  begin
    send(op, *args)
  rescue Dalli::NetworkError
    raise
  rescue Dalli::DalliError
    raise
  rescue Exception => ex
    Dalli.logger.error "Unexpected exception in Dalli: #{ex.class.name}: #{ex.message}"
    Dalli.logger.error "This is a bug in Dalli, please enter an issue in Github if it does not already exist."
    Dalli.logger.error ex.backtrace.join("\n\t")
    down!
  end
end

#unlock!Object



76
77
# File 'lib/dalli/server.rb', line 76

def unlock!
end