feat: Implement notification rate limiting mechanism

- Add in-memory and Redis-based notification rate limiting
- Create configurable hourly notification limits
- Implement notification limit checking for user notifications
- Add environment variables for customizing notification limits
This commit is contained in:
1808837298@qq.com
2025-02-18 15:30:43 +08:00
parent 9d9c461c48
commit 56f6b2ab56
4 changed files with 138 additions and 1 deletions

View File

@@ -25,6 +25,17 @@ func NotifyUser(user *model.UserCache, data dto.Notify) error {
if !ok {
notifyType = constant.NotifyTypeEmail
}
// Check notification limit
canSend, err := CheckNotificationLimit(user.Id, data.Type)
if err != nil {
common.SysError(fmt.Sprintf("failed to check notification limit: %s", err.Error()))
return err
}
if !canSend {
return fmt.Errorf("notification limit exceeded for user %d with type %s", user.Id, notifyType)
}
switch notifyType {
case constant.NotifyTypeEmail:
userEmail := user.Email
@@ -46,7 +57,7 @@ func NotifyUser(user *model.UserCache, data dto.Notify) error {
// TODO: 实现webhook通知
_ = webhookURL // 临时处理未使用警告等待webhook实现
}
return nil // 添加缺失的return
return nil
}
func sendEmailNotify(userEmail string, data dto.Notify) error {