Module: RedisClient::Config::Common

Included in:
RedisClient::Config, SentinelConfig
Defined in:
lib/redis_client/config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#circuit_breakerObject (readonly)

Returns the value of attribute circuit_breaker.



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

def circuit_breaker
  @circuit_breaker
end

#command_builderObject (readonly)

Returns the value of attribute command_builder.



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

def command_builder
  @command_builder
end

#connect_timeoutObject (readonly)

Returns the value of attribute connect_timeout.



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

def connect_timeout
  @connect_timeout
end

#customObject (readonly)

Returns the value of attribute custom.



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

def custom
  @custom
end

#dbObject (readonly)

Returns the value of attribute db.



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

def db
  @db
end

#driverObject (readonly)

Returns the value of attribute driver.



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

def driver
  @driver
end

#driver_infoObject (readonly)

Returns the value of attribute driver_info.



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

def driver_info
  @driver_info
end

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

#idle_timeoutObject (readonly)

Returns the value of attribute idle_timeout.



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

def idle_timeout
  @idle_timeout
end

#inherit_socketObject (readonly)

Returns the value of attribute inherit_socket.



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

def inherit_socket
  @inherit_socket
end

#middlewares_stackObject (readonly)

Returns the value of attribute middlewares_stack.



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

def middlewares_stack
  @middlewares_stack
end

#protocolObject (readonly)

Returns the value of attribute protocol.



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

def protocol
  @protocol
end

#read_timeoutObject (readonly)

Returns the value of attribute read_timeout.



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

def read_timeout
  @read_timeout
end

#sslObject (readonly) Also known as: ssl?

Returns the value of attribute ssl.



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

def ssl
  @ssl
end

#ssl_paramsObject (readonly)

Returns the value of attribute ssl_params.



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

def ssl_params
  @ssl_params
end

#write_timeoutObject (readonly)

Returns the value of attribute write_timeout.



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

def write_timeout
  @write_timeout
end

Instance Method Details

#build_lib_nameString

Build the library name for CLIENT SETINFO LIB-NAME.

Raises:

  • if driver_info is not a String or Array

Parameters:

  • Upstream driver info

Returns:

  • Library name with optional upstream driver info



144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/redis_client/config.rb', line 144

def build_lib_name
  return "redis-client" if @driver_info.nil?

  info = case @driver_info
  when String then @driver_info
  when Array then @driver_info.join(";")
         else raise ArgumentError, "driver_info must be a String or Array of Strings"
  end

  return "redis-client" if info.empty?

  "redis-client(#{info})"
end

#connection_preludeObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/redis_client/config.rb', line 95

def connection_prelude
  prelude = []
  pass = password
  if protocol == 3
    prelude << if pass
      ["HELLO", "3", "AUTH", username, pass]
    else
      ["HELLO", "3"]
    end
  elsif pass
    prelude << if @username && !@username.empty?
      ["AUTH", @username, pass]
    else
      ["AUTH", pass]
    end
  end

  if @db && @db != 0
    prelude << ["SELECT", @db.to_s]
  end

  # Add CLIENT SETINFO commands for lib-name and lib-ver
  # These commands are supported in Redis 7.2+
  if @driver_info
    prelude << ["CLIENT", "SETINFO", "LIB-NAME", build_lib_name]
    prelude << ["CLIENT", "SETINFO", "LIB-VER", RedisClient::VERSION]
  end

  # Deep freeze all the strings and commands
  prelude.map! do |commands|
    commands = commands.map { |str| str.frozen? ? str : str.dup.freeze }
    commands.freeze
  end
  prelude.freeze
end

#initialize(username: nil, password: nil, db: nil, id: nil, timeout: DEFAULT_TIMEOUT, read_timeout: timeout, write_timeout: timeout, connect_timeout: timeout, idle_timeout: DEFAULT_IDLE_TIMEOUT, ssl: nil, custom: {}, ssl_params: nil, driver: nil, protocol: 3, client_implementation: RedisClient, command_builder: CommandBuilder, inherit_socket: false, reconnect_attempts: false, middlewares: false, circuit_breaker: nil, driver_info: nil) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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
84
85
86
87
88
89
90
91
92
93
# File 'lib/redis_client/config.rb', line 22

def initialize(
  username: nil,
  password: nil,
  db: nil,
  id: nil,
  timeout: DEFAULT_TIMEOUT,
  read_timeout: timeout,
  write_timeout: timeout,
  connect_timeout: timeout,
  idle_timeout: DEFAULT_IDLE_TIMEOUT,
  ssl: nil,
  custom: {},
  ssl_params: nil,
  driver: nil,
  protocol: 3,
  client_implementation: RedisClient,
  command_builder: CommandBuilder,
  inherit_socket: false,
  reconnect_attempts: false,
  middlewares: false,
  circuit_breaker: nil,
  driver_info: nil
)
  @username = username
  @password = password && !password.respond_to?(:call) ? ->(_) { password } : password
  @db = begin
    Integer(db || DEFAULT_DB)
  rescue ArgumentError
    raise ArgumentError, "db: must be an Integer, got: #{db.inspect}"
  end

  @id = id
  @ssl = ssl || false

  @ssl_params = ssl_params
  @connect_timeout = connect_timeout
  @read_timeout = read_timeout
  @write_timeout = write_timeout
  @idle_timeout = idle_timeout

  @driver = driver ? RedisClient.driver(driver) : RedisClient.default_driver

  @custom = custom

  @client_implementation = client_implementation
  @protocol = protocol
  unless protocol == 2 || protocol == 3
    raise ArgumentError, "Unknown protocol version #{protocol.inspect}, expected 2 or 3"
  end

  @command_builder = command_builder
  @inherit_socket = inherit_socket

  reconnect_attempts = Array.new(reconnect_attempts, 0).freeze if reconnect_attempts.is_a?(Integer)
  @reconnect_attempts = reconnect_attempts

  circuit_breaker = CircuitBreaker.new(**circuit_breaker) if circuit_breaker.is_a?(Hash)
  if @circuit_breaker = circuit_breaker
    middlewares = [CircuitBreaker::Middleware] + (middlewares || [])
  end

  middlewares_stack = Middlewares
  if middlewares && !middlewares.empty?
    middlewares_stack = Class.new(Middlewares)
    middlewares.each do |mod|
      middlewares_stack.include(mod)
    end
  end
  @middlewares_stack = middlewares_stack

  @driver_info = driver_info
end

#new_client(**kwargs) ⇒ Object



171
172
173
# File 'lib/redis_client/config.rb', line 171

def new_client(**kwargs)
  @client_implementation.new(self, **kwargs)
end

#new_pool(**kwargs) ⇒ Object



166
167
168
169
# File 'lib/redis_client/config.rb', line 166

def new_pool(**kwargs)
  kwargs[:timeout] ||= DEFAULT_TIMEOUT
  Pooled.new(self, **kwargs)
end

#passwordObject



131
132
133
# File 'lib/redis_client/config.rb', line 131

def password
  @password&.call(username)
end

#resolved?Boolean

Returns:



158
159
160
# File 'lib/redis_client/config.rb', line 158

def resolved?
  true
end

#retriable?(attempt) ⇒ Boolean

Returns:



175
176
177
# File 'lib/redis_client/config.rb', line 175

def retriable?(attempt)
  @reconnect_attempts && @reconnect_attempts[attempt]
end

#retry_connecting?(attempt, _error) ⇒ Boolean

Returns:



179
180
181
182
183
184
185
186
187
188
189
# File 'lib/redis_client/config.rb', line 179

def retry_connecting?(attempt, _error)
  if @reconnect_attempts
    if (pause = @reconnect_attempts[attempt])
      if pause > 0
        sleep(pause)
      end
      return true
    end
  end
  false
end

#sentinel?Boolean

Returns:



162
163
164
# File 'lib/redis_client/config.rb', line 162

def sentinel?
  false
end

#server_urlObject



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/redis_client/config.rb', line 197

def server_url
  if path
    url = "unix://#{path}"
    if db != 0
      url = "#{url}?db=#{db}"
    end
  else
    # add brackets to IPv6 address
    redis_host = if host.count(":") >= 2
      "[#{host}]"
    else
      host
    end
    url = "redis#{'s' if ssl?}://#{redis_host}:#{port}"
    if db != 0
      url = "#{url}/#{db}"
    end
  end
  url
end

#ssl_contextObject



191
192
193
194
195
# File 'lib/redis_client/config.rb', line 191

def ssl_context
  if ssl
    @ssl_context ||= @driver.ssl_context(@ssl_params || {})
  end
end

#usernameObject



135
136
137
# File 'lib/redis_client/config.rb', line 135

def username
  @username || DEFAULT_USERNAME
end