Class: FaradayMiddleware::CircuitBreaker::Middleware

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/faraday_middleware/circuit_breaker/middleware.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Middleware

Returns a new instance of Middleware.



9
10
11
12
13
14
15
16
17
# File 'lib/faraday_middleware/circuit_breaker/middleware.rb', line 9

def initialize(app, options = {})
  super(app)
  assert_valid_options!(options)

  @option_set = OptionSet.new(options)

  setup_data_store
  setup_notifiers
end

Instance Method Details

#call(env) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/faraday_middleware/circuit_breaker/middleware.rb', line 19

def call(env)
  base_url = option_set.cache_key_generator.call(env.url)
  Stoplight(base_url) do
    @app.call(env)
  end
  .with_cool_off_time(option_set.timeout)
  .with_threshold(option_set.threshold)
  .with_fallback { |e| option_set.fallback.call(env, e) }
  .with_error_handler { |err, handler| option_set.error_handler.call(err, handler) }
  .run
end