Class: EventStore::HTTP::Retry

Inherits:
Object
  • Object
show all
Includes:
Log::Dependency
Defined in:
lib/event_store/http/retry.rb,
lib/event_store/http/retry/telemetry.rb,
lib/event_store/http/retry/substitute.rb

Direct Known Subclasses

Substitute::Retry

Defined Under Namespace

Modules: Defaults, Substitute, Telemetry

Constant Summary collapse

Trigger =
Class.new StandardError

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build(settings = nil, namespace: nil) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/event_store/http/retry.rb', line 21

def self.build(settings=nil, namespace: nil)
  settings ||= Settings.instance
  namespace ||= Array(namespace)

  instance = new
  ::Telemetry.configure instance
  settings.set instance, namespace
  instance
end

.register_telemetry_sink(instance) ⇒ Object



31
32
33
34
35
# File 'lib/event_store/http/retry.rb', line 31

def self.register_telemetry_sink(instance)
  sink = Telemetry::Sink.new
  instance.telemetry.register sink
  sink
end

Instance Method Details

#call(&block) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/event_store/http/retry.rb', line 37

def call(&block)
  retries ||= 0

  logger.trace { "Performing operation (Retries: #{retries}/#{retry_limit})" }

  return_value = block.(self, retries)

  logger.debug { "Operation succeeded (Retries: #{retries}/#{retry_limit})" }

  return_value

rescue => error
  if retries == retry_limit
    logger.error { "Operation failed; retry limit exceeded (Retries: #{retries}/#{retry_limit}, ErrorClass: #{error.class}, ErrorMessage: #{error.message.empty? ? '(none)' : error.message})" }
    raise error
  end

  logger.warn { "Operation failed; retrying (Retries: #{retries}/#{retry_limit}, ErrorClass: #{error.class}, ErrorMessage: #{error.message.empty? ? '(none)' : error.message})" }

  retries += 1
  sleep retry_duration_seconds
  record_retry error, retries

  retry
end

#failed(error = nil) ⇒ Object



63
64
65
66
67
# File 'lib/event_store/http/retry.rb', line 63

def failed(error=nil)
  error ||= Trigger.new

  raise error
end

#record_retry(error, retries) ⇒ Object



69
70
71
# File 'lib/event_store/http/retry.rb', line 69

def record_retry(error, retries)
  telemetry.record :retried, Telemetry::Retried.new(error, retries, retry_limit)
end

#retry_durationObject



17
18
19
# File 'lib/event_store/http/retry.rb', line 17

def retry_duration
  @retry_duration ||= Defaults.retry_duration
end

#retry_duration_secondsObject



73
74
75
# File 'lib/event_store/http/retry.rb', line 73

def retry_duration_seconds
  Rational(retry_duration, 1_000)
end

#retry_limitObject



13
14
15
# File 'lib/event_store/http/retry.rb', line 13

def retry_limit
  @retry_limit ||= Defaults.retry_limit
end