@@ -13,6 +13,7 @@ class Node
13
13
MIN_SLOT = 0
14
14
MAX_SLOT = SLOT_SIZE - 1
15
15
MAX_STARTUP_SAMPLE = 37
16
+ MAX_THREADS = Integer ( ENV . fetch ( 'MAX_THREADS' , 5 ) )
16
17
IGNORE_GENERIC_CONFIG_KEYS = %i[ url host port path ] . freeze
17
18
18
19
ReloadNeeded = Class . new ( ::RedisClient ::Error )
@@ -39,18 +40,21 @@ def load_info(options, **kwargs) # rubocop:disable Metrics/AbcSize, Metrics/Cycl
39
40
errors = Array . new ( startup_size )
40
41
startup_options = options . to_a . sample ( MAX_STARTUP_SAMPLE ) . to_h
41
42
startup_nodes = ::RedisClient ::Cluster ::Node . new ( startup_options , **kwargs )
42
- threads = startup_nodes . each_with_index . map do |raw_client , idx |
43
- Thread . new ( raw_client , idx ) do |cli , i |
44
- Thread . pass
45
- reply = cli . call ( 'CLUSTER' , 'NODES' )
46
- node_info_list [ i ] = parse_node_info ( reply )
47
- rescue StandardError => e
48
- errors [ i ] = e
49
- ensure
50
- cli &.close
43
+ startup_nodes . each_slice ( MAX_THREADS * 2 ) . with_index do |chuncked_startup_nodes , chuncked_idx |
44
+ threads = chuncked_startup_nodes . each_with_index . map do |raw_client , idx |
45
+ Thread . new ( raw_client , ( MAX_THREADS * 2 * chuncked_idx ) + idx ) do |cli , i |
46
+ Thread . pass
47
+ reply = cli . call ( 'CLUSTER' , 'NODES' )
48
+ node_info_list [ i ] = parse_node_info ( reply )
49
+ rescue StandardError => e
50
+ errors [ i ] = e
51
+ ensure
52
+ cli &.close
53
+ end
51
54
end
55
+ threads . each ( &:join )
52
56
end
53
- threads . each ( & :join )
57
+
54
58
raise ::RedisClient ::Cluster ::InitialSetupError , errors if node_info_list . all? ( &:nil? )
55
59
56
60
grouped = node_info_list . compact . group_by do |rows |
@@ -267,17 +271,20 @@ def build_clients(options, pool: nil, **kwargs)
267
271
def try_map # rubocop:disable Metrics/MethodLength
268
272
results = { }
269
273
errors = { }
270
- threads = @clients . map do |k , v |
271
- Thread . new ( k , v ) do |node_key , client |
272
- Thread . pass
273
- reply = yield ( node_key , client )
274
- results [ node_key ] = reply unless reply . nil?
275
- rescue StandardError => e
276
- errors [ node_key ] = e
274
+ @clients . each_slice ( MAX_THREADS * 2 ) do |chuncked_clients |
275
+ threads = chuncked_clients . map do |k , v |
276
+ Thread . new ( k , v ) do |node_key , client |
277
+ Thread . pass
278
+ reply = yield ( node_key , client )
279
+ results [ node_key ] = reply unless reply . nil?
280
+ rescue StandardError => e
281
+ errors [ node_key ] = e
282
+ end
277
283
end
284
+
285
+ threads . each ( &:join )
278
286
end
279
287
280
- threads . each ( &:join )
281
288
[ results , errors ]
282
289
end
283
290
end
0 commit comments