chore: add user-based throttling to prevent FCM rate limiting errors
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user