diff options
author | Eugen Rochko <eugen@zeonfederated.com> | 2017-09-29 03:16:20 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-09-29 03:16:20 +0200 |
commit | f4ca116ea8f86057e91c99a1cd8e64e116c86746 (patch) | |
tree | c00b43d66305d5eac1e6f886d0aaab8fe94e2ec0 /app/lib | |
parent | 5b45c1646aa324d2eb4a3ecb8c12ed4df3cdf129 (diff) |
After 7 days of repeated delivery failures, give up on inbox (#5131)
- A successful delivery cancels it out - An incoming delivery from account of the inbox cancels it out
Diffstat (limited to 'app/lib')
-rw-r--r-- | app/lib/delivery_failure_tracker.rb | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/app/lib/delivery_failure_tracker.rb b/app/lib/delivery_failure_tracker.rb new file mode 100644 index 000000000..8d3be35de --- /dev/null +++ b/app/lib/delivery_failure_tracker.rb @@ -0,0 +1,56 @@ +# frozen_string_literal: true + +class DeliveryFailureTracker + FAILURE_DAYS_THRESHOLD = 7 + + def initialize(inbox_url) + @inbox_url = inbox_url + end + + def track_failure! + Redis.current.sadd(exhausted_deliveries_key, today) + Redis.current.sadd('unavailable_inboxes', @inbox_url) if reached_failure_threshold? + end + + def track_success! + Redis.current.del(exhausted_deliveries_key) + Redis.current.srem('unavailable_inboxes', @inbox_url) + end + + def days + Redis.current.scard(exhausted_deliveries_key) || 0 + end + + class << self + def filter(arr) + arr.reject(&method(:unavailable?)) + end + + def unavailable?(url) + Redis.current.sismember('unavailable_inboxes', url) + end + + def available?(url) + !unavailable?(url) + end + + def track_inverse_success!(from_account) + new(from_account.inbox_url).track_success! if from_account.inbox_url.present? + new(from_account.shared_inbox_url).track_success! if from_account.shared_inbox_url.present? + end + end + + private + + def exhausted_deliveries_key + "exhausted_deliveries:#{@inbox_url}" + end + + def today + Time.now.utc.strftime('%Y%m%d') + end + + def reached_failure_threshold? + days >= FAILURE_DAYS_THRESHOLD + end +end |