1
0
Fork 0
mirror of https://github.com/binwiederhier/ntfy.git synced 2024-11-22 19:33:27 +01:00

Get rid of v.messages counter

This commit is contained in:
binwiederhier 2023-01-27 10:06:48 -05:00
parent 9e9caee639
commit ccc2dd1128
2 changed files with 9 additions and 21 deletions

View file

@ -597,7 +597,6 @@ func (s *Server) handlePublishWithoutResponse(r *http.Request, v *visitor) (*mes
return nil, err return nil, err
} }
} }
v.IncrementMessages()
if s.userManager != nil && v.user != nil { if s.userManager != nil && v.user != nil {
s.userManager.EnqueueStats(v.user.ID, v.Stats()) // FIXME this makes no sense for tier-less users s.userManager.EnqueueStats(v.user.ID, v.Stats()) // FIXME this makes no sense for tier-less users
} }

View file

@ -58,10 +58,9 @@ type visitor struct {
userManager *user.Manager // May be nil userManager *user.Manager // May be nil
ip netip.Addr // Visitor IP address ip netip.Addr // Visitor IP address
user *user.User // Only set if authenticated user, otherwise nil user *user.User // Only set if authenticated user, otherwise nil
messages int64 // Number of messages sent, reset every day
emails int64 // Number of emails sent, reset every day emails int64 // Number of emails sent, reset every day
requestLimiter *rate.Limiter // Rate limiter for (almost) all requests (including messages) requestLimiter *rate.Limiter // Rate limiter for (almost) all requests (including messages)
messagesLimiter *util.FixedLimiter // Rate limiter for messages, may be nil messagesLimiter *util.FixedLimiter // Rate limiter for messages
emailsLimiter *rate.Limiter // Rate limiter for emails emailsLimiter *rate.Limiter // Rate limiter for emails
subscriptionLimiter util.Limiter // Fixed limiter for active subscriptions (ongoing connections) subscriptionLimiter util.Limiter // Fixed limiter for active subscriptions (ongoing connections)
bandwidthLimiter util.Limiter // Limiter for attachment bandwidth downloads bandwidthLimiter util.Limiter // Limiter for attachment bandwidth downloads
@ -124,7 +123,6 @@ func newVisitor(conf *Config, messageCache *messageCache, userManager *user.Mana
userManager: userManager, // May be nil userManager: userManager, // May be nil
ip: ip, ip: ip,
user: user, user: user,
messages: messages,
emails: emails, emails: emails,
firebase: time.Unix(0, 0), firebase: time.Unix(0, 0),
seen: time.Now(), seen: time.Now(),
@ -135,7 +133,7 @@ func newVisitor(conf *Config, messageCache *messageCache, userManager *user.Mana
bandwidthLimiter: nil, // Set in resetLimiters bandwidthLimiter: nil, // Set in resetLimiters
accountLimiter: nil, // Set in resetLimiters, may be nil accountLimiter: nil, // Set in resetLimiters, may be nil
} }
v.resetLimiters() v.resetLimiters(messages)
return v return v
} }
@ -177,7 +175,7 @@ func (v *visitor) FirebaseTemporarilyDeny() {
} }
func (v *visitor) MessageAllowed() error { func (v *visitor) MessageAllowed() error {
if v.messagesLimiter != nil && v.messagesLimiter.Allow(1) != nil { if v.messagesLimiter.Allow(1) != nil {
return errVisitorLimitReached return errVisitorLimitReached
} }
return nil return nil
@ -228,12 +226,6 @@ func (v *visitor) Stale() bool {
return time.Since(v.seen) > visitorExpungeAfter return time.Since(v.seen) > visitorExpungeAfter
} }
func (v *visitor) IncrementMessages() {
v.mu.Lock()
defer v.mu.Unlock()
v.messages++
}
func (v *visitor) IncrementEmails() { func (v *visitor) IncrementEmails() {
v.mu.Lock() v.mu.Lock()
defer v.mu.Unlock() defer v.mu.Unlock()
@ -244,7 +236,7 @@ func (v *visitor) Stats() *user.Stats {
v.mu.Lock() v.mu.Lock()
defer v.mu.Unlock() defer v.mu.Unlock()
return &user.Stats{ return &user.Stats{
Messages: v.messages, Messages: v.messagesLimiter.Value(),
Emails: v.emails, Emails: v.emails,
} }
} }
@ -252,11 +244,8 @@ func (v *visitor) Stats() *user.Stats {
func (v *visitor) ResetStats() { func (v *visitor) ResetStats() {
v.mu.Lock() v.mu.Lock()
defer v.mu.Unlock() defer v.mu.Unlock()
v.messages = 0
v.emails = 0 v.emails = 0
if v.messagesLimiter != nil { v.messagesLimiter.Reset()
v.messagesLimiter.Reset()
}
} }
// SetUser sets the visitors user to the given value // SetUser sets the visitors user to the given value
@ -266,7 +255,7 @@ func (v *visitor) SetUser(u *user.User) {
shouldResetLimiters := v.user.TierID() != u.TierID() // TierID works with nil receiver shouldResetLimiters := v.user.TierID() != u.TierID() // TierID works with nil receiver
v.user = u v.user = u
if shouldResetLimiters { if shouldResetLimiters {
v.resetLimiters() v.resetLimiters(0)
} }
} }
@ -281,11 +270,11 @@ func (v *visitor) MaybeUserID() string {
return "" return ""
} }
func (v *visitor) resetLimiters() { func (v *visitor) resetLimiters(messages int64) {
log.Debug("%s Resetting limiters for visitor", v.stringNoLock()) log.Debug("%s Resetting limiters for visitor", v.stringNoLock())
limits := v.limitsNoLock() limits := v.limitsNoLock()
v.requestLimiter = rate.NewLimiter(limits.RequestLimitReplenish, limits.RequestLimitBurst) v.requestLimiter = rate.NewLimiter(limits.RequestLimitReplenish, limits.RequestLimitBurst)
v.messagesLimiter = util.NewFixedLimiterWithValue(limits.MessageLimit, v.messages) v.messagesLimiter = util.NewFixedLimiterWithValue(limits.MessageLimit, messages)
v.emailsLimiter = rate.NewLimiter(limits.EmailLimitReplenish, limits.EmailLimitBurst) v.emailsLimiter = rate.NewLimiter(limits.EmailLimitReplenish, limits.EmailLimitBurst)
v.bandwidthLimiter = util.NewBytesLimiter(int(limits.AttachmentBandwidthLimit), oneDay) v.bandwidthLimiter = util.NewBytesLimiter(int(limits.AttachmentBandwidthLimit), oneDay)
if v.user == nil { if v.user == nil {
@ -350,7 +339,7 @@ func configBasedVisitorLimits(conf *Config) *visitorLimits {
func (v *visitor) Info() (*visitorInfo, error) { func (v *visitor) Info() (*visitorInfo, error) {
v.mu.Lock() v.mu.Lock()
messages := v.messages messages := v.messagesLimiter.Value()
emails := v.emails emails := v.emails
v.mu.Unlock() v.mu.Unlock()
var attachmentsBytesUsed int64 var attachmentsBytesUsed int64