From dec9d3872f674d2f56672442630e673c73639171 Mon Sep 17 00:00:00 2001 From: Muhsin Keloth Date: Wed, 16 Jul 2025 09:06:33 +0530 Subject: [PATCH] chore: add user-based throttling to prevent FCM rate limiting errors --- .../notification/push_notification_service.rb | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/app/services/notification/push_notification_service.rb b/app/services/notification/push_notification_service.rb index 9878107c1..a695c3dd9 100644 --- a/app/services/notification/push_notification_service.rb +++ b/app/services/notification/push_notification_service.rb @@ -5,6 +5,7 @@ class Notification::PushNotificationService def perform return unless user_subscribed_to_notification? + return if user_notification_throttled? notification_subscriptions.each do |subscription| send_browser_push(subscription) @@ -26,6 +27,26 @@ class Notification::PushNotificationService false end + def user_notification_throttled? + # Throttle push notifications to prevent FCM rate limiting errors + # Limits each user to 5 notifications per minute to avoid overwhelming FCM API + max_notifications_per_minute = 5 + one_minute_ago = 1.minute.ago + + recent_notifications_count = Notification.where( + user: user, + account: notification.account, + created_at: one_minute_ago..Time.current + ).count + + if recent_notifications_count >= max_notifications_per_minute + Rails.logger.info "User #{user.email} throttled: #{recent_notifications_count} notifications in last minute" + return true + end + + false + end + def conversation @conversation ||= notification.conversation end