Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use constants for notifier events #198

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions lib/circuitbox/circuit_breaker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ class CircuitBreaker
time_window: 60
}.freeze

# Define constants for events
FAILURE_EVENT = 'failure'.freeze
SUCCESS_EVENT = 'success'.freeze
SKIPPED_EVENT = 'skipped'.freeze
CLOSE_EVENT = 'close'.freeze
OPEN_EVENT = 'open'.freeze

# Initialize a CircuitBreaker
#
# @param service [String, Symbol] Name of the circuit for notifications and metrics store
Expand Down Expand Up @@ -117,14 +124,14 @@ def error_rate(failures = failure_count, success = success_count)
#
# @return [Integer] Number of failures
def failure_count
@circuit_store.load(stat_storage_key('failure'), raw: true).to_i
@circuit_store.load(stat_storage_key(FAILURE_EVENT), raw: true).to_i
end

# Number of successes the circuit has encountered in the current time window
#
# @return [Integer] Number of successes
def success_count
@circuit_store.load(stat_storage_key('success'), raw: true).to_i
@circuit_store.load(stat_storage_key(SUCCESS_EVENT), raw: true).to_i
end

# If the circuit is open the key indicating that the circuit is open
Expand All @@ -141,8 +148,8 @@ def try_close_next_time
def should_open?
aligned_time = align_time_to_window

failures, successes = @circuit_store.values_at(stat_storage_key('failure', aligned_time),
stat_storage_key('success', aligned_time),
failures, successes = @circuit_store.values_at(stat_storage_key(FAILURE_EVENT, aligned_time),
stat_storage_key(SUCCESS_EVENT, aligned_time),
raw: true)
# Calling to_i is only needed for moneta stores which can return a string representation of an integer.
# While readability could increase by adding .map(&:to_i) to the end of the values_at call it's also slightly
Expand Down Expand Up @@ -186,7 +193,7 @@ def open!
end

def notify_opened
notify_event('open')
notify_event(OPEN_EVENT)
end

def trip
Expand All @@ -204,21 +211,21 @@ def close!

# Running event outside of the synchronize block to allow other threads
# that may be waiting to become unblocked
notify_event('close')
notify_event(CLOSE_EVENT)
end

def half_open?
@circuit_store.key?(@half_open_storage_key)
end

def success!
increment_and_notify_event('success')
increment_and_notify_event(SUCCESS_EVENT)

close! if half_open?
end

def failure!
increment_and_notify_event('failure')
increment_and_notify_event(FAILURE_EVENT)

if half_open?
half_open_failure
Expand All @@ -228,7 +235,7 @@ def failure!
end

def skipped!
notify_event('skipped')
notify_event(SKIPPED_EVENT)
end

# Send event notification to notifier
Expand Down