blob: 2b3a6187f273b6e8c1082fe3d06a3d20555b2b2c (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
# frozen_string_literal: true
# A configurable circuit breaker to protect the application from external service failures.
# The circuit measures the amount of failures and if the threshold is exceeded, stops sending requests.
module Gitlab
module CircuitBreaker
InternalServerError = Class.new(StandardError)
DEFAULT_ERROR_THRESHOLD = 50
DEFAULT_VOLUME_THRESHOLD = 10
class << self
include ::Gitlab::Utils::StrongMemoize
# @param [String] unique name for the circuit
# @param options [Hash] an options hash setting optional values per circuit
def run_with_circuit(service_name, options = {}, &block)
circuit(service_name, options).run(exception: false, &block)
end
private
def circuit(service_name, options)
strong_memoize_with(:circuit, service_name, options) do
circuit_options = {
exceptions: [InternalServerError],
error_threshold: DEFAULT_ERROR_THRESHOLD,
volume_threshold: DEFAULT_VOLUME_THRESHOLD
}.merge(options)
Circuitbox.circuit(service_name, circuit_options)
end
end
end
end
end
|