From ee9f1d7adb1939442880c6e9faf5534d38452d29 Mon Sep 17 00:00:00 2001 From: Muhsin Keloth Date: Wed, 13 Aug 2025 13:32:22 +0530 Subject: [PATCH] chore: Handle WebPush rate limiting in push notification service (#12184) Implemented a rescue block for WebPush::TooManyRequests that logs warnings during rate limiting events. This captures user email and account ID for better traceability. We will implement a proper throttling mechanism after identifying patterns across accounts. --- .../notification/push_notification_service.rb | 23 +++++++++++++------ 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/app/services/notification/push_notification_service.rb b/app/services/notification/push_notification_service.rb index 9878107c1..125ad9113 100644 --- a/app/services/notification/push_notification_service.rb +++ b/app/services/notification/push_notification_service.rb @@ -68,14 +68,23 @@ class Notification::PushNotificationService WebPush.payload_send(**browser_push_payload(subscription)) Rails.logger.info("Browser push sent to #{user.email} with title #{push_message[:title]}") - rescue WebPush::ExpiredSubscription, WebPush::InvalidSubscription, WebPush::Unauthorized => e - Rails.logger.info "WebPush subscription expired: #{e.message}" - subscription.destroy! - rescue Errno::ECONNRESET, Net::OpenTimeout, Net::ReadTimeout => e - Rails.logger.error "WebPush operation error: #{e.message}" rescue StandardError => e - ChatwootExceptionTracker.new(e, account: notification.account).capture_exception - true + handle_browser_push_error(e, subscription) + end + + def handle_browser_push_error(error, subscription) + case error + when WebPush::ExpiredSubscription, WebPush::InvalidSubscription, WebPush::Unauthorized + Rails.logger.info "WebPush subscription expired: #{error.message}" + subscription.destroy! + when WebPush::TooManyRequests + Rails.logger.warn "WebPush rate limited for #{user.email} on account #{notification.account.id}: #{error.message}" + when Errno::ECONNRESET, Net::OpenTimeout, Net::ReadTimeout + Rails.logger.error "WebPush operation error: #{error.message}" + else + ChatwootExceptionTracker.new(error, account: notification.account).capture_exception + true + end end def send_fcm_push(subscription)