Class: SlowRide::Adapter

Inherits:
Object
  • Object
show all
Defined in:
lib/slow_ride.rb

Direct Known Subclasses

Redis

Instance Method Summary collapse

Constructor Details

#initialize(failure_threshold:, minimum_checks: 1_000, &failure_handler) ⇒ Adapter

Returns a new instance of Adapter.



26
27
28
29
30
# File 'lib/slow_ride.rb', line 26

def initialize(failure_threshold:, minimum_checks: 1_000, &failure_handler)
  @failure_threshold = failure_threshold
  @minimum_checks = minimum_checks
  @failure_handler = failure_handler
end

Instance Method Details

#check ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/slow_ride.rb', line 32

def check
  checked_count = checked

  begin
    yield
  rescue => ex
    failed_count = failed

    if checked_count >= @minimum_checks && failed_count.to_f / checked_count >= @failure_threshold
      @failure_handler.call failed_count, checked_count
      failure_threshold_exceeded failed: failed_count, checked: checked_count
    end
    raise ex
  end
end

#failure_threshold_exceeded(failed:, checked:) ⇒ Object



48
49
# File 'lib/slow_ride.rb', line 48

def failure_threshold_exceeded(failed:, checked:)
end