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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 796fe04bd550f92d5bf2656a62de54caf1f640b45ab5d5f51fed61d0b61ee883
4
- data.tar.gz: 4b34ce4e0ed5a2d4816863970789e7c23ce53fe8a561718b78b79432d09e3fe8
3
+ metadata.gz: a978872b66cb08635b537696d919aa49352de251b9525bc4fa17a25b84801bb3
4
+ data.tar.gz: 2e45ea495c8537bc5a84317d371c4dad00aecf625352eb3893240d8905f20792
5
5
  SHA512:
6
- metadata.gz: d9dfb29603171606e71daaa43474812790988876720dd254a0069fc429ba035ede74b1b19d243967b9adb61c0d48ab4aa2931ef188cd2bb17d820b480967c897
7
- data.tar.gz: e6ff3a4543991e139a26f8fa4bdb714aafe83678490f9890ca5fd0ec1a95bdddd0c8fec57dcb4ff88fb28a19d577a556172cb39e4bd4f290d41c6871de43345b
6
+ metadata.gz: 33fc3e223b160391b28528b8555494e64502911826d454c296d3e22293785ce0aa5b7ebe8d52fb1331ed3c5e061788b23f359ddc902fae7c433f83e6988073b1
7
+ data.tar.gz: 9427851d1969f93a46877dc2c3e8fdc024555ecc60adae8ffdabe82879fe1eaa21b96af5cbe98ea6bd366b40dd0ea95fcf20940497ba5d749c13d082ec291972
data/CHANGELOG.md CHANGED
@@ -1,5 +1,103 @@
1
1
  # Unreleased
2
2
 
3
+ # 0.31.0
4
+
5
+ - Improved the Ruby driver buffering logic, should trigger GC less, particularly for large response and when using SSL.
6
+ - `ssl_params` can now be a proc, for environment where SSL certificates frequently change.
7
+ - Raise `RedisClient::ProtocolError` instead of `NoMethodError` when the RESP3 parser hits an unexpected end of stream.
8
+
9
+ # 0.30.1
10
+
11
+ - hiredis-client: Use `pkg-config` to locate OpenSSL. Should fix compilation issue on precompiled rubies such as the ones from `mise`.
12
+
13
+ # 0.30.0
14
+
15
+ - hiredis-client: Now support configuring SSL `verify_mode`.
16
+
17
+ # 0.29.0
18
+
19
+ - Fix connecting to Redis 7.1 and older with `driver_info:`set.
20
+ - Added `RedisClient::Error#next_error` for an easier way to check all errors produced by a pipeline.
21
+
22
+ # 0.28.0
23
+
24
+ - Added `RedisClient::HashRing` for horizontal sharing (compatible with `Redis::Distributed` from `redis-rb`).
25
+
26
+ # 0.27.0
27
+
28
+ - Added `idle_timeout` to revalidate connections that haven't been successfuly used in a long time. Defaults to 30 seconds.
29
+ - Added `driver_info` configuration, to issue `CLIENT SETINFO` during connection prelude.
30
+
31
+ # 0.26.4
32
+
33
+ - Further improve `rediss://` URLs used with Redis sentinel. Now avoid override explictly set `ssl:` parameter.
34
+ - Fix compatibility with `redis-rb` in sentinel mode.
35
+
36
+ # 0.26.3
37
+
38
+ - Fix `rediss://` (ssl) URLs used with Redis sentinel.
39
+ - Handle Ruby 4.0 connection timeout raising an `IO::Timeout` instead of `Errno::ETIMEDOUT`.
40
+ - Entirely close the connection on authentication failures.
41
+
42
+ # 0.26.2
43
+
44
+ - Fix compatibility with `connection_pool` version 3+.
45
+
46
+ # 0.26.1
47
+
48
+ - Fix a few corner cases where `RedisClient::Error#final?` was innacurate.
49
+ - hiredis-client: Properly reconnect to the new leader after a sentinel failover.
50
+
51
+ # 0.26.0
52
+
53
+ - Add `RedisClient::Error#final?` and `#retriable?` to allow middleware to filter out non-final errors.
54
+ - Fix precedence of `db: nil` initialization parameter.
55
+
56
+ ```ruby
57
+ Redis.new(url: "redis://localhost:6379/3", db: nil).db
58
+ ```
59
+
60
+ Before: `0`
61
+ After: `3`
62
+
63
+ # 0.25.3
64
+
65
+ - Fix `hiredis-client` compilation with `clang 21`.
66
+
67
+ # 0.25.2
68
+
69
+ - Fix circuit breakers to respect the `error_threshold_timeout` config is provided.
70
+ - Fix circuit breakers to clear errors when closing back.
71
+
72
+ # 0.25.1
73
+
74
+ - Fix Ruby driver TCP keep alive TTL. It was intended to be 120 seconds but was mistakenly set to 15 seconds.
75
+
76
+ # 0.25.0
77
+
78
+ - Fix `hiredis-client` compilation with GCC 15.
79
+ - Fix `hiredis-client` from a work directory with spaces.
80
+ - Add `CommandError#code`.
81
+ - Add `RedisClient::NoScriptError` for `EVALSHA`.
82
+
83
+ # 0.24.0
84
+
85
+ - Allow `sentinel_password` to be provided as a `Proc`.
86
+ - Ensure `Config#inspect` and `Config#to_s` do not display stored passwords.
87
+
88
+ # 0.23.2
89
+
90
+ - Fix retry logic not to attempt to retry on an open circuit breaker. Fix #227.
91
+
92
+ # 0.23.1
93
+
94
+ - Fix a potential crash in `hiredis-client` when using subcriptions (`next_event`). See #221.
95
+
96
+ # 0.23.0
97
+
98
+ - Allow `password` to be a callable. Makes it easy to implement short lived password authentication strategies.
99
+ - Fix a thread safety issue in `hiredis-client` when using the `pubsub` client concurrently.
100
+
3
101
  # 0.22.2
4
102
 
5
103
  - Fix the sentinel client to properly extend timeout for blocking commands.
@@ -62,6 +160,7 @@
62
160
  - Discard sockets rather than explictly close them when a fork is detected. #126.
63
161
  - Allow to configure sentinel client via url. #117.
64
162
  - Fix sentinel to preverse the auth/password when refreshing the sentinel list. #107.
163
+ - Added `RedisClient#measure_round_trip_delay` method. #113.
65
164
 
66
165
  # 0.14.1
67
166
 
@@ -166,7 +265,7 @@
166
265
  - Added `_v` versions of `call` methods to make it easier to pass commands as arrays without splating.
167
266
  - Fix calling `blocking_call` with a block in a pipeline.
168
267
  - `blocking_call` now raise `ReadTimeoutError` if the command didn't complete in time.
169
- - Fix `blocking_call` to not respect `retry_attempts` on timeout.
268
+ - Fix `blocking_call` to not respect `reconnect_attempts` on timeout.
170
269
  - Stop parsing RESP3 sets as Ruby Set instances.
171
270
  - Fix `SystemStackError` when parsing very large hashes. Fix: #30
172
271
  - `hiredis` now more properly release the GVL when doing IOs.
data/README.md CHANGED
@@ -1,9 +1,10 @@
1
1
  # RedisClient
2
2
 
3
- `redis-client` is a simple, low-level, client for Redis 6+.
3
+ `redis-client` is a simple, low-level, client for [Redis](https://redis.io/) 6+, [Valkey](https://valkey.io/) 7+, [KeyDB](https://docs.keydb.dev/),
4
+ and several other databases that implement the same `RESP3` protocol.
4
5
 
5
6
  Contrary to the `redis` gem, `redis-client` doesn't try to map all Redis commands to Ruby constructs,
6
- it merely is a thin wrapper on top of the RESP3 protocol.
7
+ it merely is a thin wrapper on top of the `RESP3` protocol.
7
8
 
8
9
  ## Installation
9
10
 
@@ -70,18 +71,19 @@ redis.call("GET", "mykey")
70
71
  - `port`: The server port. Defaults to `6379`.
71
72
  - `path`: The path to a UNIX socket, if set `url`, `host` and `port` are ignored.
72
73
  - `ssl`: Whether to connect using SSL or not.
73
- - `ssl_params`: A configuration Hash passed to [`OpenSSL::SSL::SSLContext#set_params`](https://www.rubydoc.info/stdlib/openssl/OpenSSL%2FSSL%2FSSLContext:set_params), notable options include:
74
+ - `ssl_params`: A configuration Hash passed to [`OpenSSL::SSL::SSLContext#set_params`](https://ruby.github.io/openssl/OpenSSL/SSL/SSLContext.html#method-i-set_params), notable options include:
74
75
  - `cert`: The path to the client certificate (e.g. `client.crt`).
75
76
  - `key`: The path to the client key (e.g. `client.key`).
76
77
  - `ca_file`: The certificate authority to use, useful for self-signed certificates (e.g. `ca.crt`),
77
78
  - `db`: The database to select after connecting, defaults to `0`.
78
79
  - `id` ID for the client connection, assigns name to current connection by sending `CLIENT SETNAME`.
79
80
  - `username` Username to authenticate against server, defaults to `"default"`.
80
- - `password` Password to authenticate against server.
81
+ - `password` Password to authenticate against server. Can either be a String or a callable that recieve `username` as argument and return a passowrd as a String.
81
82
  - `timeout`: The general timeout in seconds, default to `1.0`.
82
83
  - `connect_timeout`: The connection timeout, takes precedence over the general timeout when connecting to the server.
83
84
  - `read_timeout`: The read timeout, takes precedence over the general timeout when reading responses from the server.
84
85
  - `write_timeout`: The write timeout, takes precedence over the general timeout when sending commands to the server.
86
+ - `idle_timeout`: Amount of time after which an idle connection has to be revalidated with a PING command. Defaults to `30` seconds.
85
87
  - `reconnect_attempts`: Specify how many times the client should retry to send queries. Defaults to `0`. Makes sure to read the [reconnection section](#reconnection) before enabling it.
86
88
  - `circuit_breaker`: A Hash with circuit breaker configuration. Defaults to `nil`. See the [circuit breaker section](#circuit-breaker) for details.
87
89
  - `protocol:` The version of the RESP protocol to use. Default to `3`.
@@ -89,7 +91,7 @@ redis.call("GET", "mykey")
89
91
 
90
92
  ### Sentinel support
91
93
 
92
- The client is able to perform automatic failover by using [Redis Sentinel](https://redis.io/docs/manual/sentinel/).
94
+ The client is able to perform automatic failover by using [Redis Sentinel](https://redis.io/docs/latest/operate/oss_and_stack/management/sentinel/).
93
95
 
94
96
  To connect using Sentinel, use:
95
97
 
@@ -142,6 +144,7 @@ redis_config = RedisClient.sentinel(name: 'mymaster', sentinel_username: 'appuse
142
144
  If you specify a username and/or password at the top level for your main Redis instance, Sentinel *will not* using thouse credentials
143
145
 
144
146
  ```ruby
147
+
145
148
  # Use 'mysecret' to authenticate against the mymaster instance, but skip authentication for the sentinels:
146
149
  SENTINELS = [{ host: '127.0.0.1', port: 26380 },
147
150
  { host: '127.0.0.1', port: 26381 }]
@@ -165,6 +168,30 @@ Also the `name`, `password`, `username` and `db` for Redis instance can be passe
165
168
  redis_config = RedisClient.sentinel(url: "redis://appuser:mysecret@mymaster/10", sentinels: SENTINELS, role: :master)
166
169
  ```
167
170
 
171
+ ### Consistent Hashing
172
+
173
+ To horizontally shard keys across multiple servers without relying on clustering, a `RedisClient::HashRing` class is provided:
174
+
175
+ ```ruby
176
+ ring = RedisClient.ring(
177
+ RedisClient.config(host: "10.0.1.1", port: 6380).new_pool(timeout: 0.5, size: 3),
178
+ RedisClient.config(host: "10.0.1.2", port: 6380).new_pool(timeout: 0.5, size: 3),
179
+ RedisClient.config(host: "10.0.1.3", port: 6380).new_pool(timeout: 0.5, size: 3),
180
+ )
181
+
182
+ ring.node_for("cache_key").call("GET", "cache_key") # => "value"
183
+
184
+ ring.nodes_for("key1", "key2", "key3").each do |node, keys|
185
+ node.call("DEL", *keys)
186
+ end
187
+
188
+ ring.nodes.each do |node|
189
+ node.close
190
+ end
191
+ ```
192
+
193
+ Note that regular clients do respond to `node_for`, `nodes_for` and `nodes`, so that code that support `RedisClient.ring` is also usable with a single server.
194
+
168
195
  ### Type support
169
196
 
170
197
  Only a select few Ruby types are supported as arguments beside strings.
@@ -334,7 +361,11 @@ end
334
361
 
335
362
  #### Exception management
336
363
 
337
- The `exception` flag in the `#pipelined` method of `RedisClient` is a feature that modifies the pipeline execution
364
+ By default, when a pipeline produce multiple command errors, only the first encountered error is raised.
365
+
366
+ However, the raised exception have a `.next_error` method you can use like a linked list to check all encountered errors.
367
+
368
+ Alternatively, the `exception` flag in the `#pipelined` method of `RedisClient` is a feature that modifies the pipeline execution
338
369
  behavior. When set to `false`, it doesn't raise an exception when a command error occurs. Instead, it allows the
339
370
  pipeline to execute all commands, and any failed command will be available in the returned array. (Defaults to `true`)
340
371
 
@@ -458,6 +489,29 @@ RedisClient.register(MyGlobalRedisInstrumentation)
458
489
 
459
490
  redis_config = RedisClient.config(custom: { tags: { "environment": Rails.env }})
460
491
  ```
492
+
493
+ ### Instrumenting Errors
494
+
495
+ It is important to note that when `reconnect_attempts` is enabled, all network errors are reported to the middlewares,
496
+ even the ones that will be retried.
497
+
498
+ In many cases you may want to ignore retriable errors, or report them differently:
499
+
500
+ ```ruby
501
+ module MyGlobalRedisInstrumentation
502
+ def call(command, redis_config)
503
+ super
504
+ rescue RedisClient::Error => error
505
+ if error.final?
506
+ # Error won't be retried.
507
+ else
508
+ # Error will be retried.
509
+ end
510
+ raise
511
+ end
512
+ end
513
+ ```
514
+
461
515
  ### Timeouts
462
516
 
463
517
  The client allows you to configure connect, read, and write timeouts.
@@ -63,8 +63,10 @@ class RedisClient
63
63
  private
64
64
 
65
65
  def refresh_state
66
- now = Process.clock_gettime(Process::CLOCK_MONOTONIC)
66
+ now = RedisClient.now
67
67
  @lock.synchronize do
68
+ return unless @state == :open
69
+
68
70
  if @errors.last < (now - @error_timeout)
69
71
  if @success_threshold > 0
70
72
  @state = :half_open
@@ -78,8 +80,8 @@ class RedisClient
78
80
  end
79
81
 
80
82
  def record_error
81
- now = Process.clock_gettime(Process::CLOCK_MONOTONIC)
82
- expiry = now - @error_timeout
83
+ now = RedisClient.now
84
+ expiry = now - @error_threshold_timeout
83
85
  @lock.synchronize do
84
86
  if @state == :closed
85
87
  @errors.reject! { |t| t < expiry }
@@ -100,6 +102,7 @@ class RedisClient
100
102
 
101
103
  @successes += 1
102
104
  if @successes >= @success_threshold
105
+ @errors.clear
103
106
  @state = :closed
104
107
  end
105
108
  end
@@ -10,11 +10,12 @@ class RedisClient
10
10
  DEFAULT_PORT = 6379
11
11
  DEFAULT_USERNAME = "default"
12
12
  DEFAULT_DB = 0
13
+ DEFAULT_IDLE_TIMEOUT = 30.0
13
14
 
14
15
  module Common
15
- attr_reader :db, :password, :id, :ssl, :ssl_params, :command_builder, :inherit_socket,
16
- :connect_timeout, :read_timeout, :write_timeout, :driver, :connection_prelude, :protocol,
17
- :middlewares_stack, :custom, :circuit_breaker
16
+ attr_reader :db, :id, :ssl, :ssl_params, :command_builder, :inherit_socket,
17
+ :connect_timeout, :read_timeout, :write_timeout, :driver, :protocol,
18
+ :middlewares_stack, :custom, :circuit_breaker, :driver_info, :idle_timeout
18
19
 
19
20
  alias_method :ssl?, :ssl
20
21
 
@@ -27,6 +28,7 @@ class RedisClient
27
28
  read_timeout: timeout,
28
29
  write_timeout: timeout,
29
30
  connect_timeout: timeout,
31
+ idle_timeout: DEFAULT_IDLE_TIMEOUT,
30
32
  ssl: nil,
31
33
  custom: {},
32
34
  ssl_params: nil,
@@ -37,10 +39,11 @@ class RedisClient
37
39
  inherit_socket: false,
38
40
  reconnect_attempts: false,
39
41
  middlewares: false,
40
- circuit_breaker: nil
42
+ circuit_breaker: nil,
43
+ driver_info: nil
41
44
  )
42
45
  @username = username
43
- @password = password
46
+ @password = password && !password.respond_to?(:call) ? ->(_) { password } : password
44
47
  @db = begin
45
48
  Integer(db || DEFAULT_DB)
46
49
  rescue ArgumentError
@@ -54,6 +57,7 @@ class RedisClient
54
57
  @connect_timeout = connect_timeout
55
58
  @read_timeout = read_timeout
56
59
  @write_timeout = write_timeout
60
+ @idle_timeout = idle_timeout
57
61
 
58
62
  @driver = driver ? RedisClient.driver(driver) : RedisClient.default_driver
59
63
 
@@ -70,7 +74,6 @@ class RedisClient
70
74
 
71
75
  reconnect_attempts = Array.new(reconnect_attempts, 0).freeze if reconnect_attempts.is_a?(Integer)
72
76
  @reconnect_attempts = reconnect_attempts
73
- @connection_prelude = build_connection_prelude
74
77
 
75
78
  circuit_breaker = CircuitBreaker.new(**circuit_breaker) if circuit_breaker.is_a?(Hash)
76
79
  if @circuit_breaker = circuit_breaker
@@ -85,12 +88,73 @@ class RedisClient
85
88
  end
86
89
  end
87
90
  @middlewares_stack = middlewares_stack
91
+
92
+ @driver_info = driver_info
93
+ end
94
+
95
+ def connection_prelude
96
+ prelude = []
97
+ pass = password
98
+ if protocol == 3
99
+ prelude << if pass
100
+ ["HELLO", "3", "AUTH", username, pass]
101
+ else
102
+ ["HELLO", "3"]
103
+ end
104
+ elsif pass
105
+ prelude << if @username && !@username.empty?
106
+ ["AUTH", @username, pass]
107
+ else
108
+ ["AUTH", pass]
109
+ end
110
+ end
111
+
112
+ if @db && @db != 0
113
+ prelude << ["SELECT", @db.to_s]
114
+ end
115
+
116
+ # Add CLIENT SETINFO commands for lib-name and lib-ver
117
+ # These commands are supported in Redis 7.2+
118
+ if @driver_info
119
+ prelude << ["CLIENT", "SETINFO", "LIB-NAME", build_lib_name]
120
+ prelude << ["CLIENT", "SETINFO", "LIB-VER", RedisClient::VERSION]
121
+ end
122
+
123
+ # Deep freeze all the strings and commands
124
+ prelude.map! do |commands|
125
+ commands = commands.map { |str| str.frozen? ? str : str.dup.freeze }
126
+ commands.freeze
127
+ end
128
+ prelude.freeze
129
+ end
130
+
131
+ def password
132
+ @password&.call(username)
88
133
  end
89
134
 
90
135
  def username
91
136
  @username || DEFAULT_USERNAME
92
137
  end
93
138
 
139
+ # Build the library name for CLIENT SETINFO LIB-NAME.
140
+ #
141
+ # @param driver_info [String, Array<String>, nil] Upstream driver info
142
+ # @return [String] Library name with optional upstream driver info
143
+ # @raise [ArgumentError] if driver_info is not a String or Array
144
+ def build_lib_name
145
+ return "redis-client" if @driver_info.nil?
146
+
147
+ info = case @driver_info
148
+ when String then @driver_info
149
+ when Array then @driver_info.join(";")
150
+ else raise ArgumentError, "driver_info must be a String or Array of Strings"
151
+ end
152
+
153
+ return "redis-client" if info.empty?
154
+
155
+ "redis-client(#{info})"
156
+ end
157
+
94
158
  def resolved?
95
159
  true
96
160
  end
@@ -108,6 +172,10 @@ class RedisClient
108
172
  @client_implementation.new(self, **kwargs)
109
173
  end
110
174
 
175
+ def retriable?(attempt)
176
+ @reconnect_attempts && @reconnect_attempts[attempt]
177
+ end
178
+
111
179
  def retry_connecting?(attempt, _error)
112
180
  if @reconnect_attempts
113
181
  if (pause = @reconnect_attempts[attempt])
@@ -122,7 +190,7 @@ class RedisClient
122
190
 
123
191
  def ssl_context
124
192
  if ssl
125
- @ssl_context ||= @driver.ssl_context(@ssl_params || {})
193
+ @driver.ssl_context(@ssl_params || {})
126
194
  end
127
195
  end
128
196
 
@@ -146,41 +214,11 @@ class RedisClient
146
214
  end
147
215
  url
148
216
  end
149
-
150
- private
151
-
152
- def build_connection_prelude
153
- prelude = []
154
- if protocol == 3
155
- prelude << if @password
156
- ["HELLO", "3", "AUTH", @username || DEFAULT_USERNAME, @password]
157
- else
158
- ["HELLO", "3"]
159
- end
160
- elsif @password
161
- prelude << if @username && !@username.empty?
162
- ["AUTH", @username, @password]
163
- else
164
- ["AUTH", @password]
165
- end
166
- end
167
-
168
- if @db && @db != 0
169
- prelude << ["SELECT", @db.to_s]
170
- end
171
-
172
- # Deep freeze all the strings and commands
173
- prelude.map! do |commands|
174
- commands = commands.map { |str| str.frozen? ? str : str.dup.freeze }
175
- commands.freeze
176
- end
177
- prelude.freeze
178
- end
179
217
  end
180
218
 
181
219
  include Common
182
220
 
183
- attr_reader :host, :port, :path
221
+ attr_reader :host, :port, :path, :server_key
184
222
 
185
223
  def initialize(
186
224
  url: nil,
@@ -189,14 +227,15 @@ class RedisClient
189
227
  path: nil,
190
228
  username: nil,
191
229
  password: nil,
230
+ db: nil,
192
231
  **kwargs
193
232
  )
194
233
  if url
195
234
  url_config = URLConfig.new(url)
196
235
  kwargs = {
197
236
  ssl: url_config.ssl?,
198
- db: url_config.db,
199
237
  }.compact.merge(kwargs)
238
+ db ||= url_config.db
200
239
  host ||= url_config.host
201
240
  port ||= url_config.port
202
241
  path ||= url_config.path
@@ -204,7 +243,7 @@ class RedisClient
204
243
  password ||= url_config.password
205
244
  end
206
245
 
207
- super(username: username, password: password, **kwargs)
246
+ super(username: username, password: password, db: db, **kwargs)
208
247
 
209
248
  if @path = path
210
249
  @host = nil
@@ -213,6 +252,8 @@ class RedisClient
213
252
  @host = host || DEFAULT_HOST
214
253
  @port = Integer(port || DEFAULT_PORT)
215
254
  end
255
+
256
+ @server_key = [@path, @host, @port].freeze
216
257
  end
217
258
  end
218
259
  end
@@ -2,8 +2,14 @@
2
2
 
3
3
  class RedisClient
4
4
  module ConnectionMixin
5
- def initialize
5
+ attr_accessor :retry_attempt
6
+ attr_reader :config
7
+
8
+ def initialize(config)
6
9
  @pending_reads = 0
10
+ @retry_attempt = nil
11
+ @config = config
12
+ @server_key = nil
7
13
  end
8
14
 
9
15
  def reconnect
@@ -17,7 +23,7 @@ class RedisClient
17
23
  end
18
24
 
19
25
  def revalidate
20
- if @pending_reads > 0
26
+ if @pending_reads > 0 || @server_key != @config.server_key
21
27
  close
22
28
  false
23
29
  else
@@ -33,6 +39,7 @@ class RedisClient
33
39
  if result.is_a?(Error)
34
40
  result._set_command(command)
35
41
  result._set_config(config)
42
+ result._set_retry_attempt(@retry_attempt)
36
43
  raise result
37
44
  else
38
45
  result
@@ -40,7 +47,7 @@ class RedisClient
40
47
  end
41
48
 
42
49
  def call_pipelined(commands, timeouts, exception: true)
43
- first_exception = nil
50
+ first_error = last_error = nil
44
51
 
45
52
  size = commands.size
46
53
  results = Array.new(commands.size)
@@ -61,14 +68,18 @@ class RedisClient
61
68
  elsif result.is_a?(Error)
62
69
  result._set_command(commands[index])
63
70
  result._set_config(config)
64
- first_exception ||= result
71
+ result._set_retry_attempt(@retry_attempt)
72
+
73
+ last_error&._set_next_error(result)
74
+ first_error ||= result
75
+ last_error = result
65
76
  end
66
77
 
67
78
  results[index] = result
68
79
  end
69
80
 
70
- if first_exception && exception
71
- raise first_exception
81
+ if first_error && exception
82
+ raise first_error
72
83
  else
73
84
  results
74
85
  end
@@ -82,5 +93,17 @@ class RedisClient
82
93
  # to account for the network delay.
83
94
  timeout + config.read_timeout
84
95
  end
96
+
97
+ def protocol_error(message)
98
+ error = ProtocolError.with_config(message, config)
99
+ error._set_retry_attempt(@retry_attempt)
100
+ error
101
+ end
102
+
103
+ def connection_error(message)
104
+ error = ConnectionError.with_config(message, config)
105
+ error._set_retry_attempt(@retry_attempt)
106
+ error
107
+ end
85
108
  end
86
109
  end
@@ -0,0 +1,87 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'zlib'
4
+
5
+ class RedisClient
6
+ class HashRing
7
+ POINTS_PER_SERVER = 160
8
+
9
+ class << self
10
+ attr_writer :digest
11
+
12
+ def digest
13
+ @digest ||= begin
14
+ require 'digest/md5'
15
+ Digest::MD5
16
+ end
17
+ end
18
+ end
19
+
20
+ attr_reader :nodes
21
+
22
+ def initialize(nodes = [], replicas: POINTS_PER_SERVER, digest: self.class.digest)
23
+ @replicas = replicas
24
+ @ring = {}
25
+ @digest = digest
26
+ ids = {}
27
+ @nodes = nodes.dup.freeze
28
+ nodes.each do |node|
29
+ id = node.id || node.config.server_url
30
+ if ids[id]
31
+ raise ArgumentError, "duplicate node id: #{id.inspect}"
32
+ end
33
+
34
+ ids[id] = true
35
+
36
+ replicas.times do |i|
37
+ @ring[server_hash_for("#{id}:#{i}".freeze)] = node
38
+ end
39
+ end
40
+ @sorted_keys = @ring.keys
41
+ @sorted_keys.sort!
42
+ end
43
+
44
+ # get the node in the hash ring for this key
45
+ def node_for(key)
46
+ hash = hash_for(key)
47
+ idx = binary_search(@sorted_keys, hash)
48
+ @ring[@sorted_keys[idx]]
49
+ end
50
+
51
+ def nodes_for(*keys)
52
+ keys.flatten!
53
+ mapping = {}
54
+ keys.each do |key|
55
+ (mapping[node_for(key)] ||= []) << key
56
+ end
57
+ mapping
58
+ end
59
+
60
+ private
61
+
62
+ def hash_for(key)
63
+ Zlib.crc32(key)
64
+ end
65
+
66
+ def server_hash_for(key)
67
+ @digest.digest(key).unpack1("L>")
68
+ end
69
+
70
+ # Find the closest index in HashRing with value <= the given value
71
+ def binary_search(ary, value)
72
+ upper = ary.size
73
+ lower = 0
74
+
75
+ while lower < upper
76
+ mid = (lower + upper) / 2
77
+ if ary[mid] > value
78
+ upper = mid
79
+ else
80
+ lower = mid + 1
81
+ end
82
+ end
83
+
84
+ upper - 1
85
+ end
86
+ end
87
+ end
@@ -23,7 +23,7 @@ class RedisClient
23
23
  end
24
24
 
25
25
  def with(options = EMPTY_HASH)
26
- pool.with(options) do |client|
26
+ pool.with(**options) do |client|
27
27
  client.connect_timeout = connect_timeout
28
28
  client.read_timeout = read_timeout
29
29
  client.write_timeout = write_timeout
@@ -76,7 +76,7 @@ class RedisClient
76
76
  private
77
77
 
78
78
  def pool
79
- @pool ||= @mutex.synchronize { new_pool }
79
+ @pool || @mutex.synchronize { @pool ||= new_pool }
80
80
  end
81
81
 
82
82
  def new_pool