2021-11-18 22:05:49 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module BulkImports
|
|
|
|
class NetworkError < Error
|
2022-11-25 23:54:43 +05:30
|
|
|
TRACKER_COUNTER_KEY = 'bulk_imports/%{entity_id}/%{stage}/%{tracker_id}/network_error/%{error}'
|
|
|
|
ENTITY_COUNTER_KEY = 'bulk_imports/%{entity_id}/network_error/%{error}'
|
2021-11-18 22:05:49 +05:30
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
RETRIABLE_EXCEPTIONS = Gitlab::HTTP::HTTP_TIMEOUT_ERRORS + [
|
|
|
|
EOFError, SocketError, OpenSSL::SSL::SSLError, OpenSSL::OpenSSLError,
|
|
|
|
Errno::ECONNRESET, Errno::ECONNREFUSED, Errno::EHOSTUNREACH, Errno::ENETUNREACH
|
|
|
|
].freeze
|
2021-11-18 22:05:49 +05:30
|
|
|
RETRIABLE_HTTP_CODES = [429].freeze
|
|
|
|
|
2022-08-13 15:12:31 +05:30
|
|
|
DEFAULT_RETRY_DELAY_SECONDS = 30
|
2021-11-18 22:05:49 +05:30
|
|
|
|
2022-08-13 15:12:31 +05:30
|
|
|
MAX_RETRIABLE_COUNT = 10
|
2021-11-18 22:05:49 +05:30
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
attr_reader :response
|
|
|
|
|
2021-11-18 22:05:49 +05:30
|
|
|
def initialize(message = nil, response: nil)
|
|
|
|
raise ArgumentError, 'message or response required' if message.blank? && response.blank?
|
|
|
|
|
|
|
|
super(message)
|
|
|
|
|
|
|
|
@response = response
|
|
|
|
end
|
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
def retriable?(object)
|
2021-11-18 22:05:49 +05:30
|
|
|
if retriable_exception? || retriable_http_code?
|
2022-11-25 23:54:43 +05:30
|
|
|
increment(object) <= MAX_RETRIABLE_COUNT
|
2021-11-18 22:05:49 +05:30
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def retry_delay
|
|
|
|
if response&.code == 429
|
|
|
|
response.headers.fetch('Retry-After', DEFAULT_RETRY_DELAY_SECONDS).to_i
|
|
|
|
else
|
|
|
|
DEFAULT_RETRY_DELAY_SECONDS
|
|
|
|
end.seconds
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def retriable_exception?
|
|
|
|
RETRIABLE_EXCEPTIONS.include?(cause&.class)
|
|
|
|
end
|
|
|
|
|
|
|
|
def retriable_http_code?
|
|
|
|
RETRIABLE_HTTP_CODES.include?(response&.code)
|
|
|
|
end
|
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
def increment(object)
|
|
|
|
key = object.is_a?(BulkImports::Entity) ? entity_cache_key(object) : tracker_cache_key(object)
|
|
|
|
|
|
|
|
Gitlab::Cache::Import::Caching.increment(key)
|
|
|
|
end
|
|
|
|
|
|
|
|
def tracker_cache_key(tracker)
|
|
|
|
TRACKER_COUNTER_KEY % {
|
2021-11-18 22:05:49 +05:30
|
|
|
stage: tracker.stage,
|
|
|
|
tracker_id: tracker.id,
|
|
|
|
entity_id: tracker.entity.id,
|
|
|
|
error: cause.class.name
|
|
|
|
}
|
2022-11-25 23:54:43 +05:30
|
|
|
end
|
2021-11-18 22:05:49 +05:30
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
def entity_cache_key(entity)
|
|
|
|
ENTITY_COUNTER_KEY % {
|
|
|
|
import_id: entity.bulk_import_id,
|
|
|
|
entity_id: entity.id,
|
|
|
|
error: cause.class.name
|
|
|
|
}
|
2021-11-18 22:05:49 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|