Skip to content

Commit 1b1e84b

Browse files
authored
Adds new configuration option to specify reconnecting using the original configuration options when refreshing nodes (#290)
2 parents 22099af + 4f12dd7 commit 1b1e84b

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ gem 'redis-cluster-client'
2828
| `:fixed_hostname` | String | `nil` | required if client should connect to single endpoint with SSL |
2929
| `:slow_command_timeout` | Integer | `-1` | timeout used for "slow" queries that fetch metdata e.g. CLUSTER NODES, COMMAND |
3030
| `:concurrency` | Hash | `{ model: :on_demand, size: 5}` | concurrency settings, `:on_demand`, `:pooled` and `:none` are valid models, size is a max number of workers, `:none` model is no concurrency, Please choose the one suited your environment if needed. |
31+
| `:connect_with_original_config` | Boolean | `false` | `true` if client should retry the connection using the original endpoint that was passed in |
3132

3233
Also, [the other generic options](https://github.com/redis-rb/redis-client#configuration) can be passed.
3334
But `:url`, `:host`, `:port` and `:path` are ignored because they conflict with the `:nodes` option.
@@ -106,6 +107,11 @@ RedisClient.cluster(concurrency: { model: :none }).new_client
106107
# Please choose the one suited your workloads.
107108
```
108109

110+
```ruby
111+
# To reconnect using the original configuration options on error. This can be useful when using a DNS endpoint and the underlying host IPs are all updated
112+
RedisClient.cluster(connect_with_original_config: true).new_client
113+
```
114+
109115
## Interfaces
110116
The following methods are able to be used like `redis-client`.
111117
* `#call`

lib/redis_client/cluster/router.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ class Router
1818

1919
def initialize(config, concurrent_worker, pool: nil, **kwargs)
2020
@config = config.dup
21+
@original_config = config.dup if config.connect_with_original_config
22+
@connect_with_original_config = config.connect_with_original_config
2123
@concurrent_worker = concurrent_worker
2224
@pool = pool
2325
@client_kwargs = kwargs
@@ -329,6 +331,7 @@ def update_cluster_info!
329331
# ignore
330332
end
331333

334+
@config = @original_config.dup if @connect_with_original_config
332335
@node = fetch_cluster_info(@config, @concurrent_worker, pool: @pool, **@client_kwargs)
333336
end
334337
end

lib/redis_client/cluster_config.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,15 @@ class ClusterConfig
2323

2424
InvalidClientConfigError = Class.new(::RedisClient::Error)
2525

26-
attr_reader :command_builder, :client_config, :replica_affinity, :slow_command_timeout
26+
attr_reader :command_builder, :client_config, :replica_affinity, :slow_command_timeout, :connect_with_original_config
2727

28-
def initialize(
28+
def initialize( # rubocop:disable Metrics/AbcSize
2929
nodes: DEFAULT_NODES,
3030
replica: false,
3131
replica_affinity: :random,
3232
fixed_hostname: '',
3333
concurrency: nil,
34+
connect_with_original_config: false,
3435
client_implementation: ::RedisClient::Cluster, # for redis gem
3536
slow_command_timeout: SLOW_COMMAND_TIMEOUT,
3637
**client_config
@@ -44,6 +45,7 @@ def initialize(
4445
@command_builder = client_config.fetch(:command_builder, ::RedisClient::CommandBuilder)
4546
@client_config = merge_generic_config(client_config, @node_configs)
4647
@concurrency = merge_concurrency_option(concurrency)
48+
@connect_with_original_config = connect_with_original_config
4749
@client_implementation = client_implementation
4850
@slow_command_timeout = slow_command_timeout
4951
@mutex = Mutex.new
@@ -56,6 +58,7 @@ def dup
5658
replica_affinity: @replica_affinity,
5759
fixed_hostname: @fixed_hostname,
5860
concurrency: @concurrency,
61+
connect_with_original_config: @connect_with_original_config,
5962
client_implementation: @client_implementation,
6063
slow_command_timeout: @slow_command_timeout,
6164
**@client_config

0 commit comments

Comments
 (0)