@@ -11,6 +11,7 @@ class LatencyReplica
1111 attr_reader :replica_clients
1212
1313 DUMMY_LATENCY_SEC = 100.0
14+ MEASURE_ATTEMPT_COUNT = 10
1415
1516 def initialize ( replications , options , pool , **kwargs )
1617 super
@@ -37,17 +38,21 @@ def any_replica_node_key(seed: nil)
3738
3839 private
3940
40- def measure_latencies ( clients ) # rubocop:disable Metrics/MethodLength
41+ def measure_latencies ( clients ) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
4142 latencies = { }
4243
4344 clients . each_slice ( ::RedisClient ::Cluster ::Node ::MAX_THREADS ) do |chuncked_clients |
4445 threads = chuncked_clients . map do |k , v |
4546 Thread . new ( k , v ) do |node_key , client |
4647 Thread . pass
47- starting = Process . clock_gettime ( Process ::CLOCK_MONOTONIC )
48- client . send ( :call_once , 'PING' )
49- ending = Process . clock_gettime ( Process ::CLOCK_MONOTONIC )
50- latencies [ node_key ] = ending - starting
48+ min = DUMMY_LATENCY_SEC + 1.0
49+ MEASURE_ATTEMPT_COUNT . times do
50+ starting = Process . clock_gettime ( Process ::CLOCK_MONOTONIC )
51+ client . send ( :call_once , 'PING' )
52+ duration = Process . clock_gettime ( Process ::CLOCK_MONOTONIC ) - starting
53+ min = duration if duration < min
54+ end
55+ latencies [ node_key ] = min
5156 rescue StandardError
5257 latencies [ node_key ] = DUMMY_LATENCY_SEC
5358 end
0 commit comments