Skip to content

Commit ed46bd4

Browse files
authored
fix: prevent calculation with float for measure latency (#105)
1 parent 139de24 commit ed46bd4

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

lib/redis_client/cluster/node/latency_replica.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class LatencyReplica
1010

1111
attr_reader :replica_clients
1212

13-
DUMMY_LATENCY_SEC = 100.0
13+
DUMMY_LATENCY_NSEC = 100 * 1000 * 1000 * 1000
1414
MEASURE_ATTEMPT_COUNT = 10
1515

1616
def initialize(replications, options, pool, **kwargs)
@@ -45,16 +45,18 @@ def measure_latencies(clients) # rubocop:disable Metrics/MethodLength, Metrics/A
4545
threads = chuncked_clients.map do |k, v|
4646
Thread.new(k, v) do |node_key, client|
4747
Thread.pass
48-
min = DUMMY_LATENCY_SEC + 1.0
48+
49+
min = DUMMY_LATENCY_NSEC
4950
MEASURE_ATTEMPT_COUNT.times do
50-
starting = Process.clock_gettime(Process::CLOCK_MONOTONIC)
51+
starting = Process.clock_gettime(Process::CLOCK_MONOTONIC, :nanosecond)
5152
client.send(:call_once, 'PING')
52-
duration = Process.clock_gettime(Process::CLOCK_MONOTONIC) - starting
53+
duration = Process.clock_gettime(Process::CLOCK_MONOTONIC, :nanosecond) - starting
5354
min = duration if duration < min
5455
end
56+
5557
latencies[node_key] = min
5658
rescue StandardError
57-
latencies[node_key] = DUMMY_LATENCY_SEC
59+
latencies[node_key] = DUMMY_LATENCY_NSEC
5860
end
5961
end
6062

0 commit comments

Comments
 (0)