fix: rate-limit daily subscription insufficient balance notifications to 6 hours
Users with daily subscriptions and low balance were getting "Подписка приостановлена" notification every 30 minutes (on each charge cycle). Now rate-limited via Redis cache to max 1 notification per 6 hours per subscription.
This commit is contained in:
@@ -146,9 +146,22 @@ class DailySubscriptionService:
|
||||
# Недостаточно средств - приостанавливаем подписку
|
||||
await suspend_daily_subscription_insufficient_balance(db, subscription)
|
||||
|
||||
# Уведомляем пользователя
|
||||
# Уведомляем пользователя (rate-limit: 1 раз в 6 часов)
|
||||
if self._bot:
|
||||
await self._notify_insufficient_balance(user, subscription, daily_price)
|
||||
from app.utils.cache import cache
|
||||
|
||||
cache_key = f'daily_insuf_notify:{subscription.id}'
|
||||
try:
|
||||
already_notified = await cache.get(cache_key)
|
||||
except Exception:
|
||||
already_notified = None
|
||||
|
||||
if not already_notified:
|
||||
await self._notify_insufficient_balance(user, subscription, daily_price)
|
||||
try:
|
||||
await cache.set(cache_key, '1', expire=21600) # 6 hours
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
logger.info(
|
||||
'Подписка приостановлена: недостаточно средств (баланс: требуется: )',
|
||||
|
||||
Reference in New Issue
Block a user