gosora/common/ratelimit.go

101 lines
2.2 KiB
Go
Raw Permalink Normal View History

package common
import (
2022-02-21 03:32:53 +00:00
"errors"
"strconv"
"sync"
"time"
)
var ErrBadRateLimiter = errors.New("That rate limiter doesn't exist")
var ErrExceededRateLimit = errors.New("You're exceeding a rate limit. Please wait a while before trying again.")
// TODO: Persist rate limits to disk
type RateLimiter interface {
2022-02-21 03:32:53 +00:00
LimitIP(limit, ip string) error
LimitUser(limit string, user int) error
}
type RateData struct {
2022-02-21 03:32:53 +00:00
value int
floorTime int
}
type RateFence struct {
2022-02-21 03:32:53 +00:00
duration int
max int
}
// TODO: Optimise this by using something other than a string when possible
type RateLimit struct {
2022-02-21 03:32:53 +00:00
data map[string][]RateData
fences []RateFence
2022-02-21 03:32:53 +00:00
sync.RWMutex
}
func NewRateLimit(fences []RateFence) *RateLimit {
2022-02-21 03:32:53 +00:00
for i, fence := range fences {
fences[i].duration = fence.duration * 1000 * 1000 * 1000
}
return &RateLimit{data: make(map[string][]RateData), fences: fences}
}
func (l *RateLimit) Limit(name string, ltype int) error {
2022-02-21 03:32:53 +00:00
l.Lock()
defer l.Unlock()
data, ok := l.data[name]
if !ok {
data = make([]RateData, len(l.fences))
for i, _ := range data {
data[i] = RateData{0, int(time.Now().Unix())}
}
}
for i, field := range data {
fence := l.fences[i]
diff := int(time.Now().Unix()) - field.floorTime
if diff >= fence.duration {
field = RateData{0, int(time.Now().Unix())}
data[i] = field
}
if field.value > fence.max {
return ErrExceededRateLimit
}
field.value++
data[i] = field
}
return nil
}
type DefaultRateLimiter struct {
2022-02-21 03:32:53 +00:00
limits map[string]*RateLimit
}
func NewDefaultRateLimiter() *DefaultRateLimiter {
2022-02-21 03:32:53 +00:00
return &DefaultRateLimiter{map[string]*RateLimit{
"register": NewRateLimit([]RateFence{{int(time.Hour / 2), 1}}),
}}
}
func (l *DefaultRateLimiter) LimitIP(limit, ip string) error {
2022-02-21 03:32:53 +00:00
limiter, ok := l.limits[limit]
if !ok {
return ErrBadRateLimiter
}
return limiter.Limit(ip, 0)
}
func (l *DefaultRateLimiter) LimitUser(limit string, user int) error {
2022-02-21 03:32:53 +00:00
limiter, ok := l.limits[limit]
if !ok {
return ErrBadRateLimiter
}
return limiter.Limit(strconv.Itoa(user), 1)
}