mirror of
https://github.com/binwiederhier/ntfy.git
synced 2025-06-27 06:40:36 +02:00
Subscription limit
This commit is contained in:
parent
5f2bb4f876
commit
fa7a45902f
3 changed files with 97 additions and 37 deletions
server
65
server/visitor.go
Normal file
65
server/visitor.go
Normal file
|
@ -0,0 +1,65 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"golang.org/x/time/rate"
|
||||
"heckel.io/ntfy/config"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
visitorExpungeAfter = 30 * time.Minute
|
||||
)
|
||||
|
||||
// visitor represents an API user, and its associated rate.Limiter used for rate limiting
|
||||
type visitor struct {
|
||||
config *config.Config
|
||||
limiter *rate.Limiter
|
||||
subscriptions int
|
||||
seen time.Time
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
func newVisitor(conf *config.Config) *visitor {
|
||||
return &visitor{
|
||||
config: conf,
|
||||
limiter: rate.NewLimiter(conf.RequestLimit, conf.RequestLimitBurst),
|
||||
seen: time.Now(),
|
||||
}
|
||||
}
|
||||
|
||||
func (v *visitor) RequestAllowed() error {
|
||||
if !v.limiter.Allow() {
|
||||
return errHTTPTooManyRequests
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (v *visitor) AddSubscription() error {
|
||||
v.mu.Lock()
|
||||
defer v.mu.Unlock()
|
||||
if v.subscriptions >= v.config.SubscriptionLimit {
|
||||
return errHTTPTooManyRequests
|
||||
}
|
||||
v.subscriptions++
|
||||
return nil
|
||||
}
|
||||
|
||||
func (v *visitor) RemoveSubscription() {
|
||||
v.mu.Lock()
|
||||
defer v.mu.Unlock()
|
||||
v.subscriptions--
|
||||
}
|
||||
|
||||
func (v *visitor) Keepalive() {
|
||||
v.mu.Lock()
|
||||
defer v.mu.Unlock()
|
||||
v.seen = time.Now()
|
||||
}
|
||||
|
||||
func (v *visitor) Stale() bool {
|
||||
v.mu.Lock()
|
||||
defer v.mu.Unlock()
|
||||
v.seen = time.Now()
|
||||
return time.Since(v.seen) > visitorExpungeAfter
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue