Skip to content

Commit 1512a0d

Browse files
authored
fix: make no-concurrency mode default (#411)
1 parent b293513 commit 1512a0d

File tree

6 files changed

+31
-13
lines changed

6 files changed

+31
-13
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ gem 'redis-cluster-client'
2828
| `:replica_affinity` | Symbol or String | `:random` | scale reading strategy, `:random`, `random_with_primary` or `:latency` are valid |
2929
| `:fixed_hostname` | String | `nil` | required if client should connect to single endpoint with SSL |
3030
| `:slow_command_timeout` | Integer | `-1` | timeout used for "slow" queries that fetch metdata e.g. CLUSTER NODES, COMMAND |
31-
| `: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+
| `:concurrency` | Hash | `{ model: :none }` | 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. |
3232
| `:connect_with_original_config` | Boolean | `false` | `true` if client should retry the connection using the original endpoint that was passed in |
3333
| `:max_startup_sample` | Integer | `3` | maximum number of nodes to fetch `CLUSTER NODES` information for startup |
3434

lib/redis_client/cluster/concurrent_worker.rb

+4-6
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,12 @@ def inspect
7171

7272
module_function
7373

74-
def create(model: :on_demand, size: 5)
75-
size = size.positive? ? size : 5
76-
74+
def create(model: :none, size: 5)
7775
case model
78-
when :on_demand, nil then ::RedisClient::Cluster::ConcurrentWorker::OnDemand.new(size: size)
79-
when :pooled then ::RedisClient::Cluster::ConcurrentWorker::Pooled.new(size: size)
8076
when :none then ::RedisClient::Cluster::ConcurrentWorker::None.new
81-
else raise ArgumentError, "Unknown model: #{model}"
77+
when :on_demand then ::RedisClient::Cluster::ConcurrentWorker::OnDemand.new(size: size)
78+
when :pooled then ::RedisClient::Cluster::ConcurrentWorker::Pooled.new(size: size)
79+
else raise ArgumentError, "unknown model: #{model}"
8280
end
8381
end
8482
end

lib/redis_client/cluster/concurrent_worker/on_demand.rb

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ class Cluster
55
module ConcurrentWorker
66
class OnDemand
77
def initialize(size:)
8+
raise ArgumentError, "size must be positive: #{size}" unless size.positive?
9+
810
@q = SizedQueue.new(size)
911
end
1012

lib/redis_client/cluster/concurrent_worker/pooled.rb

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ module ConcurrentWorker
1111
# So it consumes memory 1 MB multiplied a number of workers.
1212
class Pooled
1313
def initialize(size:)
14+
raise ArgumentError, "size must be positive: #{size}" unless size.positive?
15+
1416
@size = size
1517
setup
1618
end

lib/redis_client/cluster_config.rb

+10-6
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class ClusterConfig
1919
VALID_NODES_KEYS = %i[ssl username password host port db].freeze
2020
MERGE_CONFIG_KEYS = %i[ssl username password].freeze
2121
IGNORE_GENERIC_CONFIG_KEYS = %i[url host port path].freeze
22-
MAX_WORKERS = Integer(ENV.fetch('REDIS_CLIENT_MAX_THREADS', 5))
22+
MAX_WORKERS = Integer(ENV.fetch('REDIS_CLIENT_MAX_THREADS', -1)) # for backward compatibility
2323
# It's used with slow queries of fetching meta data like CLUSTER NODES, COMMAND and so on.
2424
SLOW_COMMAND_TIMEOUT = Float(ENV.fetch('REDIS_CLIENT_SLOW_COMMAND_TIMEOUT', -1))
2525
# It affects to strike a balance between load and stability in initialization or changed states.
@@ -110,12 +110,16 @@ def server_url
110110
private
111111

112112
def merge_concurrency_option(option)
113-
case option
114-
when Hash
115-
option = option.transform_keys(&:to_sym)
116-
{ size: MAX_WORKERS }.merge(option)
117-
else { size: MAX_WORKERS }
113+
opts = {}
114+
115+
if MAX_WORKERS.positive?
116+
opts[:model] = :on_demand
117+
opts[:size] = MAX_WORKERS
118118
end
119+
120+
opts.merge!(option.transform_keys(&:to_sym)) if option.is_a?(Hash)
121+
opts[:model] = :none if opts.empty?
122+
opts.freeze
119123
end
120124

121125
def build_node_configs(addrs)

test/redis_client/test_cluster_config.rb

+12
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,18 @@ def test_command_builder
103103
assert_equal(::RedisClient::CommandBuilder, ::RedisClient::ClusterConfig.new.command_builder)
104104
end
105105

106+
def test_concurrency
107+
[
108+
{ value: nil, want: { model: :none } },
109+
{ value: { model: :none }, want: { model: :none } },
110+
{ value: { model: :on_demand, size: 3 }, want: { model: :on_demand, size: 3 } },
111+
{ value: { model: :pooled, size: 6 }, want: { model: :pooled, size: 6 } }
112+
].each do |c|
113+
cfg = ::RedisClient::ClusterConfig.new(concurrency: c[:value])
114+
assert_equal(c[:want], cfg.instance_variable_get(:@concurrency))
115+
end
116+
end
117+
106118
def test_build_node_configs
107119
config = ::RedisClient::ClusterConfig.new
108120
[

0 commit comments

Comments
 (0)