redis-client 0.22.2 → 0.31.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +100 -1
- data/README.md +60 -6
- data/lib/redis_client/circuit_breaker.rb +6 -3
- data/lib/redis_client/config.rb +81 -40
- data/lib/redis_client/connection_mixin.rb +29 -6
- data/lib/redis_client/hash_ring.rb +87 -0
- data/lib/redis_client/pooled.rb +2 -2
- data/lib/redis_client/ruby_connection/buffered_io.rb +10 -2
- data/lib/redis_client/ruby_connection/resp3.rb +2 -36
- data/lib/redis_client/ruby_connection.rb +23 -21
- data/lib/redis_client/sentinel_config.rb +27 -6
- data/lib/redis_client/version.rb +1 -1
- data/lib/redis_client.rb +141 -19
- metadata +4 -11
- data/Gemfile +0 -22
- data/Gemfile.lock +0 -72
- data/Rakefile +0 -126
- data/redis-client.gemspec +0 -32
|
@@ -193,12 +193,17 @@ class RedisClient
|
|
|
193
193
|
buffer_size = @buffer.bytesize
|
|
194
194
|
start = @offset - buffer_size
|
|
195
195
|
empty_buffer = start >= 0
|
|
196
|
+
read_buffer = nil
|
|
196
197
|
|
|
197
198
|
loop do
|
|
198
199
|
bytes = if empty_buffer
|
|
199
200
|
@io.read_nonblock([remaining, @chunk_size].max, @buffer, exception: false)
|
|
200
201
|
else
|
|
201
|
-
|
|
202
|
+
# We're most likely reading 4kiB or more, hence the read buffer will never be
|
|
203
|
+
# embedded on any recent Ruby implementation, hence we can provide an empty
|
|
204
|
+
# buffer, Ruby will take care of preallocating the required memory.
|
|
205
|
+
read_buffer ||= "".b
|
|
206
|
+
@io.read_nonblock([remaining, @chunk_size].max, read_buffer, exception: false)
|
|
202
207
|
end
|
|
203
208
|
|
|
204
209
|
case bytes
|
|
@@ -233,7 +238,10 @@ class RedisClient
|
|
|
233
238
|
@buffer << bytes.force_encoding(ENCODING)
|
|
234
239
|
end
|
|
235
240
|
remaining -= bytes.bytesize
|
|
236
|
-
|
|
241
|
+
if !strict || remaining <= 0
|
|
242
|
+
read_buffer&.clear
|
|
243
|
+
return
|
|
244
|
+
end
|
|
237
245
|
end
|
|
238
246
|
end
|
|
239
247
|
end
|
|
@@ -16,21 +16,6 @@ class RedisClient
|
|
|
16
16
|
Integer => :dump_numeric,
|
|
17
17
|
Float => :dump_numeric,
|
|
18
18
|
}.freeze
|
|
19
|
-
PARSER_TYPES = {
|
|
20
|
-
'#' => :parse_boolean,
|
|
21
|
-
'$' => :parse_blob,
|
|
22
|
-
'+' => :parse_string,
|
|
23
|
-
'=' => :parse_verbatim_string,
|
|
24
|
-
'-' => :parse_error,
|
|
25
|
-
':' => :parse_integer,
|
|
26
|
-
'(' => :parse_integer,
|
|
27
|
-
',' => :parse_double,
|
|
28
|
-
'_' => :parse_null,
|
|
29
|
-
'*' => :parse_array,
|
|
30
|
-
'%' => :parse_map,
|
|
31
|
-
'~' => :parse_set,
|
|
32
|
-
'>' => :parse_array,
|
|
33
|
-
}.transform_keys(&:ord).freeze
|
|
34
19
|
INTEGER_RANGE = ((((2**64) / 2) * -1)..(((2**64) / 2) - 1)).freeze
|
|
35
20
|
|
|
36
21
|
def dump(command, buffer = nil)
|
|
@@ -73,23 +58,6 @@ class RedisClient
|
|
|
73
58
|
buffer
|
|
74
59
|
end
|
|
75
60
|
|
|
76
|
-
def dump_set(set, buffer)
|
|
77
|
-
buffer << '~' << set.size.to_s << EOL
|
|
78
|
-
set.each do |item|
|
|
79
|
-
dump_any(item, buffer)
|
|
80
|
-
end
|
|
81
|
-
buffer
|
|
82
|
-
end
|
|
83
|
-
|
|
84
|
-
def dump_hash(hash, buffer)
|
|
85
|
-
buffer << '%' << hash.size.to_s << EOL
|
|
86
|
-
hash.each_pair do |key, value|
|
|
87
|
-
dump_any(key, buffer)
|
|
88
|
-
dump_any(value, buffer)
|
|
89
|
-
end
|
|
90
|
-
buffer
|
|
91
|
-
end
|
|
92
|
-
|
|
93
61
|
def dump_numeric(numeric, buffer)
|
|
94
62
|
dump_string(numeric.to_s, buffer)
|
|
95
63
|
end
|
|
@@ -137,6 +105,8 @@ class RedisClient
|
|
|
137
105
|
parse_set(io)
|
|
138
106
|
elsif type == 62 # '>'.ord
|
|
139
107
|
parse_array(io)
|
|
108
|
+
elsif type.nil?
|
|
109
|
+
raise SyntaxError, "Unexpected end of stream"
|
|
140
110
|
else
|
|
141
111
|
raise UnknownType, "Unknown sigil type: #{type.chr.inspect}"
|
|
142
112
|
end
|
|
@@ -179,10 +149,6 @@ class RedisClient
|
|
|
179
149
|
hash
|
|
180
150
|
end
|
|
181
151
|
|
|
182
|
-
def parse_push(io)
|
|
183
|
-
parse_array(io)
|
|
184
|
-
end
|
|
185
|
-
|
|
186
152
|
def parse_sequence(io, size)
|
|
187
153
|
return if size < 0 # RESP2 nil
|
|
188
154
|
|
|
@@ -28,23 +28,14 @@ class RedisClient
|
|
|
28
28
|
|
|
29
29
|
context = OpenSSL::SSL::SSLContext.new
|
|
30
30
|
context.set_params(params)
|
|
31
|
-
if context.verify_mode != OpenSSL::SSL::VERIFY_NONE
|
|
32
|
-
if context.respond_to?(:verify_hostname) # Missing on JRuby
|
|
33
|
-
context.verify_hostname
|
|
34
|
-
end
|
|
35
|
-
end
|
|
36
|
-
|
|
37
31
|
context
|
|
38
32
|
end
|
|
39
33
|
end
|
|
40
34
|
|
|
41
35
|
SUPPORTS_RESOLV_TIMEOUT = Socket.method(:tcp).parameters.any? { |p| p.last == :resolv_timeout }
|
|
42
36
|
|
|
43
|
-
attr_reader :config
|
|
44
|
-
|
|
45
37
|
def initialize(config, connect_timeout:, read_timeout:, write_timeout:)
|
|
46
|
-
super()
|
|
47
|
-
@config = config
|
|
38
|
+
super(config)
|
|
48
39
|
@connect_timeout = connect_timeout
|
|
49
40
|
@read_timeout = read_timeout
|
|
50
41
|
@write_timeout = write_timeout
|
|
@@ -75,7 +66,11 @@ class RedisClient
|
|
|
75
66
|
begin
|
|
76
67
|
@io.write(buffer)
|
|
77
68
|
rescue SystemCallError, IOError, OpenSSL::SSL::SSLError => error
|
|
78
|
-
raise
|
|
69
|
+
raise connection_error(error.message)
|
|
70
|
+
rescue Error => error
|
|
71
|
+
error._set_config(config)
|
|
72
|
+
error._set_retry_attempt(@retry_attempt)
|
|
73
|
+
raise error
|
|
79
74
|
end
|
|
80
75
|
end
|
|
81
76
|
|
|
@@ -87,7 +82,7 @@ class RedisClient
|
|
|
87
82
|
begin
|
|
88
83
|
@io.write(buffer)
|
|
89
84
|
rescue SystemCallError, IOError, OpenSSL::SSL::SSLError => error
|
|
90
|
-
raise
|
|
85
|
+
raise connection_error(error.message)
|
|
91
86
|
end
|
|
92
87
|
end
|
|
93
88
|
|
|
@@ -97,26 +92,32 @@ class RedisClient
|
|
|
97
92
|
else
|
|
98
93
|
@io.with_timeout(timeout) { RESP3.load(@io) }
|
|
99
94
|
end
|
|
100
|
-
rescue RedisClient::RESP3::
|
|
101
|
-
raise
|
|
95
|
+
rescue RedisClient::RESP3::Error => error
|
|
96
|
+
raise protocol_error(error.message)
|
|
102
97
|
rescue SystemCallError, IOError, OpenSSL::SSL::SSLError => error
|
|
103
|
-
raise
|
|
98
|
+
raise connection_error(error.message)
|
|
104
99
|
end
|
|
105
100
|
|
|
106
101
|
def measure_round_trip_delay
|
|
107
|
-
start =
|
|
102
|
+
start = RedisClient.now_ms
|
|
108
103
|
call(["PING"], @read_timeout)
|
|
109
|
-
|
|
104
|
+
RedisClient.now_ms - start
|
|
110
105
|
end
|
|
111
106
|
|
|
112
107
|
private
|
|
113
108
|
|
|
114
109
|
def connect
|
|
110
|
+
@server_key = @config.server_key
|
|
115
111
|
socket = if @config.path
|
|
116
112
|
UNIXSocket.new(@config.path)
|
|
117
113
|
else
|
|
118
114
|
sock = if SUPPORTS_RESOLV_TIMEOUT
|
|
119
|
-
|
|
115
|
+
begin
|
|
116
|
+
Socket.tcp(@config.host, @config.port, connect_timeout: @connect_timeout, resolv_timeout: @connect_timeout)
|
|
117
|
+
rescue Errno::ETIMEDOUT => timeout_error
|
|
118
|
+
timeout_error.message << ": #{@connect_timeout}s"
|
|
119
|
+
raise
|
|
120
|
+
end
|
|
120
121
|
else
|
|
121
122
|
Socket.tcp(@config.host, @config.port, connect_timeout: @connect_timeout)
|
|
122
123
|
end
|
|
@@ -149,9 +150,10 @@ class RedisClient
|
|
|
149
150
|
write_timeout: @write_timeout,
|
|
150
151
|
)
|
|
151
152
|
true
|
|
152
|
-
rescue SystemCallError, OpenSSL::SSL::SSLError, SocketError => error
|
|
153
|
-
socket&.close
|
|
153
|
+
rescue SystemCallError, IOError, OpenSSL::SSL::SSLError, SocketError => error
|
|
154
154
|
raise CannotConnectError, error.message, error.backtrace
|
|
155
|
+
ensure
|
|
156
|
+
socket&.to_io&.close unless defined?(@io) && @io
|
|
155
157
|
end
|
|
156
158
|
|
|
157
159
|
KEEP_ALIVE_INTERVAL = 15 # Same as hiredis defaults
|
|
@@ -164,7 +166,7 @@ class RedisClient
|
|
|
164
166
|
if %i[SOL_TCP SOL_SOCKET TCP_KEEPIDLE TCP_KEEPINTVL TCP_KEEPCNT].all? { |c| Socket.const_defined? c } # Linux
|
|
165
167
|
def enable_socket_keep_alive(socket)
|
|
166
168
|
socket.setsockopt(Socket::SOL_SOCKET, Socket::SO_KEEPALIVE, true)
|
|
167
|
-
socket.setsockopt(Socket::SOL_TCP, Socket::TCP_KEEPIDLE,
|
|
169
|
+
socket.setsockopt(Socket::SOL_TCP, Socket::TCP_KEEPIDLE, KEEP_ALIVE_TTL)
|
|
168
170
|
socket.setsockopt(Socket::SOL_TCP, Socket::TCP_KEEPINTVL, KEEP_ALIVE_INTERVAL)
|
|
169
171
|
socket.setsockopt(Socket::SOL_TCP, Socket::TCP_KEEPCNT, KEEP_ALIVE_PROBES)
|
|
170
172
|
end
|
|
@@ -22,12 +22,16 @@ class RedisClient
|
|
|
22
22
|
raise ArgumentError, "Expected role to be either :master or :replica, got: #{role.inspect}"
|
|
23
23
|
end
|
|
24
24
|
|
|
25
|
+
# Track whether SSL was explicitly provided by user
|
|
26
|
+
ssl_explicitly_set = client_config.key?(:ssl)
|
|
27
|
+
|
|
25
28
|
if url
|
|
26
29
|
url_config = URLConfig.new(url)
|
|
27
30
|
client_config = {
|
|
28
31
|
username: url_config.username,
|
|
29
32
|
password: url_config.password,
|
|
30
33
|
db: url_config.db,
|
|
34
|
+
ssl: url_config.ssl?,
|
|
31
35
|
}.compact.merge(client_config)
|
|
32
36
|
name ||= url_config.host
|
|
33
37
|
end
|
|
@@ -38,9 +42,14 @@ class RedisClient
|
|
|
38
42
|
end
|
|
39
43
|
|
|
40
44
|
@to_list_of_hash = @to_hash = nil
|
|
45
|
+
password = if sentinel_password && !sentinel_password.respond_to?(:call)
|
|
46
|
+
->(_) { sentinel_password }
|
|
47
|
+
else
|
|
48
|
+
sentinel_password
|
|
49
|
+
end
|
|
41
50
|
@extra_config = {
|
|
42
51
|
username: sentinel_username,
|
|
43
|
-
password:
|
|
52
|
+
password: password,
|
|
44
53
|
db: nil,
|
|
45
54
|
}
|
|
46
55
|
if client_config[:protocol] == 2
|
|
@@ -61,6 +70,10 @@ class RedisClient
|
|
|
61
70
|
|
|
62
71
|
client_config[:reconnect_attempts] ||= DEFAULT_RECONNECT_ATTEMPTS
|
|
63
72
|
@client_config = client_config || {}
|
|
73
|
+
@sentinel_client_config = @client_config.dup
|
|
74
|
+
# Only remove SSL from sentinel config if it was derived from URL,
|
|
75
|
+
# not if explicitly set by user.
|
|
76
|
+
@sentinel_client_config.delete(:ssl) unless ssl_explicitly_set
|
|
64
77
|
super(**client_config)
|
|
65
78
|
@sentinel_configs = sentinels_to_configs(sentinels)
|
|
66
79
|
end
|
|
@@ -77,6 +90,10 @@ class RedisClient
|
|
|
77
90
|
end
|
|
78
91
|
end
|
|
79
92
|
|
|
93
|
+
def server_key
|
|
94
|
+
config.server_key
|
|
95
|
+
end
|
|
96
|
+
|
|
80
97
|
def host
|
|
81
98
|
config.host
|
|
82
99
|
end
|
|
@@ -99,6 +116,10 @@ class RedisClient
|
|
|
99
116
|
end
|
|
100
117
|
|
|
101
118
|
def check_role!(role)
|
|
119
|
+
unless role.is_a?(String)
|
|
120
|
+
raise TypeError, "Expected role to be a string, got: #{role.inspect}"
|
|
121
|
+
end
|
|
122
|
+
|
|
102
123
|
if @role == :master
|
|
103
124
|
unless role == "master"
|
|
104
125
|
sleep SENTINEL_DELAY
|
|
@@ -124,9 +145,9 @@ class RedisClient
|
|
|
124
145
|
sentinels.map do |sentinel|
|
|
125
146
|
case sentinel
|
|
126
147
|
when String
|
|
127
|
-
Config.new(**@
|
|
148
|
+
Config.new(**@sentinel_client_config, **@extra_config, url: sentinel)
|
|
128
149
|
else
|
|
129
|
-
Config.new(**@
|
|
150
|
+
Config.new(**@sentinel_client_config, **@extra_config, **sentinel)
|
|
130
151
|
end
|
|
131
152
|
end
|
|
132
153
|
end
|
|
@@ -143,7 +164,7 @@ class RedisClient
|
|
|
143
164
|
|
|
144
165
|
def resolve_master
|
|
145
166
|
each_sentinel do |sentinel_client|
|
|
146
|
-
host, port = sentinel_client.
|
|
167
|
+
host, port = sentinel_client.call_v(["SENTINEL", "get-master-addr-by-name", @name])
|
|
147
168
|
next unless host && port
|
|
148
169
|
|
|
149
170
|
refresh_sentinels(sentinel_client)
|
|
@@ -162,7 +183,7 @@ class RedisClient
|
|
|
162
183
|
|
|
163
184
|
def resolve_replica
|
|
164
185
|
each_sentinel do |sentinel_client|
|
|
165
|
-
replicas = sentinel_client.
|
|
186
|
+
replicas = sentinel_client.call_v(["SENTINEL", "replicas", @name], &@to_list_of_hash)
|
|
166
187
|
replicas.reject! do |r|
|
|
167
188
|
flags = r["flags"].to_s.split(",")
|
|
168
189
|
flags.include?("s_down") || flags.include?("o_down")
|
|
@@ -205,7 +226,7 @@ class RedisClient
|
|
|
205
226
|
end
|
|
206
227
|
|
|
207
228
|
def refresh_sentinels(sentinel_client)
|
|
208
|
-
sentinel_response = sentinel_client.
|
|
229
|
+
sentinel_response = sentinel_client.call_v(["SENTINEL", "sentinels", @name], &@to_list_of_hash)
|
|
209
230
|
sentinels = sentinel_response.map do |sentinel|
|
|
210
231
|
{ host: sentinel.fetch("ip"), port: Integer(sentinel.fetch("port")) }
|
|
211
232
|
end
|
data/lib/redis_client/version.rb
CHANGED
data/lib/redis_client.rb
CHANGED
|
@@ -45,6 +45,14 @@ class RedisClient
|
|
|
45
45
|
def default_driver=(name)
|
|
46
46
|
@default_driver = driver(name)
|
|
47
47
|
end
|
|
48
|
+
|
|
49
|
+
def now
|
|
50
|
+
Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def now_ms
|
|
54
|
+
Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond)
|
|
55
|
+
end
|
|
48
56
|
end
|
|
49
57
|
|
|
50
58
|
register_driver :ruby do
|
|
@@ -53,7 +61,7 @@ class RedisClient
|
|
|
53
61
|
end
|
|
54
62
|
|
|
55
63
|
module Common
|
|
56
|
-
attr_reader :config, :id
|
|
64
|
+
attr_reader :config, :id, :nodes
|
|
57
65
|
attr_accessor :connect_timeout, :read_timeout, :write_timeout
|
|
58
66
|
|
|
59
67
|
def initialize(
|
|
@@ -70,11 +78,21 @@ class RedisClient
|
|
|
70
78
|
@write_timeout = write_timeout
|
|
71
79
|
@command_builder = config.command_builder
|
|
72
80
|
@pid = PIDCache.pid
|
|
81
|
+
@nodes = [self].freeze
|
|
73
82
|
end
|
|
74
83
|
|
|
75
84
|
def timeout=(timeout)
|
|
76
85
|
@connect_timeout = @read_timeout = @write_timeout = timeout
|
|
77
86
|
end
|
|
87
|
+
|
|
88
|
+
def node_for(_key)
|
|
89
|
+
self
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def nodes_for(*keys)
|
|
93
|
+
keys.flatten!
|
|
94
|
+
{ self => keys }
|
|
95
|
+
end
|
|
78
96
|
end
|
|
79
97
|
|
|
80
98
|
module HasConfig
|
|
@@ -91,13 +109,55 @@ class RedisClient
|
|
|
91
109
|
end
|
|
92
110
|
end
|
|
93
111
|
|
|
112
|
+
module Retriable
|
|
113
|
+
def _set_retry_attempt(retry_attempt)
|
|
114
|
+
@retry_attempt = retry_attempt
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
def retry_attempt
|
|
118
|
+
@retry_attempt || 0
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
def retriable?
|
|
122
|
+
!!@retry_attempt
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
def final?
|
|
126
|
+
!@retry_attempt
|
|
127
|
+
end
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
module Final
|
|
131
|
+
def _set_retry_attempt(_retry_attempt)
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
def retry_attempt
|
|
135
|
+
0
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
def retriable?
|
|
139
|
+
false
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
def final?
|
|
143
|
+
true
|
|
144
|
+
end
|
|
145
|
+
end
|
|
146
|
+
|
|
94
147
|
class Error < StandardError
|
|
95
148
|
include HasConfig
|
|
149
|
+
include Retriable
|
|
150
|
+
|
|
151
|
+
attr_reader :next_error
|
|
96
152
|
|
|
97
153
|
def self.with_config(message, config = nil)
|
|
98
|
-
new(message)
|
|
99
|
-
|
|
100
|
-
|
|
154
|
+
error = new(message)
|
|
155
|
+
error._set_config(config)
|
|
156
|
+
error
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
def _set_next_error(error) # :nodoc:
|
|
160
|
+
@next_error = error
|
|
101
161
|
end
|
|
102
162
|
end
|
|
103
163
|
|
|
@@ -122,8 +182,19 @@ class RedisClient
|
|
|
122
182
|
end
|
|
123
183
|
end
|
|
124
184
|
|
|
185
|
+
module HasCode
|
|
186
|
+
attr_reader :code
|
|
187
|
+
|
|
188
|
+
def initialize(message = nil, code = nil)
|
|
189
|
+
super(message)
|
|
190
|
+
@code = code
|
|
191
|
+
end
|
|
192
|
+
end
|
|
193
|
+
|
|
125
194
|
class CommandError < Error
|
|
126
195
|
include HasCommand
|
|
196
|
+
include HasCode
|
|
197
|
+
include Final
|
|
127
198
|
|
|
128
199
|
class << self
|
|
129
200
|
def parse(error_message)
|
|
@@ -136,7 +207,7 @@ class RedisClient
|
|
|
136
207
|
end
|
|
137
208
|
code ||= error_message.split(' ', 2).first
|
|
138
209
|
klass = ERRORS.fetch(code, self)
|
|
139
|
-
klass.new(error_message.strip)
|
|
210
|
+
klass.new(error_message.strip, code.freeze)
|
|
140
211
|
end
|
|
141
212
|
end
|
|
142
213
|
end
|
|
@@ -145,12 +216,15 @@ class RedisClient
|
|
|
145
216
|
PermissionError = Class.new(CommandError)
|
|
146
217
|
WrongTypeError = Class.new(CommandError)
|
|
147
218
|
OutOfMemoryError = Class.new(CommandError)
|
|
219
|
+
NoScriptError = Class.new(CommandError)
|
|
148
220
|
|
|
149
221
|
ReadOnlyError = Class.new(ConnectionError)
|
|
150
222
|
ReadOnlyError.include(HasCommand)
|
|
223
|
+
ReadOnlyError.include(HasCode)
|
|
151
224
|
|
|
152
225
|
MasterDownError = Class.new(ConnectionError)
|
|
153
226
|
MasterDownError.include(HasCommand)
|
|
227
|
+
MasterDownError.include(HasCode)
|
|
154
228
|
|
|
155
229
|
CommandError::ERRORS = {
|
|
156
230
|
"WRONGPASS" => AuthenticationError,
|
|
@@ -159,6 +233,7 @@ class RedisClient
|
|
|
159
233
|
"MASTERDOWN" => MasterDownError,
|
|
160
234
|
"WRONGTYPE" => WrongTypeError,
|
|
161
235
|
"OOM" => OutOfMemoryError,
|
|
236
|
+
"NOSCRIPT" => NoScriptError,
|
|
162
237
|
}.freeze
|
|
163
238
|
|
|
164
239
|
class << self
|
|
@@ -170,6 +245,12 @@ class RedisClient
|
|
|
170
245
|
SentinelConfig.new(client_implementation: self, **kwargs)
|
|
171
246
|
end
|
|
172
247
|
|
|
248
|
+
def ring(*clients, **options)
|
|
249
|
+
clients.flatten!
|
|
250
|
+
require "redis_client/hash_ring" unless defined?(HashRing)
|
|
251
|
+
HashRing.new(clients, **options)
|
|
252
|
+
end
|
|
253
|
+
|
|
173
254
|
def new(arg = nil, **kwargs)
|
|
174
255
|
if arg.is_a?(Config::Common)
|
|
175
256
|
super
|
|
@@ -190,6 +271,7 @@ class RedisClient
|
|
|
190
271
|
@middlewares = config.middlewares_stack.new(self)
|
|
191
272
|
@raw_connection = nil
|
|
192
273
|
@disable_reconnection = false
|
|
274
|
+
@retry_attempt = nil
|
|
193
275
|
end
|
|
194
276
|
|
|
195
277
|
def inspect
|
|
@@ -209,6 +291,10 @@ class RedisClient
|
|
|
209
291
|
config.read_timeout
|
|
210
292
|
end
|
|
211
293
|
|
|
294
|
+
def idle_timeout
|
|
295
|
+
config.idle_timeout
|
|
296
|
+
end
|
|
297
|
+
|
|
212
298
|
def db
|
|
213
299
|
config.db
|
|
214
300
|
end
|
|
@@ -684,6 +770,7 @@ class RedisClient
|
|
|
684
770
|
close if !config.inherit_socket && @pid != PIDCache.pid
|
|
685
771
|
|
|
686
772
|
if @disable_reconnection
|
|
773
|
+
@raw_connection.retry_attempt = nil
|
|
687
774
|
if block_given?
|
|
688
775
|
yield @raw_connection
|
|
689
776
|
else
|
|
@@ -694,17 +781,25 @@ class RedisClient
|
|
|
694
781
|
connection = nil
|
|
695
782
|
preferred_error = nil
|
|
696
783
|
begin
|
|
784
|
+
@retry_attempt = config.retriable?(tries) ? tries : nil
|
|
697
785
|
connection = raw_connection
|
|
698
786
|
if block_given?
|
|
699
|
-
yield connection
|
|
787
|
+
result = yield connection
|
|
788
|
+
@last_used_at = RedisClient.now
|
|
789
|
+
result
|
|
700
790
|
else
|
|
701
791
|
connection
|
|
702
792
|
end
|
|
703
793
|
rescue ConnectionError, ProtocolError => error
|
|
704
794
|
preferred_error ||= error
|
|
705
|
-
preferred_error = error unless error.is_a?(CircuitBreaker::OpenCircuitError)
|
|
706
795
|
close
|
|
707
796
|
|
|
797
|
+
if error.is_a?(CircuitBreaker::OpenCircuitError)
|
|
798
|
+
raise preferred_error
|
|
799
|
+
else
|
|
800
|
+
preferred_error = error
|
|
801
|
+
end
|
|
802
|
+
|
|
708
803
|
if !@disable_reconnection && config.retry_connecting?(tries, error)
|
|
709
804
|
tries += 1
|
|
710
805
|
retry
|
|
@@ -717,7 +812,10 @@ class RedisClient
|
|
|
717
812
|
connection = ensure_connected
|
|
718
813
|
begin
|
|
719
814
|
@disable_reconnection = true
|
|
720
|
-
|
|
815
|
+
@raw_connection.retry_attempt = nil
|
|
816
|
+
result = yield connection
|
|
817
|
+
@last_used_at = RedisClient.now
|
|
818
|
+
result
|
|
721
819
|
rescue ConnectionError, ProtocolError
|
|
722
820
|
close
|
|
723
821
|
raise
|
|
@@ -731,13 +829,24 @@ class RedisClient
|
|
|
731
829
|
if @raw_connection.nil? || !@raw_connection.revalidate
|
|
732
830
|
connect
|
|
733
831
|
end
|
|
832
|
+
|
|
833
|
+
if config.idle_timeout && (@last_used_at + config.idle_timeout) < RedisClient.now
|
|
834
|
+
@middlewares.call(["PING"], config) do
|
|
835
|
+
@raw_connection.call(["PING"], nil)
|
|
836
|
+
rescue ConnectionError, ProtocolError
|
|
837
|
+
close
|
|
838
|
+
connect
|
|
839
|
+
end
|
|
840
|
+
end
|
|
841
|
+
|
|
842
|
+
@raw_connection.retry_attempt = @retry_attempt
|
|
734
843
|
@raw_connection
|
|
735
844
|
end
|
|
736
845
|
|
|
737
846
|
def connect
|
|
738
847
|
@pid = PIDCache.pid
|
|
739
848
|
|
|
740
|
-
if @raw_connection
|
|
849
|
+
if @raw_connection&.revalidate
|
|
741
850
|
@middlewares.connect(config) do
|
|
742
851
|
@raw_connection.reconnect
|
|
743
852
|
end
|
|
@@ -751,6 +860,8 @@ class RedisClient
|
|
|
751
860
|
)
|
|
752
861
|
end
|
|
753
862
|
end
|
|
863
|
+
@last_used_at = RedisClient.now
|
|
864
|
+
@raw_connection.retry_attempt = @retry_attempt
|
|
754
865
|
|
|
755
866
|
prelude = config.connection_prelude.dup
|
|
756
867
|
|
|
@@ -758,31 +869,42 @@ class RedisClient
|
|
|
758
869
|
prelude << ["CLIENT", "SETNAME", id]
|
|
759
870
|
end
|
|
760
871
|
|
|
761
|
-
# The connection prelude is deliberately not sent to Middlewares
|
|
762
872
|
if config.sentinel?
|
|
763
873
|
prelude << ["ROLE"]
|
|
764
|
-
|
|
765
|
-
|
|
874
|
+
end
|
|
875
|
+
|
|
876
|
+
unless prelude.empty?
|
|
877
|
+
results = @middlewares.call_pipelined(prelude, config) do
|
|
878
|
+
@raw_connection.call_pipelined(prelude, nil, exception: false)
|
|
766
879
|
end
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
880
|
+
|
|
881
|
+
results.each do |result|
|
|
882
|
+
# CLIENT SETINFO is unsupported on Redis < 7.2. The pipeline drained all responses before raising,
|
|
883
|
+
# so if that's the only error, the socket is healthy: keep it.
|
|
884
|
+
if result.is_a?(CommandError) && !(result.command[0] == "CLIENT" && result.command[1] == "SETINFO")
|
|
885
|
+
raise result
|
|
772
886
|
end
|
|
773
887
|
end
|
|
888
|
+
|
|
889
|
+
if config.sentinel?
|
|
890
|
+
config.check_role!(results.last.first)
|
|
891
|
+
end
|
|
774
892
|
end
|
|
775
893
|
rescue FailoverError, CannotConnectError => error
|
|
894
|
+
@raw_connection&.close
|
|
776
895
|
error._set_config(config)
|
|
777
896
|
raise error
|
|
778
897
|
rescue ConnectionError => error
|
|
898
|
+
@raw_connection&.close
|
|
779
899
|
connect_error = CannotConnectError.with_config(error.message, config)
|
|
780
900
|
connect_error.set_backtrace(error.backtrace)
|
|
781
901
|
raise connect_error
|
|
782
902
|
rescue CommandError => error
|
|
783
|
-
|
|
903
|
+
@raw_connection&.close
|
|
904
|
+
|
|
905
|
+
if error.command&.first == "HELLO" && error.message.match?(/ERR unknown command/)
|
|
784
906
|
raise UnsupportedServer,
|
|
785
|
-
"redis-client requires Redis 6+ with HELLO command available (#{config.server_url})"
|
|
907
|
+
"redis-client requires Redis 6+ with HELLO command available (#{config.server_url})", cause: error
|
|
786
908
|
else
|
|
787
909
|
raise
|
|
788
910
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: redis-client
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.31.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Jean Boussier
|
|
8
|
-
autorequire:
|
|
9
8
|
bindir: bin
|
|
10
9
|
cert_chain: []
|
|
11
|
-
date:
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
12
11
|
dependencies:
|
|
13
12
|
- !ruby/object:Gem::Dependency
|
|
14
13
|
name: connection_pool
|
|
@@ -24,7 +23,6 @@ dependencies:
|
|
|
24
23
|
- - ">="
|
|
25
24
|
- !ruby/object:Gem::Version
|
|
26
25
|
version: '0'
|
|
27
|
-
description:
|
|
28
26
|
email:
|
|
29
27
|
- jean.boussier@gmail.com
|
|
30
28
|
executables: []
|
|
@@ -32,11 +30,8 @@ extensions: []
|
|
|
32
30
|
extra_rdoc_files: []
|
|
33
31
|
files:
|
|
34
32
|
- CHANGELOG.md
|
|
35
|
-
- Gemfile
|
|
36
|
-
- Gemfile.lock
|
|
37
33
|
- LICENSE.md
|
|
38
34
|
- README.md
|
|
39
|
-
- Rakefile
|
|
40
35
|
- lib/redis-client.rb
|
|
41
36
|
- lib/redis_client.rb
|
|
42
37
|
- lib/redis_client/circuit_breaker.rb
|
|
@@ -44,6 +39,7 @@ files:
|
|
|
44
39
|
- lib/redis_client/config.rb
|
|
45
40
|
- lib/redis_client/connection_mixin.rb
|
|
46
41
|
- lib/redis_client/decorator.rb
|
|
42
|
+
- lib/redis_client/hash_ring.rb
|
|
47
43
|
- lib/redis_client/middlewares.rb
|
|
48
44
|
- lib/redis_client/pid_cache.rb
|
|
49
45
|
- lib/redis_client/pooled.rb
|
|
@@ -53,7 +49,6 @@ files:
|
|
|
53
49
|
- lib/redis_client/sentinel_config.rb
|
|
54
50
|
- lib/redis_client/url_config.rb
|
|
55
51
|
- lib/redis_client/version.rb
|
|
56
|
-
- redis-client.gemspec
|
|
57
52
|
homepage: https://github.com/redis-rb/redis-client
|
|
58
53
|
licenses:
|
|
59
54
|
- MIT
|
|
@@ -62,7 +57,6 @@ metadata:
|
|
|
62
57
|
homepage_uri: https://github.com/redis-rb/redis-client
|
|
63
58
|
source_code_uri: https://github.com/redis-rb/redis-client
|
|
64
59
|
changelog_uri: https://github.com/redis-rb/redis-client/blob/master/CHANGELOG.md
|
|
65
|
-
post_install_message:
|
|
66
60
|
rdoc_options: []
|
|
67
61
|
require_paths:
|
|
68
62
|
- lib
|
|
@@ -77,8 +71,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
77
71
|
- !ruby/object:Gem::Version
|
|
78
72
|
version: '0'
|
|
79
73
|
requirements: []
|
|
80
|
-
rubygems_version:
|
|
81
|
-
signing_key:
|
|
74
|
+
rubygems_version: 4.0.20
|
|
82
75
|
specification_version: 4
|
|
83
76
|
summary: Simple low-level client for Redis 6+
|
|
84
77
|
test_files: []
|