Skip to content

Commit

Permalink
Suppress circuit_success for proxy circuits
Browse files Browse the repository at this point in the history
  • Loading branch information
justinhoward committed Sep 2, 2021
1 parent dacba56 commit 0577a53
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/faulty/cache/circuit_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def finalize

self.circuit ||= Circuit.new(
Faulty::Storage::CircuitProxy.name,
notifier: notifier,
notifier: Events::FilterNotifier.new(notifier, exclude: %i[circuit_success]),
cache: Cache::Null.new
)
end
Expand Down
1 change: 1 addition & 0 deletions lib/faulty/events.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ module Events
require 'faulty/events/honeybadger_listener'
require 'faulty/events/log_listener'
require 'faulty/events/notifier'
require 'faulty/events/filter_notifier'
31 changes: 31 additions & 0 deletions lib/faulty/events/filter_notifier.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

class Faulty
module Events
# Wraps a Notifier and filters events by name
class FilterNotifier
# @param notifier [Notifier] The internal notifier to filter events for
# @param events [Array, nil] An array of events to allow. If nil, all
# {EVENTS} will be used
# @param exclude [Array, nil] An array of events to disallow. If nil,
# no events will be disallowed. Takes priority over `events`.
def initialize(notifier, events: nil, exclude: nil)
@notifier = notifier
@events = Set.new(events || EVENTS)
exclude&.each { |e| @events.delete(e) }
end

# Notify all listeners of an event
#
# If a listener raises an error while handling an event, that error will
# be captured and written to STDERR.
#
# @param (see Notifier)
def notify(event, payload)
return unless @events.include?(event)

@notifier.notify(event, payload)
end
end
end
end
2 changes: 1 addition & 1 deletion lib/faulty/storage/circuit_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def finalize

self.circuit ||= Circuit.new(
Faulty::Storage::CircuitProxy.name,
notifier: notifier,
notifier: Events::FilterNotifier.new(notifier, exclude: %i[circuit_success]),
cache: Cache::Null.new
)
end
Expand Down
7 changes: 7 additions & 0 deletions spec/cache/circuit_proxy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ def respond_to_missing?(*_args)
expect { proxy.read('foo') }.to raise_error(Faulty::CircuitTrippedError)
end

it 'does not notify for circuit sucesses by default' do
expect(notifier).not_to receive(:notify)
backend = Faulty::Cache::Mock.new
proxy = described_class.new(backend, notifier: notifier)
proxy.read('foo')
end

it 'delegates fault_tolerant? directly' do
backend = instance_double(Faulty::Cache::Mock)
marker = Object.new
Expand Down
32 changes: 32 additions & 0 deletions spec/events/filter_notifier_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# frozen_string_literal: true

RSpec.describe Faulty::Events::FilterNotifier do
let(:backend) { Faulty::Events::Notifier.new }

it 'forwards all events by default' do
filter = described_class.new(backend)
expect(backend).to receive(:notify).with(:circuit_success, {})
filter.notify(:circuit_success, {})
end

it 'forwards only given events' do
filter = described_class.new(backend, events: %i[circuit_failure])
expect(backend).to receive(:notify).with(:circuit_failure, {})
filter.notify(:circuit_success, {})
filter.notify(:circuit_failure, {})
end

it 'forwards all except exluded events' do
filter = described_class.new(backend, exclude: %i[circuit_success])
expect(backend).to receive(:notify).with(:circuit_failure, {})
filter.notify(:circuit_success, {})
filter.notify(:circuit_failure, {})
end

it 'forwards given events except excluded' do
filter = described_class.new(backend, events: %i[circuit_failure circuit_success], exclude: %i[circuit_success])
expect(backend).to receive(:notify).with(:circuit_failure, {})
filter.notify(:circuit_success, {})
filter.notify(:circuit_failure, {})
end
end
7 changes: 7 additions & 0 deletions spec/storage/circuit_proxy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,11 @@ def respond_to_missing?(*_args)
expect { proxy.entry(circuit, Faulty.current_time, true) }
.to raise_error(Faulty::CircuitTrippedError)
end

it 'does not notify for circuit sucesses by default' do
expect(notifier).not_to receive(:notify)
backend = Faulty::Storage::Null.new
proxy = described_class.new(backend, notifier: notifier)
proxy.entry(circuit, Faulty.current_time, true)
end
end

0 comments on commit 0577a53

Please sign in to comment.