Module: RedisClient::ConnectionMixin

Included in:
RubyConnection
Defined in:
lib/redis_client/connection_mixin.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



6
7
8
# File 'lib/redis_client/connection_mixin.rb', line 6

def config
  @config
end

#retry_attemptObject

Returns the value of attribute retry_attempt.



5
6
7
# File 'lib/redis_client/connection_mixin.rb', line 5

def retry_attempt
  @retry_attempt
end

Instance Method Details

#call(command, timeout) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/redis_client/connection_mixin.rb', line 34

def call(command, timeout)
  @pending_reads += 1
  write(command)
  result = read(connection_timeout(timeout))
  @pending_reads -= 1
  if result.is_a?(Error)
    result._set_command(command)
    result._set_config(config)
    result._set_retry_attempt(@retry_attempt)
    raise result
  else
    result
  end
end

#call_pipelined(commands, timeouts, exception: true) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/redis_client/connection_mixin.rb', line 49

def call_pipelined(commands, timeouts, exception: true)
  first_exception = nil

  size = commands.size
  results = Array.new(commands.size)
  @pending_reads += size
  write_multi(commands)

  size.times do |index|
    timeout = timeouts && timeouts[index]
    result = read(connection_timeout(timeout))
    @pending_reads -= 1

    # A multi/exec command can return an array of results.
    # An error from a multi/exec command is handled in Multi#_coerce!.
    if result.is_a?(Array)
      result.each do |res|
        res._set_config(config) if res.is_a?(Error)
      end
    elsif result.is_a?(Error)
      result._set_command(commands[index])
      result._set_config(config)
      result._set_retry_attempt(@retry_attempt)
      first_exception ||= result
    end

    results[index] = result
  end

  if first_exception && exception
    raise first_exception
  else
    results
  end
end

#closeObject



20
21
22
23
# File 'lib/redis_client/connection_mixin.rb', line 20

def close
  @pending_reads = 0
  nil
end

#connection_error(message) ⇒ Object



100
101
102
103
104
# File 'lib/redis_client/connection_mixin.rb', line 100

def connection_error(message)
  error = ConnectionError.with_config(message, config)
  error._set_retry_attempt(@retry_attempt)
  error
end

#connection_timeout(timeout) ⇒ Object



85
86
87
88
89
90
91
92
# File 'lib/redis_client/connection_mixin.rb', line 85

def connection_timeout(timeout)
  return timeout unless timeout && timeout > 0

  # Can't use the command timeout argument as the connection timeout
  # otherwise it would be very racy. So we add the regular read_timeout on top
  # to account for the network delay.
  timeout + config.read_timeout
end

#initialize(config) ⇒ Object



8
9
10
11
12
13
# File 'lib/redis_client/connection_mixin.rb', line 8

def initialize(config)
  @pending_reads = 0
  @retry_attempt = nil
  @config = config
  @server_key = nil
end

#protocol_error(message) ⇒ Object



94
95
96
97
98
# File 'lib/redis_client/connection_mixin.rb', line 94

def protocol_error(message)
  error = ProtocolError.with_config(message, config)
  error._set_retry_attempt(@retry_attempt)
  error
end

#reconnectObject



15
16
17
18
# File 'lib/redis_client/connection_mixin.rb', line 15

def reconnect
  close
  connect
end

#revalidateObject



25
26
27
28
29
30
31
32
# File 'lib/redis_client/connection_mixin.rb', line 25

def revalidate
  if @pending_reads > 0 || @server_key != @config.server_key
    close
    false
  else
    connected?
  end
end