2021-05-31 10:58:16 +02:00
|
|
|
package limiter
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
/* The Limiter struct saves all interactions in a map of lists indexed by user id going back the time limit.
|
|
|
|
When checking if a user is allowed to perform an action, it traverses the list for that userid.
|
|
|
|
If an item is older than the time limit, remove it and don't count.
|
|
|
|
If it is in the limit, then count. If the amount of interactions is higher than the limit, return an error */
|
|
|
|
type Limiter struct {
|
|
|
|
TimeLimit time.Duration
|
|
|
|
RateLimit int
|
2021-05-31 11:15:54 +02:00
|
|
|
Logs map[string][]*Action
|
2021-05-31 10:58:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Limiter) LogInteraction(userid string, action string) {
|
2021-05-31 11:15:54 +02:00
|
|
|
ac := &Action{
|
2021-05-31 10:58:16 +02:00
|
|
|
Timestamp: time.Now(),
|
|
|
|
Type: action,
|
2021-05-31 11:15:54 +02:00
|
|
|
}
|
|
|
|
l.Logs[userid] = append(l.Logs[userid], ac)
|
2021-05-31 10:58:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* CheckAllowed counts the amount of log entries for a given userid, making sure to delete and not count the expired ones.
|
|
|
|
Returns an error if the amount of log entries exceeds the ratelimit */
|
|
|
|
func (l *Limiter) CheckAllowed(userid string) error {
|
|
|
|
counter := 0
|
2021-05-31 11:35:46 +02:00
|
|
|
expiredEntries := make([]*Action, 0)
|
2021-05-31 10:58:16 +02:00
|
|
|
for i := 0; i < len(l.Logs[userid]); i++ {
|
|
|
|
/* If the timestamp plus the timelimit is happened before "Now" */
|
|
|
|
if l.Logs[userid][i].Timestamp.Add(l.TimeLimit).Before(time.Now()) {
|
2021-05-31 11:35:46 +02:00
|
|
|
expiredEntries = append(expiredEntries, l.Logs[userid][i])
|
2021-05-31 10:58:16 +02:00
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
counter++
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* remove entries */
|
|
|
|
for i := 0; i < len(expiredEntries); i++ {
|
|
|
|
l.removeAction(userid, expiredEntries[i])
|
|
|
|
}
|
|
|
|
|
2021-05-31 14:32:04 +02:00
|
|
|
if counter > l.RateLimit {
|
2021-05-31 10:58:16 +02:00
|
|
|
return errors.New("rate limit exceeded")
|
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-31 11:35:46 +02:00
|
|
|
func (l *Limiter) removeAction(userid string, a *Action) {
|
|
|
|
index := 0
|
|
|
|
for i := 0; i < len(l.Logs[userid]); i++ {
|
|
|
|
if l.Logs[userid][i] == a {
|
|
|
|
index = i
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
l.Logs[userid][index] = l.Logs[userid][len(l.Logs[userid])-1]
|
2021-05-31 10:58:16 +02:00
|
|
|
l.Logs[userid] = l.Logs[userid][:len(l.Logs[userid])-1]
|
|
|
|
}
|