@@ -11,6 +11,7 @@ class LatencyReplica
11
11
attr_reader :replica_clients
12
12
13
13
DUMMY_LATENCY_SEC = 100.0
14
+ MEASURE_ATTEMPT_COUNT = 10
14
15
15
16
def initialize ( replications , options , pool , **kwargs )
16
17
super
@@ -37,17 +38,21 @@ def any_replica_node_key(seed: nil)
37
38
38
39
private
39
40
40
- def measure_latencies ( clients ) # rubocop:disable Metrics/MethodLength
41
+ def measure_latencies ( clients ) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
41
42
latencies = { }
42
43
43
44
clients . each_slice ( ::RedisClient ::Cluster ::Node ::MAX_THREADS ) do |chuncked_clients |
44
45
threads = chuncked_clients . map do |k , v |
45
46
Thread . new ( k , v ) do |node_key , client |
46
47
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
51
56
rescue StandardError
52
57
latencies [ node_key ] = DUMMY_LATENCY_SEC
53
58
end
0 commit comments