Throttle discord alerts
This commit is contained in:
@@ -2,6 +2,7 @@ package discord
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/bwmarrin/discordgo"
|
||||
@@ -19,6 +20,8 @@ type DiscordController struct {
|
||||
HostName string
|
||||
Environment string
|
||||
UserRepo *repo.UserRepository
|
||||
lastSent map[string]time.Time
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
func NewDiscordController(userRepo *repo.UserRepository, hostName string, environment string) *DiscordController {
|
||||
@@ -28,6 +31,7 @@ func NewDiscordController(userRepo *repo.UserRepository, hostName string, enviro
|
||||
HostName: hostName,
|
||||
Environment: environment,
|
||||
UserRepo: userRepo,
|
||||
lastSent: make(map[string]time.Time),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -106,6 +110,14 @@ func (c *DiscordController) NotifyAccountDelete(userID int64, paymentProvider st
|
||||
}
|
||||
|
||||
func (c *DiscordController) NotifyPotentialAbuse(message string) {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
now := time.Now()
|
||||
if lastTime, exists := c.lastSent[message]; exists && now.Sub(lastTime) < time.Minute {
|
||||
log.Infof("Skipping duplicate abuse notification: %s", message)
|
||||
return
|
||||
}
|
||||
c.lastSent[message] = now
|
||||
c.Notify(fmt.Sprintf("%s: %s", c.HostName, message))
|
||||
}
|
||||
|
||||
|
||||
@@ -147,6 +147,8 @@ func (r *RateLimitMiddleware) APIRateLimitForUserMiddleware(urlSanitizer func(_
|
||||
func (r *RateLimitMiddleware) getLimiter(reqPath string, reqMethod string) *limiter.Limiter {
|
||||
if reqPath == "/users/ott" ||
|
||||
reqPath == "/users/verify-email" ||
|
||||
reqPath == "/user/change-email" ||
|
||||
reqPath == "/users/public-key" ||
|
||||
reqPath == "/public-collection/verify-password" ||
|
||||
reqPath == "/family/accept-invite" ||
|
||||
reqPath == "/users/srp/attributes" ||
|
||||
|
||||
Reference in New Issue
Block a user