Commit b044f855 authored by Michael Kozono's avatar Michael Kozono

Refactor mostly for readability

parent ce9dc2b7
...@@ -39,7 +39,7 @@ module Gitlab ...@@ -39,7 +39,7 @@ module Gitlab
handle_error(lease[:error]) handle_error(lease[:error])
# When no new event is found sleep for a few moments # When no new event is found sleep for a few moments
arbitrary_sleep(lease[:ttl]) sleep_break(lease[:ttl])
end end
def find_and_handle_events! def find_and_handle_events!
...@@ -54,26 +54,26 @@ module Gitlab ...@@ -54,26 +54,26 @@ module Gitlab
private private
def handle_error(did_error) def handle_error(error)
track_failing_since(did_error) track_failing_since(error)
if excessive_errors? if excessive_errors?
exit!("Consecutive errors for over #{MAX_ERROR_DURATION} seconds") exit!("Consecutive errors for over #{MAX_ERROR_DURATION} seconds")
end end
end end
def track_failing_since(did_error) def track_failing_since(error)
if did_error if error
@failing_since ||= Time.now @failing_since ||= Time.now.utc
else else
@failing_since = nil @failing_since = nil
end end
end end
def excessive_errors? def excessive_errors?
return if @failing_since.nil? return unless @failing_since
MAX_ERROR_DURATION < (Time.now - @failing_since) (Time.now.utc - @failing_since) > MAX_ERROR_DURATION
end end
def handle_events(batch, previous_batch_last_id) def handle_events(batch, previous_batch_last_id)
...@@ -152,25 +152,27 @@ module Gitlab ...@@ -152,25 +152,27 @@ module Gitlab
end end
# rubocop: enable CodeReuse/ActiveRecord # rubocop: enable CodeReuse/ActiveRecord
# Sleeps for the expired TTL that remains on the lease plus some random seconds. # Sleeps for the specified duration plus some random seconds.
# #
# This allows multiple GeoLogCursors to randomly process a batch of events, # This allows multiple GeoLogCursors to randomly process a batch of events,
# without favouring the shortest path (or latency). # without favouring the shortest path (or latency).
def arbitrary_sleep(delay) #
jitter = rand(1..20) * 0.1 # Exits early if needed.
sleep_break(delay + jitter)
end
def sleep_break(seconds) def sleep_break(seconds)
while seconds > 0.0 sleep(random_jitter_time)
to_sleep = seconds > 1.0 ? 1.0 : seconds
seconds -= to_sleep seconds.to_i.times do
sleep(to_sleep)
break if exit? break if exit?
sleep(1)
end end
end end
# Returns a random float from 0.1 to 2.0
def random_jitter_time
rand(1..20) * 0.1
end
def gap_tracking def gap_tracking
@gap_tracking ||= ::Gitlab::Geo::EventGapTracking.new(logger) @gap_tracking ||= ::Gitlab::Geo::EventGapTracking.new(logger)
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment