Last active
March 15, 2018 16:19
-
-
Save Skulli/80a3f31d1b25b7b98c025b2bd182aa4a to your computer and use it in GitHub Desktop.
throttle exceptions (see https://github.com/smartinez87/exception_notification/issues/106)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
config.ignore_if do |exception, options| | |
# use beginning to prevent non unique keys (they include object ids and such) | |
key = exception.message.split("for").first[0..30] | |
ExpirableKey.new(key).exist_with_renew | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ExpirableKey | |
attr_accessor :key, :redis, :expire_in | |
def initialize(key, connection: Redis.current, expire_in: 60) | |
self.redis = connection | |
self.expire_in = expire_in | |
self.key = key | |
end | |
def exist? | |
redis.get(key).present? | |
end | |
def empty? | |
not(exist?) | |
end | |
def exist_with_renew?(value: true) | |
result = exist? | |
set(value) | |
result | |
end | |
def set(value = true) | |
redis.set(key, value) | |
redis.expire(key, expire_in) if expire_in.present? | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment